Handling main window correctly

Hi,
I am learning Win32 C++ programming. I have some problems in handling the main window.

I want two buttons to be displayed on my main window.On clicking the button 1 the button 2 should be displayed but I see both code displaying on my main window!
What should I do to clear the screen and display the next button.

Here is a small snippet of my code!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
HWND button1,button2;
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
button 1 =CreateWindow("BUTTON" , "START" , WS_CHILD|BS_BITMAP|BS_CENTER|BS_OWNERDRAW , 350 , 300 , 100 , 50 , hWnd , (HMENU)ID_DLG2_START , hInstance , NULL);
button 2 =CreateWindow("BUTTON" , "STOP" , WS_CHILD|BS_BITMAP|BS_CENTER|BS_OWNERDRAW , 350 , 300 , 100 , 50 , hWnd , (HMENU)ID_DLG2_STOP , hInstance , NULL);
if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   mainWND = hWnd;

   return TRUE;

}


Thank you
Last edited on
V07 wrote:
What should I do to clear the screen and display the next button.


You'll need to handle/process events. In your WM_COMMAND event you'll get the click events from buttons.

You can use ShowWindow to show/hide the buttons in question.

If you want to manually redraw the screen you can use InvalidateRect.
Hi, wizebin

Since I am a beginner in this topic I could not catch your suggestions. I referred Microsoft's documentation but could not figure out. Could you please give me a example or link or something.

Thank you
closed account (z05DSL3A)
https://msdn.microsoft.com/library/windows/desktop/ff381399(v=vs.85).aspx
you need something like "WS_CHILD&~WS_VISIBLE|..." in style

that you'd type shouldn't make button visible

mainWND = hWnd; not needed

If you run code from say vs2005 all be cool

after deal with errors.


i've change style string suggestion
Last edited on

Since I am a beginner in this topic I could not catch your suggestions. I referred Microsoft's documentation but could not figure out. Could you please give me a example or link or something.


The ShowWindow() function takes two parameters. The 1st is the handle of the window you want to show or hide, the second is the action you want to take...

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx

In your case SW_HIDE will make the button invisible.
no it's bad style .Button without ws_visible should not appear
that that you'd type it apeear and then disappear.
Last edited on
Here is what you want I think....

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
//Main.cpp
#ifndef UNICODE
    #define  UNICODE
#endif
#ifndef _UNICODE
    #define  _UNICODE
#endif
#include <windows.h>
#define BTN_START  1500
#define BTN_STOP   1505


LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
 switch(msg)
 {
   case WM_CREATE:
    {
       HINSTANCE hIns=((LPCREATESTRUCT)lParam)->hInstance;
       CreateWindowEx(0,L"button",L"Start",WS_CHILD|WS_VISIBLE,125,40,80,30,hwnd,(HMENU)BTN_START,hIns,0);
       CreateWindowEx(0,L"button",L"Stop",WS_CHILD,125,80,80,30,hwnd,(HMENU)BTN_STOP,hIns,0);
       return 0;
    }
   case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
          case BTN_START:
            ShowWindow(GetDlgItem(hwnd,BTN_STOP),SW_SHOW);
            ShowWindow(GetDlgItem(hwnd,BTN_START),SW_HIDE);
            break;
          case BTN_STOP:
            MessageBox(hwnd,L"You Clicked The Stop Button!",L"Report",MB_OK);
            break;
        }
        return 0;
    }
   case WM_DESTROY:
    {
       PostQuitMessage(0);
       return 0;
    }
 }

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


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 TCHAR szClassName[]=L"PowerfulProcessing";
 WNDCLASSEX wc={0};
 MSG messages;
 HWND hWnd;

 wc.lpszClassName = szClassName;
 wc.lpfnWndProc   = fnWndProc;
 wc.cbSize        = sizeof(WNDCLASSEX);
 wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
 wc.hInstance     = hInstance;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,L"Some Big Powerful Processing App",WS_OVERLAPPEDWINDOW,150,150,350,250,HWND_DESKTOP,0,hInstance,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}
Topic archived. No new replies allowed.