Create a new window after pressing a button.

Hello world!!

I am a beginner in creating windows programming. At the moment I am trying to obtain an application that displays a bmp file into a pop-up window after an action like a button or a selected menu.

The main code is here (the resource files are not relevant for the problem!):

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <windows.h>
#include "resource.h"

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK SplashWndProc (HWND, UINT, WPARAM, LPARAM);

HWND hMainWnd;               
HWND hSplashWnd;             
HWND hWndButton;             
HBITMAP hSplashBMP;      
HDC hSplashDC;
HDC hMemoryDC;
LONG BitmapWidth, BitmapHeight;


char szClassName[ ] = "WindowsApp";
char SplashClassName[] = "SplashWindowClass";


int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{

    MSG messages;            
    WNDCLASSEX wincl;        
    WNDCLASSEX    splashwincl;  


    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);
    wincl.hIcon = LoadIcon (hThisInstance, MAKEINTRESOURCE(IDI_ICON));
    wincl.hIconSm = LoadIcon (hThisInstance, MAKEINTRESOURCE(IDI_ICON));
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
    wincl.cbClsExtra = 0;                      
    wincl.cbWndExtra = 0;                      
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

    splashwincl.cbSize           = sizeof(WNDCLASSEX);
    splashwincl.style            = 0;
    splashwincl.lpfnWndProc      = (WNDPROC)SplashWndProc;
    splashwincl.cbClsExtra       = 0;
    splashwincl.cbWndExtra       = 0;
    splashwincl.hInstance        = hThisInstance;
    splashwincl.hIcon            = NULL;
    splashwincl.hIconSm          = NULL;
    splashwincl.hCursor          = LoadCursor(NULL, IDC_ARROW);
    splashwincl.hbrBackground    = NULL;
    splashwincl.lpszMenuName     = NULL;
    splashwincl.lpszClassName    = SplashClassName;

    if (!RegisterClassEx(&splashwincl))
    {
        MessageBox(NULL, "Failed To Register The Splash Window Class.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }

    RECT DesktopRect;
    GetWindowRect(GetDesktopWindow(), &DesktopRect);    

    int mainWinRows = 256;
    int mainWinCols = 256;
    
    hMainWnd = CreateWindowEx (
           WS_EX_CLIENTEDGE,    /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Pinzas v2.0",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           1,       /* Windows decides the position *///(DesktopRect.right - mainWinRows) / 2
           1,       /* where the window ends up on the screen *///(DesktopRect.bottom - mainWinCols) / 2
           mainWinCols,         /* The programs width */
           mainWinRows,         /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );
    
    if (!hMainWnd)
    {
        MessageBox(NULL, "Window Creation Failed.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }


    hSplashBMP = LoadBitmap(hThisInstance, MAKEINTRESOURCE(BITMAP_ID));

    if (!hSplashBMP)
    {
        MessageBox(NULL, "Failed To Load Bitmap", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }
    
    BITMAP Bitmap;
    GetObject(hSplashBMP, sizeof(BITMAP), &Bitmap);
    BitmapWidth = Bitmap.bmWidth;
    BitmapHeight = Bitmap.bmHeight;

    HWND hSplashWnd = CreateWindowEx(
    0,
    SplashClassName,
    "Modulator",
    WS_POPUP,
    (DesktopRect.right - BitmapWidth) / 2,
    (DesktopRect.bottom - BitmapHeight) / 2,
    Bitmap.bmWidth,
    Bitmap.bmHeight,
    HWND_DESKTOP,
    NULL,
    hThisInstance,
    NULL);
    
    if (!hSplashWnd)
    {
        MessageBox(NULL, "Splash Window Creation Failed.", "Error", MB_OK | MB_ICONERROR);
        return 0;
    }
    
    hSplashDC = GetDC(hSplashWnd);
    hMemoryDC = CreateCompatibleDC(hSplashDC);
    SelectObject(hMemoryDC, (HGDIOBJ)hSplashBMP);

    ShowWindow (hMainWnd, SW_SHOW);
    
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }
    
    // Free memory for the bitmap
    DeleteObject(hSplashBMP);
    ReleaseDC(hSplashWnd, hSplashDC);
    ReleaseDC(hSplashWnd, hMemoryDC);
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}



LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {

        case WM_CREATE:
        {
            HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);

            hWndButton = CreateWindowEx(
            0, 
            "BUTTON", 
            "Plot", 
            WS_VISIBLE | WS_CHILD, 
            10, 
            50, 
            80, 
            20, 
            hwnd, 
            (HMENU) IDB_BUTTON, 
            hInstance, 
            NULL); 
        }
        break;

        case WM_COMMAND: 
        {
            switch (LOWORD(wParam))
            {
                case IDB_BUTTON:
                {
                    switch (HIWORD(wParam))
                    {
                        case BN_CLICKED:
                            //MessageBox(NULL, "Selected Button", "Success", MB_OK | MB_ICONINFORMATION);
                            // Load the splash to the screen
                            ShowWindow (hSplashWnd, SW_SHOWNORMAL);
                        break; 
                    }
                }
                break;
                
                case IDM_FILE_EXIT: 
                    PostMessage(hwnd, WM_CLOSE, 0, 0);
                break; 

                case IDM_ITEM: 
                     ShowWindow (hSplashWnd, SW_SHOWNORMAL);
                break; 
                
            }
            return 0;
        } 
        break;
        
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
            
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

LRESULT CALLBACK SplashWndProc( HWND    hwnd,
                                UINT    message,
                                WPARAM  wParam,          
                                LPARAM  lParam )
{
    HDC hdc;
    PAINTSTRUCT ps;

    switch (message)
    {
        case WM_PAINT:
             hdc = BeginPaint(hwnd, &ps);
             BitBlt(hdc, 0, 0, BitmapWidth, BitmapHeight, hMemoryDC, 0, 0, SRCCOPY); 
             EndPaint(hwnd, &ps);
        break;

        case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;

        default:
            return (DefWindowProc(hwnd, message, wParam, lParam));
    }
    return 0;
}


The problem is that the popup window does not appear!!. But if I include the sentence ShowWindow inside the WinMain it does appear; but what I want to do is to have the ability to control the display.

What is wrong with my code?, or is it a better way of doing this?

Thanks a lot.
Topic archived. No new replies allowed.