焦点在PictureBox1,使用Enter触发,去掉ActiveControl.Name == "PictureBox1"使用Enter就触发
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(ActiveControl.Name == "PictureBox1" && keyData == Keys.Enter)
{
//操作代码
} return base.ProcessCmdKey (ref msg, keyData);
}
解决方案 »
- WinFrom上的DataGridView,怎样做才能让user 感觉不刷新?
- c#程序中,如何实现:将数据库里的字段提取出来,去google上搜索,得到网页上“……相关项”,将这个“相关项个数”信息提出来,再存入数据库中
- 虚心请教,Winform的列表显示问题。
- 菜鸟求助!
- 有关 .NET Remoting 中 对象的生存期问题
- 关于部署的问题
- 苏州的程序员注意了!
- 新手提问一个关于搜索的问题
- Environment.Exit(0)软件退出时调用此函数出问题。
- 急!!老板让把一个在ucdos 5.0下的软件重新修改到windows下,现在需要把该软件反汇编一下,哪位能提示一二,谢谢!!
- 最后3分了,如何在html或aspx中调用windows应用程序模式的dll文件
- 求书《C#XML入门经典——C#编程人员必备的XML技能 》
{
this.KeyPreview = true;
}private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.S)
{
pictureBox1ClickEvent();
}
}void pictureBox1ClickEvent()
{
MessageBox.Show("test");
}private void pictureBox1_Click(object sender, System.EventArgs e)
{
pictureBox1ClickEvent();
}大概这样.
老大,理论上这么说,给点代码意思意思呀
首先,创建一个WinHotKey类,如下
public class WinHotKey
{
[DllImport("user32.dll",SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
int id,
KeyModifiers fsModifiers,
Keys vk
); [DllImport("user32.dll",SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control =2,
Shift = 4,
Windows = 8
} public WinHotKey()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}然后,在程序中这样调用
//快捷键定义
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other; public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
modifier |= WinHotKey.KeyModifiers.Control;
if(this.key_Alt )
modifier |= WinHotKey.KeyModifiers.Alt;
if(this.key_Shift)
modifier |= WinHotKey.KeyModifiers.Shift;
if(this.key_Windows)
modifier |= WinHotKey.KeyModifiers.Windows;
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
}
catch
{
//login.ShowMessage("快捷键定义错误!");
}
} //激活热键
protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息,弹出消息
if( ReceiveNewMessage == true)
{
for(int i=0;i<this.manInforList.Count;i++)
{
ManInfor searchMan = (ManInfor)this.manInforList[i];
if( searchMan.manInforID.Equals( getFriendID))
{
searchMan.Clicked = true;
searchMan.P2PShow();
break;
}
}
}
else
{
this.Show();
this.TopMost = true;
this.panel_Main.Refresh();
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
}
break;
}
base.WndProc(ref m );
}
----------------------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace HotKey
{
public class Form1 : System.Windows.Forms.Form
{
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.HighlightText;
this.textBox1.Location = new System.Drawing.Point(8, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(272, 144);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 1;
this.label1.Text = "Error Message:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 191);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new Form1());
} public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
modifier |= WinHotKey.KeyModifiers.Control;
if(this.key_Alt )
modifier |= WinHotKey.KeyModifiers.Alt;
if(this.key_Shift)
modifier |= WinHotKey.KeyModifiers.Shift;
if(this.key_Windows)
modifier |= WinHotKey.KeyModifiers.Windows;
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
}
catch(Exception err)
{
this.textBox1.AppendText(err.Message + "\r\n");
}
} protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息
this.textBox1.AppendText(m.Msg.ToString() + "\r\n");
break;
}
}
base.WndProc(ref m );
} private void Form1_Load(object sender, System.EventArgs e)
{
this.SetHotKey(true, false, false, false, Keys.F10);
}
}
#region WinHotKey Class
public class WinHotKey
{
[DllImport("user32.dll",SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
int id,
KeyModifiers fsModifiers,
Keys vk
); [DllImport("user32.dll",SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control =2,
Shift = 4,
Windows = 8
} public WinHotKey(){}
}
#endregion
}
this.SetHotKey(true, false, false, false, Keys.A);
this.SetHotKey(true, false, false, false, Keys.B);
但是只有一个起作用呢?还有就是,如果这样用的话不是把我所有的键盘都屏蔽了吗?那我还要输入其他东西怎么办?我只是像增加多几个键盘事件罢了。