Automatically Activating a button after CreateWindow

I have 6 buttons created like this with different tasks:

CreateWindow(L"BUTTON", L"OVERVIEW", WS_VISIBLE | WS_CHILD, 0, 0, 100, 25, hwnd, (HMENU)TAB_OVERVIEW, NULL, NULL);

When the user clicks on this OVERVIEW button, a WM_COMMAND message is sent and the TAB_OVERVIEW button is decoded and my OVERVIEW screen comes up.

How can I automate the OVERVIEW screen coming up once I display all of the buttons? Is there a setting in CreateWindow that will simlate a particular button press (in this case OVERVIEW) and send a WM_COMMAND message?

I can't find any references to this exact scenario.

Thanks.
If I understand you correctly, you can send a BN_CLICKED message programatically using SendMessage(). Check out MSDN on the BN_CLICKED Notification code. You'll want to use various macros such as MAKEWPARAM to load up the WPARAM and LPARAM parameters. Not sure which message handling function you'll want to use to add the SendMessage() call.
Last edited on
depending on how you did things, you can often just call the button handler directly.
the message system eventually calls it, after sending itself a message, decoding it, and figuring out that it needs to call the handler... just bypass all the nonsense and directly punch in, unless you have done something that prevents this.
Here's a fun little example....

Suppose you have a main Form/Window/Dialog with just an edit control and a button on it. When you type something in the edit control, then click the button, code runs which retrieves the contents of the edit control and displays it in a MessageBox(). Here's that program...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// cl Main.cpp /O1 /Os /FeForm4.exe /link Kernel32.lib User32.lib           // 91,648 Bytes
// g++ Form2.cpp -luser32 -lkernel32 -oForm2_gcc.exe -mwindows -m64 -s -Os  // 17,920 Bytes
// cl Form4.cpp /O1 /Os /GS- /link TCLib.lib kernel32.lib user32.lib        //  4,608 Bytes
//#define TCLib
#include <windows.h>
#ifdef TCLib
  #include "stdio.h"
  #include "tchar.h"
#else
  #include <cstdio>
  #include <tchar.h>
#endif
#define  IDC_BUTTON 1500
#define  IDC_EDIT   1505


LRESULT CALLBACK fnWndProc_OnCreate(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 HINSTANCE hIns  = NULL;      
                          
 hIns=((LPCREATESTRUCT)lParam)->hInstance;         
 CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),_T(""),WS_CHILD|WS_VISIBLE,50,40,200,25,hWnd,(HMENU)IDC_EDIT,hIns,0);
 CreateWindowEx(0,_T("button"),_T("Retrieve Text"),WS_CHILD|WS_VISIBLE,95,90,120,30,hWnd,(HMENU)IDC_BUTTON,hIns,0);
  
 return 0; 
}


LRESULT CALLBACK fnWndProc_OnCommand(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 TCHAR szBuffer[256];
                                                               
 if(LOWORD(wParam)==IDC_BUTTON && HIWORD(wParam)==BN_CLICKED)  
 {                                                             
    GetWindowText(GetDlgItem(hWnd,IDC_EDIT),szBuffer,256);	
    MessageBox(hWnd,szBuffer,_T("Button Click"),MB_OK);        
 }                                                             
                                                               
 return 0;	                                               
}                                                              


LRESULT CALLBACK fnWndProc_OnClose(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 DestroyWindow(hWnd);                                          
 return 0;   	
}


LRESULT CALLBACK fnWndProc_OnDestroy(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 PostQuitMessage(0);                                           
 return 0;   	
}


LRESULT CALLBACK fnWndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)                                                // I don't personally much care for placing a lot of code
 {                                                          // in 'case' constructs under a switch statement.  So at
   case WM_CREATE:                                          // left you see I created seperate 'message handling functions'
     return fnWndProc_OnCreate(hWnd, msg, wParam, lParam);  // to put actual code to be executed for each message to be
   case WM_COMMAND:                                         // handled.  The idea illustrated here is pretty simple; in  my
     return fnWndProc_OnCommand(hWnd, msg, wParam, lParam); // actual production code I don't use switch logic but rather
   case WM_CLOSE:
     return fnWndProc_OnClose(hWnd, msg, wParam, lParam);   
   case WM_DESTROY:                                         // for loop logic to iterate through a struct array containing
     return fnWndProc_OnDestroy(hWnd, msg, wParam, lParam); // the message and a function pointer to the message handling
 }                                                          // function that handles that specific message.  

 return (DefWindowProc(hWnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[] =_T("Form4");              
 HWND hWnd = NULL;                              
 WNDCLASSEX wc;                                 
 MSG messages;                                  
                                                
 memset(&wc,0,sizeof(wc));                                          
 wc.lpszClassName = szClassName;                
 wc.lpfnWndProc   = fnWndProc;                  
 wc.cbSize        = sizeof(WNDCLASSEX);         
 wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;    
 wc.hInstance     = hInstance;                  
 RegisterClassEx(&wc);                          
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,175,320,200,HWND_DESKTOP,0,hInstance,0);  // C Based Object Constructor
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {                                       
    TranslateMessage(&messages);         
    DispatchMessage(&messages);          
 }
                                         
 return messages.wParam;
}

Now let's automate that process so that the button click message handler gets called right away....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// cl Main.cpp /O1 /Os /FeForm4.exe /link Kernel32.lib User32.lib           // 91,648 Bytes
// g++ Form2.cpp -luser32 -lkernel32 -oForm2_gcc.exe -mwindows -m64 -s -Os  // 17,920 Bytes
// cl Form4.cpp /O1 /Os /GS- /link TCLib.lib kernel32.lib user32.lib        //  4,608 Bytes
//#define TCLib
#include <windows.h>
#ifdef TCLib
  #include "stdio.h"
  #include "tchar.h"
#else
  #include <cstdio>
  #include <tchar.h>
#endif
#define  IDC_BUTTON 1500
#define  IDC_EDIT   1505


LRESULT CALLBACK fnWndProc_OnCreate(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 HINSTANCE hIns  = NULL;      
 HWND      hEdit = NULL;
 HWND      hBtn  = NULL;
                          
 hIns=((LPCREATESTRUCT)lParam)->hInstance;         
 hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),_T(""),WS_CHILD|WS_VISIBLE,50,40,200,25,hWnd,(HMENU)IDC_EDIT,hIns,0);
 SetWindowText(hEdit,_T("Here Is Some Text!"));
 hBtn=CreateWindowEx(0,_T("button"),_T("Retrieve Text"),WS_CHILD|WS_VISIBLE,95,90,120,30,hWnd,(HMENU)IDC_BUTTON,hIns,0);
 PostMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_BUTTON,BN_CLICKED),(LPARAM)hBtn);
 
 return 0; 
}


LRESULT CALLBACK fnWndProc_OnCommand(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 TCHAR szBuffer[256];
                                                               
 if(LOWORD(wParam)==IDC_BUTTON && HIWORD(wParam)==BN_CLICKED)  
 {                                                             
    GetWindowText(GetDlgItem(hWnd,IDC_EDIT),szBuffer,256);	
    MessageBox(hWnd,szBuffer,_T("Button Click"),MB_OK);        
 }                                                             
                                                               
 return 0;	                                               
}                                                              


LRESULT CALLBACK fnWndProc_OnClose(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 DestroyWindow(hWnd);                                          
 return 0;   	
}


LRESULT CALLBACK fnWndProc_OnDestroy(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 PostQuitMessage(0);                                           
 return 0;   	
}


LRESULT CALLBACK fnWndProc(HWND hWnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)                                                // I don't personally much care for placing a lot of code
 {                                                          // in 'case' constructs under a switch statement.  So at
   case WM_CREATE:                                          // left you see I created seperate 'message handling functions'
     return fnWndProc_OnCreate(hWnd, msg, wParam, lParam);  // to put actual code to be executed for each message to be
   case WM_COMMAND:                                         // handled.  The idea illustrated here is pretty simple; in  my
     return fnWndProc_OnCommand(hWnd, msg, wParam, lParam); // actual production code I don't use switch logic but rather
   case WM_CLOSE:
     return fnWndProc_OnClose(hWnd, msg, wParam, lParam);   
   case WM_DESTROY:                                         // for loop logic to iterate through a struct array containing
     return fnWndProc_OnDestroy(hWnd, msg, wParam, lParam); // the message and a function pointer to the message handling
 }                                                          // function that handles that specific message.  

 return (DefWindowProc(hWnd, msg, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[] =_T("Form4");              
 HWND hWnd = NULL;                              
 WNDCLASSEX wc;                                 
 MSG messages;                                  
                                                
 memset(&wc,0,sizeof(wc));                                          
 wc.lpszClassName = szClassName;                
 wc.lpfnWndProc   = fnWndProc;                  
 wc.cbSize        = sizeof(WNDCLASSEX);         
 wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;    
 wc.hInstance     = hInstance;                  
 RegisterClassEx(&wc);                          
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,175,320,200,HWND_DESKTOP,0,hInstance,0);  // C Based Object Constructor
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {                                       
    TranslateMessage(&messages);         
    DispatchMessage(&messages);          
 }
                                         
 return messages.wParam;
}
Finally, change the PostMessage() call above to SendMessage() and note the difference.
Topic archived. No new replies allowed.