Win32 TreeView AfterSelection - winapi

quick question...
I am working with treeview in win32 (VC++).
I want to remove selection facility provided for treeview. Can anyone tell what window message is posted onAfterSelect Event of tree view.
TV also has checkboxes. So disabling mouse click isn't an option...
Thanks in advance...
-
Varun
More Info
I am stuck at another point. My win32 application is essentially a modeless dialog - using CreateDialog & ShowWindow. After getting TVN_SELCHANGING, when I am returning 1, it isn't working. I think the default wndproc is getting called before I bypass the windows message. What should I do now?

I had this problem and just reversed the selection once it had already taken place. If you're not responding to it, anyway, then there shouldn't be any side effects.
case WM_NOTIFY:
{
if(wParam == IDC_TREE_MC)
{
LPNMHDR lpnmh = (LPNMHDR) lParam;
TVHITTESTINFO ht = {0};
if ((lpnmh->code == NM_CLICK) && (lpnmh->idFrom == IDC_TREE_MC)) // For Treeview Check Box Check Event
{
DWORD dwpos = GetMessagePos();
ht.pt.x = GET_X_LPARAM(dwpos);
ht.pt.y = GET_Y_LPARAM(dwpos);
MapWindowPoints(HWND_DESKTOP, lpnmh->hwndFrom, &ht.pt, 1);
TreeView_HitTest(lpnmh->hwndFrom, &ht);
if(TVHT_ONITEMSTATEICON & ht.flags)
PostMessage(hDlg, UM_CHECKSTATECHANGE, (WPARAM)lpnmh->hwndFrom, (LPARAM)ht.hItem);
else
TreeView_SelectItem(lpnmh->hwndFrom, NULL);
}
else if ((lpnmh->code == TVN_SELCHANGED ) && (lpnmh->idFrom == IDC_TREE_MC))
TreeView_SelectNode(lpnmh->hwndFrom, NULL);
}
break;
}

to remove selection facility provided for treeview
Could you please clarify this?
Do you want to prevent user from changing selection?
If you really want to do it, insert WM_NOTIFY case handler in the parent window, check for NMTREEVIEW code member (lParam is a pointer to NMTREEVIEW).
If code is TVN_SELCHANGING return 1 if you want to prevent selection change.
Returning 0 will alow selection change.

int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hWndDialog = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, WndProc);
if (hWndDialog != NULL)
{
ShowWindow(hWndDialog, SW_SHOW);
}
while(GetMessage(&Msg, NULL, 0, 0))
{
if(!IsDialogMessage(hWndDialog, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return 0;
}
INT_PTR CALLBACK WndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_REFRESH:
RefreshButtonHandler();
break;
case IDC_BTN_ADD_INSTALL:
AddInstallBtnHandler();
break;
case IDOK:
case IDCANCEL:
DestroyWindow(hDlg);
PostQuitMessage(0);
return (INT_PTR)TRUE;
break;
}
case WM_NOTIFY:
{
if(wParam == IDC_TREE_MC)
{
LPNMHDR lpnmh = (LPNMHDR) lParam;
TVHITTESTINFO ht = {0};
if ((lpnmh->code == NM_CLICK) && (lpnmh->idFrom == IDC_TREE_MC)) // For Treeview Check Box Check Event
{
DWORD dwpos = GetMessagePos();
ht.pt.x = GET_X_LPARAM(dwpos);
ht.pt.y = GET_Y_LPARAM(dwpos);
MapWindowPoints(HWND_DESKTOP, lpnmh->hwndFrom, &ht.pt, 1);
TreeView_HitTest(lpnmh->hwndFrom, &ht);
if(TVHT_ONITEMSTATEICON & ht.flags)
PostMessage(hDlg, UM_CHECKSTATECHANGE, (WPARAM)lpnmh->hwndFrom, (LPARAM)ht.hItem);
else
TreeView_SelectItem(lpnmh->hwndFrom, NULL);
}
else if ((lpnmh->code == TVN_SELCHANGING ) && (lpnmh->idFrom == IDC_TREE_MC))
return (INT_PTR)TRUE;
}
break;
}
case UM_CHECKSTATECHANGE:
{
//Handle TreeView Check State Event
}
break;
}
return (INT_PTR)FALSE;
}
Sorry for the bad formatting... I am sleep deprived :-)

Related

how to call a function on button click

I want to create two buttons that perform separate functions.
When button_B1 is clicked, Function_B1() runs.
When button_B2 is clicked, Function_B2() runs.
How do I call a function on a button click?
When a Win32 button is clicked, it sends a BN_CLICKED notification to its parent window, where the message carries the button's HWND and ID. In the parent window's wndproc, you can catch the notification and call whatever function you want for whichever button is sending the notification.
case WM_COMMAND:
{
if ((HIWORD(wParam) == BN_CLICKED) && (lParam != 0))
{
switch (LOWORD(wParam))
{
case ID_BTN1:
Function_B1();
break;
case ID_BTN2:
Function_B2();
break;
}
}
break:
}
Or
case WM_COMMAND:
{
if ((HIWORD(wParam) == BN_CLICKED) && (lParam != 0))
{
HWND hwndBtn = (HWND) lParam;
if (hwndBtn == hwndBtn1)
Function_B1();
else if (hwndBtn == hwndBtn2)
Function_B2();
}
break:
}
Thank you, Remy.
I just had a problem with the switch statement:
case WM_COMMAND:
{
switch (wmId)
{
case 1:
Function_B1(hWnd, wParam, lParam);
break;
case 2:
Function_B2(hWnd, wParam, lParam);
break;
case 3:
Function_B3(hWnd, wParam, lParam);
break;

How to cancel item label editing in Tree-View control upon ESC keydown in WinAPI

I have a dialog box with a Tree-View control where the user can edit the item labels. I want the user to be able to cancel the label edit by pressing ESC key.
The problem is that pressing ESC closes the dialog window immediately.
I have tried getting the handle to the EditBox control by a TreeView_GetEditControl() call upon TVN_BEGINLABELEDIT message and subclassing it to trap the ESC key, but when I do that, typing in edit box becomes impossible.
What is the problem?
Here is the relevant code:
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam) {
switch(message) {
//...
case WM_NOTIFY:
{
LPNMHDR pNmHdr = (LPNMHDR)lParam;
switch(pNmHdr->code) {
case TVN_BEGINLABELEDIT:
{
HWND hwndTV = (HWND)GetWindowLongPtr(hWnd, GWLP_USERDATA); // stored handle to Tree-View ctl
HWND hWndEditBox = TreeView_GetEditControl(hwndTV);
// subclass edit box
TreeViewGlobals::g_wpOrigEditBoxProc =
(WNDPROC)SetWindowLongPtr(hWndEditBox,
GWLP_WNDPROC, (LONG_PTR)EditBoxCtl_SubclassProc);
break;
}
case TVN_ENDLABELEDIT:
{
SetWindowLongPtr(hWnd, DWLP_MSGRESULT, (LONG)TRUE); // accept edit
return TRUE;
}
default:
break;
}
}
default:
break;
}
return FALSE;
}
INT_PTR CALLBACK EditBoxCtl_SubclassProc(HWND hWndEditBox, UINT message,
WPARAM wParam, LPARAM lParam) {
switch(message) {
HANDLE_MSG(hWndEditBox, WM_GETDLGCODE, EditBoxCtl_OnGetDlgCode);
HANDLE_MSG(hWndEditBox, WM_KEYDOWN, EditBoxCtl_OnKey); // does not receive WM_KEYDOWN for ESC unless I handle WM_GETDLGCODE above
default:
break;
}
return CallWindowProc(TreeViewGlobals::g_wpOrigEditBoxProc,
hWndEditBox, message, wParam, lParam);
}
UINT EditBoxCtl_OnGetDlgCode(HWND hWndEditBox, LPMSG lpmsg) {
if(lpmsg) {
if(lpmsg->message == WM_KEYDOWN && lpmsg->wParam == VK_ESCAPE) {
return DLGC_WANTMESSAGE;
}
}
return 0;
}
void EditBoxCtl_OnKey(HWND hWndEditBox, UINT vk, BOOL fDown,
int cRepeat, UINT flags) {
switch(vk) {
case VK_ESCAPE:
Beep(4000, 150); // never beeps
break;
default:
break;
}
}
P.S. I noticed that when I remove WM_GETDLGCODE handler in EditBoxCtl_SubclassProc(), it becomes possible to type in the edit box again, but then I can't trap WM_KEYDOWN for ESC key from that procedure.
Below is the solution that I found. The trick seems to be calling the original control proc with WM_GETDLGCODE intercepted in subclass proc, storing the return value and then returning it with DLGC_WANTALLKEYS or DLGC_WANTMESSAGE flag set to prevent system from further processing the keystroke.
The upside to this approach is that pressing ESC cancels editing and reverts the item label to its original text, and pressing ENTER while editing no longer just closes the dialog(which was another problem) without any additional code to handle those cases.
Here is the code that works:
INT_PTR CALLBACK EditBoxCtl_SubclassProc(HWND hWndEditBox, UINT message,
WPARAM wParam, LPARAM lParam) {
switch(message) {
//HANDLE_MSG(hWndEditBox, WM_GETDLGCODE, EditBoxCtl_OnGetDlgCode); // can't use this: need wParam and lParam for CallWindowProc()
case WM_GETDLGCODE: {
INT_PTR ret = CallWindowProc(TreeViewGlobals::g_wpOrigEditBoxProc,
hWndEditBox, message, wParam, lParam);
MSG* lpmsg = (MSG*)lParam;
if(lpmsg) {
if(lpmsg->message == WM_KEYDOWN &&
(lpmsg->wParam == VK_ESCAPE || lpmsg->wParam == VK_RETURN) )
{
return ret | DLGC_WANTALLKEYS;
}
}
return ret;
}
default:
break;
}
return CallWindowProc(TreeViewGlobals::g_wpOrigEditBoxProc,
hWndEditBox, message, wParam, lParam);
}
The problem is that the modal dialog has its own message loop and its own translation with IsDialogMessage. Using the MFC I would say, just use PreTranslateMessage but this isn't available in plain WinApi. You don't have access to the internal message loop and the keyboard interface.
So the Escape key is handled inside the message loop. And causes a WM_COMMAND message with IDCANCEL to be sent. (See the MSDN specs about dialogs)
Maybe the easiest way is to interrcept the WM_COMMAND message sent to the dialog, check if who has the focus and if the inplace edit control has the focus you just set the focus back to the tree control and eat forget the IDCANCEL and don't close the dialog.
you need remember the tree-view hwnd when you receive TVN_BEGINLABELEDIT (in class member, associated with dialog) and zero it when you receive TVN_ENDLABELEDIT. when user press esc or enter in modal dialog box - you receive WM_COMMAND with IDCANCEL (on esc) or IDOK( on enter). you need check saved tree-view hwnd and if it not 0 - call TreeView_EndEditLabelNow
switch (uMsg)
{
case WM_INITDIALOG:
m_hwndTV = 0;
break;
case WM_NOTIFY:
switch (reinterpret_cast<NMHDR*>(lParam)->code)
{
case TVN_BEGINLABELEDIT:
m_hwndTV = reinterpret_cast<NMHDR*>(lParam)->hwndFrom;
return TRUE;
case TVN_ENDLABELEDIT:
m_hwndTV = 0;
//set the item's label to the edited text
SetWindowLongPtrW(hwndDlg, DWLP_MSGRESULT, TRUE);
return TRUE;
}
break;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
break;
case WM_COMMAND:
switch (wParam)
{
case IDCANCEL:
if (m_hwndTV)
{
TreeView_EndEditLabelNow(m_hwndTV, TRUE);
}
else
{
EndDialog(hwndDlg, IDCANCEL);
}
break;
case IDOK:
if (m_hwndTV)
{
TreeView_EndEditLabelNow(m_hwndTV, FALSE);
}
else
{
EndDialog(hwndDlg, IDOK);
}
break;
}
break;
}

MessageBox return without user input?

I am trying to implemente a close-save-file dialog into a Win32 project, but encouter a strange problem. Here is my solution.
Create a simple win32 project in Visual Studio.
Handle WM_COMMAND to create new window.
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
InitInstance(hInst, SW_SHOW);
//DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
windowCount--;
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
Handle WM_SYSCOMMNAD to show the save-before-quit messagebox.
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE) {
int ret = MessageBox(NULL, L"do you really want to close", L"question", MB_YESNO|MB_APPLMODAL);
if (ret == IDNO)
return 0;
closedCount++;
StringCchPrintf(buff, 256, L"hwnd %x user choose to close\n", hWnd);
OutputDebugString(buff);
}
return DefWindowProc(hWnd, message, wParam, lParam);
two variable closeCount and windowCount to ensure terminate after all windows are closed.
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0) || (closedCount != windowCount))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
windowCount++;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
Full code: http://pastebin.com/EVWWMz8L
There are two bugs in above code:
Create two windows and click close button for each window, then confirm to close for one window would close both windows. Which means, close MessageBox in one window would cause MessageBox in the other window return without any user input.
Create two windows and click close button for each window. Then use aero thumbnail to activate one messagebox and confirm to close, but the associated window would not close. I need to confirm both messagebox to close window.
How could this happen, what's wrong with my code?
1, Handler for WM_DESTROY is wrong, right handler should be:
case WM_DESTROY: {
StringCchPrintf(buff, 256, L"%x recieved Wm-DESTROY\n", hWnd);
OutputDebugString(buff);
closedCount++;
if (closedCount == windowCount) {
PostQuitMessage(0);
}
}
FULL code: http://pastebin.com/EU7cVKUb
Root cause:
PostQuitMessage terminate the MessageBox/DialogBox message loop, cause them missing and return immediate. In conclusion, DO NOT call PostQuitMessage until you really need to do this.
2, MessageBox/DialogBox should have owner window, otherwise user could use alt+tab/aero-thumbnail to select the box. So create the messagebox with hwnd as parent could solve problem 2.

infinite loop inside the getmessage (DispatchMessage(& msg ); is not working)

I am creating a button application using resource editor. After creating button I try to do like this-
m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this);
if (m_hwndPreview == NULL)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
MSG msg;
BOOL bRet;
while ( (bRet=GetMessage (& msg,0, 0,0)) != 0)
{// this msg always contains the data like -msg = {msg=0x0000c03e wp=0x0000000000000012//always 12 I don't know why ?? lp=0x0000000000000000}
if (bRet == -1)
{
bRet = HRESULT_FROM_WIN32(GetLastError());
MessageBox(NULL, L"Hurr i am the error",L"Error",MB_ICONERROR | MB_OK);
}
else if (!IsDialogMessage (m_hwndPreview, & msg))
{
TranslateMessage (&msg); //on debugging TranslateMessage = 0x000007feee615480 TranslateMessage
DispatchMessage(& msg ); //but show nothing when I put cursor on this method to know the value that means it's not called
MessageBox(NULL, L"there is no error in receiving before dispatch the message",L"Error",
MB_ICONERROR | MB_OK);//this messagebox repeats again and again after the call to DialogProc function and I am not able to come out of the loop and here I need to restart my PC
at some other place I define createdialog function like this-
//this function is called just on createDialogParam() function.after it the controls go to getmessage where everything loops.
BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam)
{ //this dialogProc function is declares Static some where in the code otherwise the createdialogparam will give error DLGPROC is invalid conversion
//this Umsg alays creates strange value like 48 and 32 etc.. and Wparam contains a very big value like 12335423526 (I mean big and strange) and lparam contains 0.
switch(Umsg)
{
case WM_INITDIALOG:
{
MessageBox(NULL, L"Inside the WM_INITDIALOG function",L"Error",
MB_ICONERROR | MB_OK);
return TRUE;
}
break;
case WM_CREATE:
{
/////////////////
MessageBox(NULL, L"Inside the WM_CREATE",L"Error",
MB_ICONERROR | MB_OK);
/////////////////////////////////
}
break;
case WM_COMMAND:
{ //here are my two buttons created by me which should show messagebox on click
int ctl = LOWORD(wParam);
int event = HIWORD(wParam);//I don't know why this event is in blue colour .. but its not the pqrt of problem right now.
if (ctl == IDC_PREVIOUS && event == BN_CLICKED )
{
MessageBox(m_hwndPreview,L"Button Clicked is next inside WM_COMMAND ",L"BTN WND",MB_ICONINFORMATION);
return 0;
}
if (ctl == IDC_NEXT && event == BN_CLICKED )
{
MessageBox(m_hwndPreview,L"Button Clicked is previous inside WM_COMMAND",L"BTN WND",MB_ICONINFORMATION);
return 0;
}
return FALSE;
}break;
case WM_DESTROY:
{
////////////////::
MessageBox(NULL, L"Inside the WM_DESTROY",L"Error",
MB_ICONERROR | MB_OK);
//////////////////
PostQuitMessage(0);
return 0;
}
break;
case WM_CLOSE:
{
MessageBox(NULL, L"Inside the WM_CLOSE",L"Error",
MB_ICONERROR | MB_OK);
DestroyWindow (m_hwndPreview);
return TRUE;
}
break;
}
MessageBox(NULL, L"outside the DefWindowProc function",L"Error",
MB_ICONERROR | MB_OK);
return 0;
}
The problem occurring is that when I debut it the control first go to CreateDialogParam and then it go to getmessage where the control don't come out of the loop causing restart problem. And I have no display of button and image at preview pane. What I expect if everything go fine is after debugging it should show picture on preview pane and I have 2 buttons "Next" and "Previous" but it show just a blank window (the buttons and photo I have already created using Resource editor... That's correct I am sure about that) .. but I don't know why I am not coming out getmessage function and dispatchmessage is not called (because I saw on debugging).
so now you can try to comment the getmessage part code that will probably out if the problem because you are creating button using IDD_MAINDIALOG and your createdialogparam directly calls your dailogproc function where you receive WM_COMMAND and that you handle by your code behind.
you should write
return true;
just after the DispatchMessage(msg);
and tehn debug it and inform me about t he result on debugging.

Select all text in edit contol by clicking Ctrl+A

How to select all text in edit control by pressing Ctrl+A?
I can catch Ctrl+A for parent window in WndProc.
But I don't know how to catch ctrl+a which are applied for edit control.
Also I tried to use accelerators, but again it applies only for parent window.
Thanks.
EDIT: 1-st the simplest method
This method Based on #phord's answers in this question:
win32 select all on edit ctrl (textbox)
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
if (msg.message == WM_KEYDOWN && msg.wParam == 'A' && GetKeyState(VK_CONTROL) < 0)
{
HWND hFocused = GetFocus();
wchar_t className[6];
GetClassName(hFocused, className, 6);
if (hFocused && !wcsicmp(className, L"edit"))
SendMessage(hFocused, EM_SETSEL, 0, -1);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
EDIT: 2-nd method
Need to use CreateAcceleratorTable + TranslateAccelerator functions:
//global variables:
enum {ID_CTRL_A = 1};
HACCEL accel;
//main procedure
ACCEL ctrl_a;
ctrl_a.cmd = ID_CTRL_A; // Hotkey ID
ctrl_a.fVirt = FCONTROL | FVIRTKEY;
ctrl_a.key = 0x41; //'A' key
accel = CreateAcceleratorTable(&ctrl_a, 1); //we have only one hotkey
//How GetMessage loop looks
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
if (!TranslateAccelerator(hWnd, accel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
//in WndProc we must add next cases
case WM_COMMAND:
{
if (LOWORD(wParam) == ID_CTRL_A && HIWORD(wParam) == 1)
{
//on which control there was pressed Ctrl+A
//there is no way of getting HWND through wParam and lParam
//so we get HWND which currently has focus.
HWND hFocused = GetFocus();
wchar_t className[6];
GetClassName(hFocused, className, 6);
if (hFocudsed && !wcsicmp(className, L"edit"))
SendMessage(hFocused, EM_SETSEL, 0, -1);
}
}
break;
case WM_DESTROY:
{
DestroyAcceleratorTable(accel);
PostQuitMessage(0);
}
break;
As you can see this is pretty simple.
No need to handle WM_KEYDOWN! I know that most examples here (and CodeProject and many other places) all say there is, but it does not cure the beep that results whenever a WM_CHAR arises that is not handled.
Instead, try this:
LRESULT CALLBACK Edit_Prc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
if(msg==WM_CHAR&&wParam==1){SendMessage(hwnd,EM_SETSEL,0,-1); return 1;}
else return CallWindowProc((void*)WPA,hwnd,msg,wParam,lParam);
}
Remember to subclass the EDIT control to this Edit_Prc() using WPA=SetWindowLong(...) where WPA is the window procedure address for CallWindowProc(...)
First change the WindowProc for the edit control:
if (!(pEditProc = (WNDPROC)SetWindowLong(hEdit, GWL_WNDPROC, (LONG)&EditProc)))
{
assert(false);
return false;
}
Then in the new window proc, process the ctrl+a:
LRESULT CALLBACK EditProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
if (msg == WM_KEYDOWN) {
if (GetKeyState(VK_CONTROL) & 0x8000 && wParam == 'A') {
SendMessage(hwnd, EM_SETSEL, 0, -1);
}
}
return CallWindowProc(pEditProc, hwnd, msg, wParam, lParam);
}
Good News!
It seems Edit Controls (not multiline) now support Ctrl + A natively on Win10.
My current Windows SDK version is 10.0.17763.0.
Only tested on simple GUI APPs created with pure Windows API.
MFC APPs should have the same result.
The test binary platform is x86, and OS is Win10 x64.
Noob proof version?
I have written my own version using an accelerator table aswell.
This cleans out the WinMain a bit, and I tried to make everything as n00b proof as possible (since I am one).
Also the enum is ommited, since it is not needed.
As stated I am only a beginner in using the winapi, so please by all means
correct me if I am wrong.
In "Resource.h" I define two ID's
One for the accelerator table we will be using,
and one for the selectall command we will be using.
Inside Resource.h:
#define IDR_ACCEL1 101
#define ID_SELECT_ALL 9003
Then inside of the resource file (in vs2017 this is PROJECTNAME.rc)
we define the accelerator table.
PROJECTNAME.rc:
IDR_ACCEL1 ACCELERATORS
{
0x41, ID_SELECT_ALL, VIRTKEY, CONTROL // ctrl-A
}
Description
0x41 is virtkey 'a'.
ID_SELECT_ALL (will be the ID of the command, this should be the ID we defined in the Resource.h file.
The VIRTKEY keyword indicated that the 0x41 should be interpreted as a virtual key.
CONTROL is the modifier needed to combine the a with (ctrl+a).
Then inside the WinMain function load the accelerator:
HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCEL1));
if (hAccel == NULL)
{
MessageBox(NULL, _T("Failed to load accelerator table!"),_T("Error"), MB_OK | MB_ICONEXCLAMATION);
return 0;
}
Note: after trying to define hAccel we do a check to see if a valid handle has be assigned.
While this is not needed, I believe it's better convention.
After this we add the TranslateAccelerator function to the message loop, so the command can be processed in the window procedure:
BOOL bRet;
while (bRet = GetMessage(&Msg, NULL, 0, 0) > 0)
{
if (bRet == -1)
{
// Error handling can be done here.
}
else if (!TranslateAccelerator(hwnd, hAccel, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
Then finally inside the Window procedure
We add the code as follows:
switch(msg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case ID_SELECT_ALL:
{
HWND hEdit;
hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT) // Define IDC_MAIN_EDIT with an integer value in "Resource.h".
}
break;
}
break;
}
}
Note: The message passed to WM_COMMAND is the ID we defined for the ctrl+a accel.
I hope this will help fellow n00bies.

Resources