我想用加速键在我对话框上实现按下CTRL+G就执行一个函数,我的做法如下:(1)在资源中Accelerator加入ID:
ID KEY TYPE
ID_MY_ACCELERATOR CTRL+G VIRTKEY(2)在mydlg.h中的public中声明:
HACCEL hAccel; (3)在mydlg.cpp的OnInitDialog()中初始化:
hAccel = LoadAccelerators(((CMyApp*)AfxGetApp())->m_hInstance,MAKEINTRESOURCE(ID_MY_ACCELERATOR));(4)在PreTranslateMessage函数中重定义
BOOL CORNOTDlg::PreTranslateMessage(MSG* pMsg)
{
if (TranslateAccelerator(GetSafeHwnd(),hAccel,pMsg))
return TRUE;
else
return CDialog::PreTranslateMessage(pMsg);
}
首先在第三步初始化出错,得到的hAccel值为0x00000000;
第二然后怎样跟我在dlg中写的函数displaytime()关联起来?让我在按下CTRL+G后能执行这个函数?
ID KEY TYPE
ID_MY_ACCELERATOR CTRL+G VIRTKEY(2)在mydlg.h中的public中声明:
HACCEL hAccel; (3)在mydlg.cpp的OnInitDialog()中初始化:
hAccel = LoadAccelerators(((CMyApp*)AfxGetApp())->m_hInstance,MAKEINTRESOURCE(ID_MY_ACCELERATOR));(4)在PreTranslateMessage函数中重定义
BOOL CORNOTDlg::PreTranslateMessage(MSG* pMsg)
{
if (TranslateAccelerator(GetSafeHwnd(),hAccel,pMsg))
return TRUE;
else
return CDialog::PreTranslateMessage(pMsg);
}
首先在第三步初始化出错,得到的hAccel值为0x00000000;
第二然后怎样跟我在dlg中写的函数displaytime()关联起来?让我在按下CTRL+G后能执行这个函数?
解决方案 »
- 一个Button 如何区分BN_CLICKED与WM_LBUTTONDOWN\WM_LBUTTONUP事件
- 菜单项前显示图标(有黑影)???如何解决 高手指点~~~
- 求助 evc下使用sqlite实现查询?
- 客户端的word或者excel文档,若用COM方式,版本问题如何解决呢?
- CSocket发送消息写在线程里为什么收不到数据?(help me )
- 怎么 %SystemRoot% 转换成系统的目录,有没有这样的API
- 大家教教我msdn怎么用啊~
- 请问如何强行中止一个线程?
- 奇怪的问题!
- 请问如何用VC6制作静态链接库(高分求教)
- 长了5只角了,继续提问,继续散分
- 请问怎样创建IShellBrowser接口的实例???
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk );
m_hHotKey = LoadAccelerators( AfxGetInstanceHandle(), "MY_ACCELERATOR" ); 在PreTranslateMessage(MSG* pMsg) 中
{
// TODO: Add your specialized code here and/or call the base class
if( m_hHotKey != NULL && TranslateAccelerator( m_hWnd, m_hHotKey, pMsg ) )
{
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}
The example in this section shows how to perform the following tasks: Create an accelerator-table resource.
Load the accelerator table at run time.
Translate accelerators in a message loop.
Process WM_COMMAND messages generated by the accelerators.
These tasks are demonstrated in the context of an application that includes a Character menu and corresponding accelerators that allow the user to select attributes of the current font. The following portion of a resource-definition file defines the Character menu and the associated accelerator table. Note that the menu items show the accelerator keystrokes and that each accelerator has the same identifier as its associated menu item. #include <windows.h>
#include "acc.h"
MainMenu MENU
{
POPUP "&Character"
{
MENUITEM "&Regular\tF5", IDM_REGULAR
MENUITEM "&Bold\tCtrl+B", IDM_BOLD
MENUITEM "&Italic\tCtrl+I", IDM_ITALIC
MENUITEM "&Underline\tCtrl+U", IDM_ULINE
}
}
FontAccel ACCELERATORS
{
VK_F5, IDM_REGULAR, VIRTKEY
"B", IDM_BOLD, CONTROL, VIRTKEY
"I", IDM_ITALIC, CONTROL, VIRTKEY
"U", IDM_ULINE, CONTROL, VIRTKEY
} The following sections from the application's source file show how to implement the accelerators. 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, "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 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 s
// 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;
}
m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));
在PreTranslateMessage(MSG* pMsg)中
{
if(m_hAccelTable)
{
if(::TranslateAccelerator(m_hWnd,m_hAccelTable,pMsg))
return true;
} return CDialog::PreTranslateMessage(pMsg);
}
用块速键实现CTRL+G执行自定义函数displaytime()
,这样只需要把菜单的Caption设定为:显示时间(&G)\tCtrl+G
就可以实现你的目的了。
对于怎么添加菜单的处理函数,很简单我就不说了。上面是我的一点建议,希望能对你有所帮助。