重寫Wndproc示例:
static bool flag = false;
protected override void WndProc(ref Message m)
{
switch (m.Msg) {
case 0x0200: //WM_MOUSEMOVE
if (flag)
{
PostMessage(this.Handle, 0x00A1,new IntPtr(2),m.LParam);//這裡需要導入 user32.dll
}
break; www.2cto.com
case 0x201://WM_LBUTTONDOWN
flag = true;
break;
case 0x202://WM_LBUTTONUP
flag = false;
break;
default:
base.WndProc(ref m);
break;
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
虛擬鍵盤代碼 16進制值 對應鍵
VK_LBUTTON 01 鼠標左鍵
VK_RBUTTON 02 鼠標右鍵
VK_CANCEL 03 用於執行Ctrl+C或Ctrl+Break
VK_MBUTTON 04 鼠標中鍵
VK_BACK 08 Backspace鍵
VK_TAB 09 Tab鍵
VK_CLEAR 0C Clear鍵
VK_RETURN 0D Enter鍵
VK_SHIFT 10 Shift鍵
VK_CONTROL 11 Ctrl鍵
VK_MENU 12 Arl鍵
VK_PAUSE 13 Pause鍵
VK_CAPITAL 14 Capslock鍵
VK_ESCAPE 1B Ese鍵
VK_SPACE 20 SpaceBar鍵
VK_PRIOR 21 PgUp鍵
VK_NEXT 22 PgDn鍵
VK_END 23 End鍵
VK_HOME 24 Home鍵
VK_LEFT 25 Left Arrow鍵
VK_UP 26 Up Arrow鍵
VK_RIGHT 27 Right Arrow鍵
VK_DOWN 28 Down Arrow鍵
VK_SELECT 29 Select鍵
VK_EXECUTE 2B Execute鍵
VK_SNAPSHOT 2C PrintScreen鍵
VK_INSERT 2D Ins鍵
VK_DELECT 2E Del鍵
VK_HELP 2F Help鍵
VK_0 30 0鍵
… … …
VK_9 39 9鍵
VK_A 41 A鍵
… … …
VK_Z 5A Z鍵
VK_NUMAPD0 60 數字板0鍵
… … …
VK_NUMAPD9 69 數字板9鍵
VK_MULTIPY 6A 乘號鍵
VK_ADD 6B 加號鍵
VK_SEPARATOR 6C Separator鍵
VK_SUBSTRACT 6D 減號鍵
VK_DECIMAL 6E 小數點鍵
VK_DIVIDE 6F 除號鍵
VK_F1 70 F1鍵
… … …
VK_F24 87 F24鍵
VK_NUMLOCK 90 NumLock鍵
VK_SCROLL 91 ScrollLock鍵
VK_CAPITAL Caps Lock
VK_ESCAPE Esc
VK_SPACE Space
VK_PRIOR Page Up
VK_NEXT Page Down
VK_END End
VK_HOME Home
VK_LEFT 向左方向鍵
VK_UP
VK_RIGHT
VK_DOWN
VK_DELETE Delete
VK_INSERT Inser
VK_NUMPAD0~VK_NUMPAD9 小鍵盤上的0-9鍵
VK_F1-VK_f12 F1-F12鍵
檢測Caps Lock 鍵:
var
KeyStates:TKeyboardState;
begin
GetKeyboardState(KeyStates);
if Odd(KeyStates[VK_CAPITAL]) then
spBtnA.Caption := 'a'
else
spBtnA.Caption := 'A';
end;