Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using an Accelerator 1

Status
Not open for further replies.

timmay3141

Programmer
Dec 3, 2002
468
US
I posted this in the Visual C++ forum, but I got no replies and it probably belongs here. I've used accelerators in MFC, but I'm new to the Windows API without using MFC. I'm trying to get Ctrl+N to do the same as the menu selection File, New. Here is the message handler used when File, New is pressed (this function is called when WM_COMMAND is sent):

void OnCommand(WPARAM wParam)
{
switch(wParam)
{
case IDM_FILE_NEW:
MessageBox(NULL, "Recieved Message", "adsfa", MB_OK);
}
}

This works fine when I use the menu, and I see a message box as I should. However, when I press Ctrl+N, it doesn't work. I've figured out that the OnCommand function is being called, but not being sent IDM_FILE_NEW as it should be. Here is what I added to the accelerator table:

IDM_FILE_NEW Ctrl + N VIRTKEY

Then, I added this to WinMain:

HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_MYACCELERATOR));

I also added this to the message pump (before handling and dispatching the message):

if(TranslateAccelerator(hWnd, hAccelTable, &msg)) continue;

As far as I can figure, this should be sending the WM_COMMAND message, which should then handle it like I pressed File, New. As I said earlier, the OnCommand function is called, but the wParam in the function doesn't equal IDM_FILE_NEW. Since I'm new to Win API, my problem could be something really stupid. Any ideas?
 
Change switch(wParam) to switch(LOWORD(wParam)).

TranslateAccelerator makes the hi-order word of wParam 1, so you can distinguish between the message being send by an ordinary invocation (i.e. mousclick on the menu or something) or the use of an accelerator.


Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top