在C#里面如何来自定义鼠标的样式,以及定义全局快捷键来实现截图。
http://www.cnblogs.com/1971ruru/archive/2010/03/19/screencap2.html
http://www.cnblogs.com/1971ruru/archive/2010/05/20/1740216.html
1、自定义鼠标的样式:假如你只用C#里面的Managed Code,自定义出的鼠标结果显示出来的会变成单色。
我们需要user32.dll里面的几个API来实现自定义彩色鼠标样式。
[DllImport( "user32.dll" )] static extern IntPtr LoadCursorFromFile( string lpFileName); [DllImport( "user32.dll" )] public static extern uint DestroyCursor( IntPtr cursorHandle ); |
这两个都比较简单,我们可以在form_load时加载鼠标句柄,记得最后要在form_closing时释放句柄资源~
this.Cursor = new Cursor( IntPtr handle )用来实例化鼠标。
2、全局快捷键或称热键,则需要调用user32里面的另外两个API来实现。
[DllImport( "user32.dll" , SetLastError = true )] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options System.Windows.Forms.Keys vk // virtual-key code ); [DllImport( "user32.dll" , SetLastError = true )] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); [Flags] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } |
比如注册F3键为截图快捷键:RegisterHotKey( this.Handle, 7890, IceApi.KeyModifiers.None, Keys.F3 );
这两个API分别要放到Form_Load和Form_FormClosed事件里面。
最后,我们需要截获系统消息来为我们定义的快捷键执行相应操作。此时我们需要重写WndProc

switch (m.Msg) {
//hotkey pressed
case 0x0312:
if (m.WParam.ToString() == "7890") {
GlobalKeyProc( this.WindowState == FormWindowState.Minimized );
}
break;
}
base.WndProc( ref m );
}
在GlobalKeyProc()里面处理我们需要的工作。