对Winform不大熟悉,
网上查了很多组合键的信息,都没找到,
都是说些怎么禁用的,还有的就是不能用,实现功能:
在一个多行文本框里头, 当用户按下 Ctrl+回车 的时候,
把光标跳转到其他文本框[TextBox2.Focus()],
还有就是要使用这个组合键是不是还要设置些什么???还希望能留下些建议!!!
网上查了很多组合键的信息,都没找到,
都是说些怎么禁用的,还有的就是不能用,实现功能:
在一个多行文本框里头, 当用户按下 Ctrl+回车 的时候,
把光标跳转到其他文本框[TextBox2.Focus()],
还有就是要使用这个组合键是不是还要设置些什么???还希望能留下些建议!!!
{
if (e.Control && e.KeyCode == Keys.Enter )
{
TextBox2.Focus();
}
}
给菜单名称上要设快捷键的按钮名称上加个&就行了,例如:
button按钮的Text为"确定(&O)",定义的快捷键就是"Alt+O",
public static extern bool RegisterHotKey(IntPtr hWnd,int id,uint fsModifiers, Keys vk);
[DllImport("user32.dll")] //快捷键卸载函数
public static extern bool UnregisterHotKey(IntPtr hWnd,int id);
{
public Form1()
{
InitializeComponent();
RegisterHotKey(Handle, 100, (int)KeyModifiers.Control, Keys.Left);// 热键一:Control +光标左箭头
RegisterHotKey(Handle, 200, 2, Keys.Right);//热键一:Control +光标右箭头
RegisterHotKey(Handle, 300, 2, Keys.Up);// 热键一:Control +光标上箭头
RegisterHotKey(Handle, 400, 2, Keys.Down);// 热键一:Control +光标下箭头
RegisterHotKey(Handle, 500, 2, Keys.V);
ContextMenu x = new ContextMenu();
this.comboBox2.ContextMenu = x;
} protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
break;
} base.WndProc(ref m);
} private void ProcessHotkey(Message m)
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
//DecreseVolumnb();
MessageBox.Show("You clicked the left key");
break;// 按下Control +光标左箭头,调用函数DecreseVolumnb();
case "200":
//AddVolumnb();
MessageBox.Show("You clicked the right key");
break;// 按下Control +光标右箭头,调用函数AddVolumnb()
case "300":// 按下Control +光标上箭头,显示窗体
this.Visible = true;
MessageBox.Show("You clicked the up key");
break;
case "400":// 按下Control +光标下箭头,隐藏窗体
MessageBox.Show("You clicked the down key");
this.Visible = false;
break;
case "500":
this.Visible = true;
break;
}
} void SetFocus()
{
if (this.comboBox2.Focus())
{
//this.comboBox2.Text=Handle.
}
} [DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk); [DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id); private void button1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
MessageBox.Show(btn.Text);
//PrintService service = new PrintService();
//Stream stream = new FileStream(Application.StartupPath + "//1.jpg",FileMode.Open);
//service.StartPrint(stream, "image");
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
map1.PrintMap(ev.Graphics.GetHdc().ToInt32(), 0, 0, map1.Width * 100, map1.Height * 100);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 100);//卸载第1个快捷键
UnregisterHotKey(Handle, 200); //缷载第2个快捷键
UnregisterHotKey(Handle, 300);//卸载第3个快捷键
UnregisterHotKey(Handle, 400); //缷载第4个快捷键
UnregisterHotKey(Handle, 500);
}
} public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
用热键了
一楼说注册KeyUp事件,'
我有些不明白,为什么要用KeyUp,
而且我用了也出现问题,要执行两次的组合键才行,
也就是说得按两次的Ctrl+回车,光标才能转到TextBox2上,
我没用KeyUp,我用了KeyDown事件,就可以了.
// Ctrl + H
if ((Control.ModifierKeys & Keys.Control) != 0 && e.KeyCode == Keys.H)
{
MessageBox.Show("Ctrl + H");
} // Alt + H
if ((Control.ModifierKeys & Keys.Alt) != 0 && e.KeyCode == Keys.H)
{
MessageBox.Show("Alt + H");
} // Shift + H
if ((Control.ModifierKeys & Keys.Shift) != 0 && e.KeyCode == Keys.H)
{
MessageBox.Show("Shift + H");
} // Ctrl + Alt + Shift + H
if ((Control.ModifierKeys & Keys.Control) != 0 &&
(Control.ModifierKeys & Keys.Alt) != 0 &&
(Control.ModifierKeys & Keys.Shift) != 0 &&
e.KeyCode == Keys.H)
{
MessageBox.Show("Ctrl + Alt + Shift + H");
}
但是还有一点问题,就是
当我按住组合键的时候,即Ctrl+回车,
它先执行的是回车键,就是说多行文本框里面换行了,
然后才执行组合键触发的方法. 我把组合键的回车键换成其它的健(如:c)倒是没出现这种情况.
但是我现在是做一个物流录入资料的系统,当然是Ctrl+Enter来得更加方便.
还请指点....
{
if (e.Control && e.KeyCode == Keys.Enter )
{
TextBox2.Focus();
}
}
顶一楼的代码
那个API函数似乎处理不好对话框模式的窗口。
{
if ( e.Control && e.KeyCode == Keys.A ) // Ctrl+A
{
处理
}
if ( e.Control && e.KeyCode == Keys.N ) // Ctrl+N
{
处理
}
}
.
人力资源
{
if (e.Control && e.KeyCode == Keys.Enter )
{
TextBox2.Focus();
}
}这样就可以了
刚找了些资料
if (e.KeyCode == Keys.Enter && e.Control)
{
e.SuppressKeyPress = true;
button_send.PerformClick();
}发觉这样弄了文本框就不会换行了