.net (c#)快捷软键盘
先看看效果吧:
哈哈。怎样?从极品时刻表学来的,
极品时刻表的效果:
正好我这个MusicManager与之相似,就模拟实现了个我的MusicQuickKeyBoard
哈哈。
顺便提供了按歌曲名称、按歌手名称搜索两种方式。非常方便客户使用,特别是广播播出系统播出站主持人的快捷操作,用户很满意呀。
总结:我们设计思路怎么来,多使用一些比较成熟的小软件,它们既然成熟,就很贴切日常使用,肯定有快捷之处,需要我们不断的发觉。当 自己的项目用到之日,你就很快有了设计思路并且不是凭空遐想、随便设计。因为你的设计有依据、有实践。客户需要方便、快捷、功能于一身。
1).这里将键盘封装成一个控件MusicKeyBoard.cs
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace pcd.WinControl
10{
11
12 public partial class MusicKeyBoard : UserControl
13 {
14 public MusicKeyBoard()
15 {
16 InitializeComponent();
17 }
18 private bool _IsActive;
19 private string _ResultValue;
20 Properties#region Properties
21 [Category("Appearance"),
22 DefaultValue(typeof(bool), "false"),
23 Description("当前控件是否激活")]
24 public bool IsActive
25 {
26 get { return _IsActive; }
27 set { _IsActive = value; }
28 }
29
30 [Category("Appearance"),
31 Description("起始数字")]
32 public string ResultValue
33 {
34 get
35 {
36 if (ListResult.SelectedValue != null)
37 return ListResult.SelectedValue.ToString();
38 else return "";
39 }
40 set { _ResultValue = value; }
41 }
42
43 #endregion
44
45 MusicKeyBoardValueChangedHandle#region MusicKeyBoardValueChangedHandle
46 public delegate void MusicKeyBoardValueChangedHandle(object sender, MusicKeyBoardValueChangedEventArgs e);
47 //**CurPlayProIndex变化事件
48 private MusicKeyBoardValueChangedHandle musicKeyBoardValueChanged;
49 /**//// <summary>
50 /// 发生变化事件
51 /// </summary>
52 public event MusicKeyBoardValueChangedHandle OnMusicKeyBoardValueChanged
53 {
54 add { musicKeyBoardValueChanged += value; }
55 remove { musicKeyBoardValueChanged -= value; }
56 }
57 protected virtual void MusicKeyBoardValueChanged(MusicKeyBoardValueChangedEventArgs e)
58 {
59 if (musicKeyBoardValueChanged != null)
60 musicKeyBoardValueChanged(this, e);
61 }
62 #endregion
63
64 private void ListResult_SelectedIndexChanged(object sender, EventArgs e)
65 {
66 string value = "";
67 if (ListResult.SelectedValue != null)
68 value = ListResult.SelectedValue.ToString().Trim();
69 else value = ListResult.Text;
70
71 KeyBoardSelectedType type;
72 if (radioButtonMusicName.Checked)
73 type = KeyBoardSelectedType.MusicName;
74 else
75 type = KeyBoardSelectedType.Songer;
76 MusicKeyBoardValueChangedEventArgs me = new MusicKeyBoardValueChangedEventArgs(value, type);
77 MusicKeyBoardValueChanged(me);
78 }
79
80 private void btn_Click(object sender, EventArgs e)
81 {
82 try
83 {
84 txtValue.Text += ((Button)sender).Text.Replace("&", "");
85 }
86 catch (Exception exc)
87 {
88
89 }
90 }
91
92
93 private void txtValue_TextChanged(object sender, EventArgs e)
94 {
95 BLL.Biz mybiz = new BLL.Biz();
96 ListResult.Items.Clear();
97 if (!string.IsNullOrEmpty(txtValue.Text.Trim()))
98 {
99 DataTable dt;
100 if (radioButtonMusicName.Checked)
101 {
102 dt = mybiz.getMusicByNameAppend(txtValue.Text.Trim());
103 foreach (DataRow dr in dt.Rows)
104 {
105 ListResult.Items.Add(dr["MusicName"].ToString().Trim());
106 }
107 }
108 else
109 {
110 dt = mybiz.getMusicBySongerAppend(txtValue.Text.Trim());
111 foreach (DataRow dr in dt.Rows)
112 {
113 ListResult.Items.Add(dr["MusicSonger"].ToString().Trim());
114 }
115 }
116 }
117 }
118
119 private void btn_Clear_Click(object sender, EventArgs e)
120 {
121 txtValue.Text = "";
122 }
123
124 private void radioButtonMusicName_CheckedChanged(object sender, EventArgs e)
125 {
126 clear();
127 }
128
129 private void radioButtonSonger_CheckedChanged(object sender, EventArgs e)
130 {
131 clear();
132 }
133 private void clear()
134 {
135 txtValue.Text = "";
136 ListResult.Items.Clear();
137 }
138
139 private void MusicKeyBoard_Load(object sender, EventArgs e)
140 {
141 HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, Keys.NumPad0);
142 HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.None, Keys.NumPad1);
143 HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.None, Keys.NumPad2);
144 HotKey.RegisterHotKey(Handle, 103, HotKey.KeyModifiers.None, Keys.NumPad3);
145 HotKey.RegisterHotKey(Handle, 104, HotKey.KeyModifiers.None, Keys.NumPad4);
146 HotKey.RegisterHotKey(Handle, 105, HotKey.KeyModifiers.None, Keys.NumPad5);
147 HotKey.RegisterHotKey(Handle, 106, HotKey.KeyModifiers.None, Keys.NumPad6);
148 HotKey.RegisterHotKey(Handle, 107, HotKey.KeyModifiers.None, Keys.NumPad7);
149 HotKey.RegisterHotKey(Handle, 108, HotKey.KeyModifiers.None, Keys.NumPad8);
150 HotKey.RegisterHotKey(Handle, 109, HotKey.KeyModifiers.None, Keys.NumPad9);
151
152 HotKey.RegisterHotKey(Handle, 110, HotKey.KeyModifiers.None, Keys.Delete);
153 HotKey.RegisterHotKey(Handle, 111, HotKey.KeyModifiers.None, Keys.Back);
154 }
155
156 private void MusicKeyBoard_Leave(object sender, EventArgs e)
157 {
158 HotKey.UnregisterHotKey(Handle, 100);
159 HotKey.UnregisterHotKey(Handle, 101);
160 HotKey.UnregisterHotKey(Handle, 102);
161 HotKey.UnregisterHotKey(Handle, 103);
162 HotKey.UnregisterHotKey(Handle, 104);
163 HotKey.UnregisterHotKey(Handle, 105);
164 HotKey.UnregisterHotKey(Handle, 106);
165 HotKey.UnregisterHotKey(Handle, 107);
166 HotKey.UnregisterHotKey(Handle, 108);
167 HotKey.UnregisterHotKey(Handle, 109);
168 HotKey.UnregisterHotKey(Handle, 110);
169 HotKey.UnregisterHotKey(Handle, 111);
170 }
171
172
173 /**////
174 /// 监视Windows消息
175 /// 重载WndProc方法,用于实现热键响应
176 ///
177 ///
178 protected override void WndProc(ref Message m)
179 {
180 const int WM_HOTKEY = 0x0312;
181 //按快捷键
182 switch (m.Msg)
183 {
184 case WM_HOTKEY:
185 switch (m.WParam.ToInt32())
186 {
187 case 100:
188 txtValue.Text += "0";
189 break;
190 case 101:
191 txtValue.Text += "1";
192 break;
193 case 102:
194 txtValue.Text += "2";
195 break;
196 case 103:
197 txtValue.Text += "3";
198 break;
199 case 104:
200 txtValue.Text += "4";
201 break;
202 case 105:
203 txtValue.Text += "5";
204 break;
205 case 106:
206 txtValue.Text += "6";
207 break;
208 case 107:
209 txtValue.Text += "7";
210 break;
211 case 108:
212 txtValue.Text += "8";
213 break;
214 case 109:
215 txtValue.Text += "9";
216 break;
217 case 110:
218 txtValue.Text = "";
219 break;
220 case 111:
221 if(txtValue.Text!="")
222 txtValue.Text =txtValue.Text.Substring(0,txtValue.Text.Length-1) ;
223 break;
224 }
225 break;
226 }
227 base.WndProc(ref m);
228 }
229 }
230
231 序号发生变化事件数据#region 序号发生变化事件数据
232 /**//// <summary>
233 /// 序号发生变化事件数据
234 /// </summary>
235 public class MusicKeyBoardValueChangedEventArgs : EventArgs
236 {
237 private string _SelectedValue;
238 /**//// <summary>
239 /// 新值
240 /// </summary>
241 public string SelectedValue
242 {
243 get { return _SelectedValue; }
244 set { _SelectedValue = value; }
245 }
246
247 private KeyBoardSelectedType _SelectedType;
248 /**//// <summary>
249 /// 类型
250 /// </summary>
251 public KeyBoardSelectedType SelectedType
252 {
253 get { return _SelectedType; }
254 set { _SelectedType = value; }
255 }
256
257 public MusicKeyBoardValueChangedEventArgs(string selectedValue, KeyBoardSelectedType selectedType)
258 {
259 _SelectedValue = selectedValue;
260 _SelectedType = selectedType;
261 }
262 }
263 /**//// <summary>
264 /// 软键盘选择类型
265 /// </summary>
266 public enum KeyBoardSelectedType
267 {
268 /**//// <summary>
269 /// 名称
270 /// </summary>
271 MusicName,
272 /**//// <summary>
273 /// 歌手
274 /// </summary>
275 Songer
276 }
277 #endregion
278}
279
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace pcd.WinControl
10{
11
12 public partial class MusicKeyBoard : UserControl
13 {
14 public MusicKeyBoard()
15 {
16 InitializeComponent();
17 }
18 private bool _IsActive;
19 private string _ResultValue;
20 Properties#region Properties
21 [Category("Appearance"),
22 DefaultValue(typeof(bool), "false"),
23 Description("当前控件是否激活")]
24 public bool IsActive
25 {
26 get { return _IsActive; }
27 set { _IsActive = value; }
28 }
29
30 [Category("Appearance"),
31 Description("起始数字")]
32 public string ResultValue
33 {
34 get
35 {
36 if (ListResult.SelectedValue != null)
37 return ListResult.SelectedValue.ToString();
38 else return "";
39 }
40 set { _ResultValue = value; }
41 }
42
43 #endregion
44
45 MusicKeyBoardValueChangedHandle#region MusicKeyBoardValueChangedHandle
46 public delegate void MusicKeyBoardValueChangedHandle(object sender, MusicKeyBoardValueChangedEventArgs e);
47 //**CurPlayProIndex变化事件
48 private MusicKeyBoardValueChangedHandle musicKeyBoardValueChanged;
49 /**//// <summary>
50 /// 发生变化事件
51 /// </summary>
52 public event MusicKeyBoardValueChangedHandle OnMusicKeyBoardValueChanged
53 {
54 add { musicKeyBoardValueChanged += value; }
55 remove { musicKeyBoardValueChanged -= value; }
56 }
57 protected virtual void MusicKeyBoardValueChanged(MusicKeyBoardValueChangedEventArgs e)
58 {
59 if (musicKeyBoardValueChanged != null)
60 musicKeyBoardValueChanged(this, e);
61 }
62 #endregion
63
64 private void ListResult_SelectedIndexChanged(object sender, EventArgs e)
65 {
66 string value = "";
67 if (ListResult.SelectedValue != null)
68 value = ListResult.SelectedValue.ToString().Trim();
69 else value = ListResult.Text;
70
71 KeyBoardSelectedType type;
72 if (radioButtonMusicName.Checked)
73 type = KeyBoardSelectedType.MusicName;
74 else
75 type = KeyBoardSelectedType.Songer;
76 MusicKeyBoardValueChangedEventArgs me = new MusicKeyBoardValueChangedEventArgs(value, type);
77 MusicKeyBoardValueChanged(me);
78 }
79
80 private void btn_Click(object sender, EventArgs e)
81 {
82 try
83 {
84 txtValue.Text += ((Button)sender).Text.Replace("&", "");
85 }
86 catch (Exception exc)
87 {
88
89 }
90 }
91
92
93 private void txtValue_TextChanged(object sender, EventArgs e)
94 {
95 BLL.Biz mybiz = new BLL.Biz();
96 ListResult.Items.Clear();
97 if (!string.IsNullOrEmpty(txtValue.Text.Trim()))
98 {
99 DataTable dt;
100 if (radioButtonMusicName.Checked)
101 {
102 dt = mybiz.getMusicByNameAppend(txtValue.Text.Trim());
103 foreach (DataRow dr in dt.Rows)
104 {
105 ListResult.Items.Add(dr["MusicName"].ToString().Trim());
106 }
107 }
108 else
109 {
110 dt = mybiz.getMusicBySongerAppend(txtValue.Text.Trim());
111 foreach (DataRow dr in dt.Rows)
112 {
113 ListResult.Items.Add(dr["MusicSonger"].ToString().Trim());
114 }
115 }
116 }
117 }
118
119 private void btn_Clear_Click(object sender, EventArgs e)
120 {
121 txtValue.Text = "";
122 }
123
124 private void radioButtonMusicName_CheckedChanged(object sender, EventArgs e)
125 {
126 clear();
127 }
128
129 private void radioButtonSonger_CheckedChanged(object sender, EventArgs e)
130 {
131 clear();
132 }
133 private void clear()
134 {
135 txtValue.Text = "";
136 ListResult.Items.Clear();
137 }
138
139 private void MusicKeyBoard_Load(object sender, EventArgs e)
140 {
141 HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, Keys.NumPad0);
142 HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.None, Keys.NumPad1);
143 HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.None, Keys.NumPad2);
144 HotKey.RegisterHotKey(Handle, 103, HotKey.KeyModifiers.None, Keys.NumPad3);
145 HotKey.RegisterHotKey(Handle, 104, HotKey.KeyModifiers.None, Keys.NumPad4);
146 HotKey.RegisterHotKey(Handle, 105, HotKey.KeyModifiers.None, Keys.NumPad5);
147 HotKey.RegisterHotKey(Handle, 106, HotKey.KeyModifiers.None, Keys.NumPad6);
148 HotKey.RegisterHotKey(Handle, 107, HotKey.KeyModifiers.None, Keys.NumPad7);
149 HotKey.RegisterHotKey(Handle, 108, HotKey.KeyModifiers.None, Keys.NumPad8);
150 HotKey.RegisterHotKey(Handle, 109, HotKey.KeyModifiers.None, Keys.NumPad9);
151
152 HotKey.RegisterHotKey(Handle, 110, HotKey.KeyModifiers.None, Keys.Delete);
153 HotKey.RegisterHotKey(Handle, 111, HotKey.KeyModifiers.None, Keys.Back);
154 }
155
156 private void MusicKeyBoard_Leave(object sender, EventArgs e)
157 {
158 HotKey.UnregisterHotKey(Handle, 100);
159 HotKey.UnregisterHotKey(Handle, 101);
160 HotKey.UnregisterHotKey(Handle, 102);
161 HotKey.UnregisterHotKey(Handle, 103);
162 HotKey.UnregisterHotKey(Handle, 104);
163 HotKey.UnregisterHotKey(Handle, 105);
164 HotKey.UnregisterHotKey(Handle, 106);
165 HotKey.UnregisterHotKey(Handle, 107);
166 HotKey.UnregisterHotKey(Handle, 108);
167 HotKey.UnregisterHotKey(Handle, 109);
168 HotKey.UnregisterHotKey(Handle, 110);
169 HotKey.UnregisterHotKey(Handle, 111);
170 }
171
172
173 /**////
174 /// 监视Windows消息
175 /// 重载WndProc方法,用于实现热键响应
176 ///
177 ///
178 protected override void WndProc(ref Message m)
179 {
180 const int WM_HOTKEY = 0x0312;
181 //按快捷键
182 switch (m.Msg)
183 {
184 case WM_HOTKEY:
185 switch (m.WParam.ToInt32())
186 {
187 case 100:
188 txtValue.Text += "0";
189 break;
190 case 101:
191 txtValue.Text += "1";
192 break;
193 case 102:
194 txtValue.Text += "2";
195 break;
196 case 103:
197 txtValue.Text += "3";
198 break;
199 case 104:
200 txtValue.Text += "4";
201 break;
202 case 105:
203 txtValue.Text += "5";
204 break;
205 case 106:
206 txtValue.Text += "6";
207 break;
208 case 107:
209 txtValue.Text += "7";
210 break;
211 case 108:
212 txtValue.Text += "8";
213 break;
214 case 109:
215 txtValue.Text += "9";
216 break;
217 case 110:
218 txtValue.Text = "";
219 break;
220 case 111:
221 if(txtValue.Text!="")
222 txtValue.Text =txtValue.Text.Substring(0,txtValue.Text.Length-1) ;
223 break;
224 }
225 break;
226 }
227 base.WndProc(ref m);
228 }
229 }
230
231 序号发生变化事件数据#region 序号发生变化事件数据
232 /**//// <summary>
233 /// 序号发生变化事件数据
234 /// </summary>
235 public class MusicKeyBoardValueChangedEventArgs : EventArgs
236 {
237 private string _SelectedValue;
238 /**//// <summary>
239 /// 新值
240 /// </summary>
241 public string SelectedValue
242 {
243 get { return _SelectedValue; }
244 set { _SelectedValue = value; }
245 }
246
247 private KeyBoardSelectedType _SelectedType;
248 /**//// <summary>
249 /// 类型
250 /// </summary>
251 public KeyBoardSelectedType SelectedType
252 {
253 get { return _SelectedType; }
254 set { _SelectedType = value; }
255 }
256
257 public MusicKeyBoardValueChangedEventArgs(string selectedValue, KeyBoardSelectedType selectedType)
258 {
259 _SelectedValue = selectedValue;
260 _SelectedType = selectedType;
261 }
262 }
263 /**//// <summary>
264 /// 软键盘选择类型
265 /// </summary>
266 public enum KeyBoardSelectedType
267 {
268 /**//// <summary>
269 /// 名称
270 /// </summary>
271 MusicName,
272 /**//// <summary>
273 /// 歌手
274 /// </summary>
275 Songer
276 }
277 #endregion
278}
279
2).fQuickSearch窗体中拖一个MusicKeyBoard控件,为控件加上
OnMusicKeyBoardValueChanged事件
Code
1 public Form6 myParent = null;
2 private void userControl11_OnMusicKeyBoardValueChanged(object sender, pcd.WinControl.MusicKeyBoardValueChangedEventArgs e)
3 {
4 try
5 {
6 if (myParent != null)
7 {
8 myParent.KeyBoardValue = e.SelectedValue;
9 myParent.KeyBoardType = e.SelectedType.ToString();
10 myParent.ReLoadSearchList();
11 }
12 }
13 catch (Exception exc)
14 {
15 MessageBox.Show(exc.Message);
16 }
17 }
1 public Form6 myParent = null;
2 private void userControl11_OnMusicKeyBoardValueChanged(object sender, pcd.WinControl.MusicKeyBoardValueChangedEventArgs e)
3 {
4 try
5 {
6 if (myParent != null)
7 {
8 myParent.KeyBoardValue = e.SelectedValue;
9 myParent.KeyBoardType = e.SelectedType.ToString();
10 myParent.ReLoadSearchList();
11 }
12 }
13 catch (Exception exc)
14 {
15 MessageBox.Show(exc.Message);
16 }
17 }
3).Form6.cs:包含textBox1 、textBox2、 button1
Code
1 public string KeyBoardValue = "";
2 public string KeyBoardType = "";
3 private void button1_Click(object sender, EventArgs e)
4 {
5 try
6 {
7 fQuickSearch demo = new fQuickSearch();
8 demo.myParent = this;
9 demo.Show();
10 }
11 catch (Exception exc)
12 {
13 MessageBox.Show(exc.Message);
14 }
15 }
16
17 public bool ReLoadSearchList()
18 {
19 textBox1.Text = KeyBoardValue;
20 textBox2.Text = KeyBoardType;
21 return true;
22 }
23
1 public string KeyBoardValue = "";
2 public string KeyBoardType = "";
3 private void button1_Click(object sender, EventArgs e)
4 {
5 try
6 {
7 fQuickSearch demo = new fQuickSearch();
8 demo.myParent = this;
9 demo.Show();
10 }
11 catch (Exception exc)
12 {
13 MessageBox.Show(exc.Message);
14 }
15 }
16
17 public bool ReLoadSearchList()
18 {
19 textBox1.Text = KeyBoardValue;
20 textBox2.Text = KeyBoardType;
21 return true;
22 }
23