您的位置:首页 > 其它

Win32 SDK中设置Hotkey

2013-11-15 16:25 363 查看
MSDN中的一个示例代码,步骤就是RegisterHotKey注册热键,然后响应WM_HOTKEY消息

@1:这个是系统热键

#include "stdafx.h"

int _cdecl _tmain (
int argc,
TCHAR *argv[])
{
if (RegisterHotKey(
NULL,
1,
MOD_ALT | MOD_NOREPEAT,
0x42))  //0x42 is 'b'
{
_tprintf(_T("Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"));
}

MSG msg = {0};
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
if (msg.message == WM_HOTKEY)
{
_tprintf(_T("WM_HOTKEY received\n"));
}
}

return 0;
}

@2:软件非全局的快捷键,Keyboard Accelerators(键盘加速器)

FontAccel ACCELERATORS
{
VK_F5, IDM_REGULAR, VIRTKEY
"B", IDM_BOLD, CONTROL, VIRTKEY
"I", IDM_ITALIC, CONTROL, VIRTKEY
"U", IDM_ULINE, CONTROL, VIRTKEY
}
HWND hwndMain;      // handle to main window
HANDLE hinstAcc;    // handle to application instance
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;            // application messages
BOOL bRet;          // for return value of GetMessage
HACCEL haccel;      // handle to accelerator table

// Perform the initialization procedure.

// Create a main window for this application instance.

hwndMain = CreateWindowEx(0L, "MainWindowClass",
"Sample Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
hinst, NULL );

// If a window cannot be created, return "failure."

if (!hwndMain)
return FALSE;

// Make the window visible and update its client area.

ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);

// Load the accelerator table.
// haccel = LoadAccelerators(hinstAcc,  MAKEINTRESOURCE(IDC_*)) ;IDC_*为.RC资源中的加速器ID 
haccel = LoadAccelerators(hinstAcc, "FontAccel");
if (haccel == NULL)
HandleAccelErr(ERR_LOADING);     // application defined

// Get and dispatch messages until a WM_QUIT message is
// received.

while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
// Check for accelerator keystrokes.

if (!TranslateAccelerator(
hwndMain,  // handle to receiving window
haccel,    // handle to active accelerator table
&msg))         // message data
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return msg.wParam;
}

LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BYTE fbFontAttrib;        // array of font-attribute flags
static HMENU hmenu;       // handle to main menu

switch (uMsg)
{
case WM_CREATE:

// Add a check mark to the Regular menu item to
// indicate that it is the default.

hmenu = GetMenu(hwndMain);
CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND |
MF_CHECKED);
return 0;

case WM_COMMAND:
switch (LOWORD(wParam))
{
// Process the accelerator and menu commands.

case IDM_REGULAR:
case IDM_BOLD:
case IDM_ITALIC:
case IDM_ULINE:

// GetFontAttributes is an application-defined
// function that sets the menu-item check marks
// and returns the user-selected font attributes.

fbFontAttrib = GetFontAttributes(
(BYTE) LOWORD(wParam), hmenu);

// SetFontAttributes is an application-defined
// function that creates a font with the
// user-specified attributes the font with
// the main window's device context.

SetFontAttributes(fbFontAttrib);
break;

default:
break;
}
break;

// Process other messages.

default:
return DefWindowProc(hwndMain, uMsg, wParam, lParam);
}
return NULL;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: