• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

DestroyWindow newby question.

I have a simple test program that I have been using to develop a splash screen. There are two problems with it.

1. If I do a 'DestroyWindow' for the splash screen and then try to do a 'MessageBox' then the message box is not displayed (The function returns code 0x01). Any MessageBox before the DestroyWindow call works however.

2. If the splash screen is hidden by another window it is not re-painted when it is un-hidden.

The code that demonstrates these problems is:-

#include <windows.h>

HINSTANCE    hSplashInstance;
HINSTANCE    hSplashInst;
HWND            hSplashWnd;
HBITMAP        imgHandle;
BITMAP            gBitMap;

LRESULT CALLBACK SplashProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
      PAINTSTRUCT      ps;
      HDC                  hdc;
      HDC                  hDCMem;

      switch (message) {
      case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            hDCMem = CreateCompatibleDC(hdc);
            SelectObject(hDCMem, imgHandle);
            SetMapMode(hDCMem, GetMapMode(hdc));  
            // Draw the bitmap using the BitBlt function        
            if (BitBlt(
                  hdc,                // The destination device context
                  0, 0,               // Coordinates of the destination rectangle
                  gBitMap.bmWidth,   // Width of the dest. and source rectangle
                  gBitMap.bmHeight,// Height of the dest. and source rectangle
                  hDCMem, 0, 0, SRCCOPY)) {
                  DeleteDC(hDCMem);
            }

            RECT rt;
            GetClientRect(hWnd, &rt);

            EndPaint(hWnd, &ps);
            break;
      case WM_DESTROY:
            PostQuitMessage(0);
            break;
      default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


BOOL InitSplashInstance(HINSTANCE hSplashInstance, int nCmdShow) {
      //
      imgHandle = (HBITMAP)LoadImage(NULL,"splash.bmp",NULL,NULL,NULL,LR_LOADFROMFILE);
      if (!imgHandle) {
            return FALSE;
      }
      GetObject(imgHandle, sizeof(BITMAP), &gBitMap);

      hSplashInst = hSplashInstance; // Store instance handle in our global variable



      hSplashWnd = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_DLGMODALFRAME |
       WS_EX_WINDOWEDGE, "SplashClass", NULL, WS_POPUP,
         (GetSystemMetrics(SM_CXSCREEN) - gBitMap.bmWidth) / 2,
         (GetSystemMetrics(SM_CYSCREEN) - gBitMap.bmHeight) / 2,
         gBitMap.bmWidth, gBitMap.bmHeight,
       NULL, NULL, hSplashInstance, NULL);
             
      if (!hSplashWnd) {
            return FALSE;
      }

      ShowWindow(hSplashWnd, nCmdShow);
      UpdateWindow(hSplashWnd);

      return TRUE;
}

ATOM SplashRegisterClass(HINSTANCE hSplashInstance) {
      WNDCLASSEX wcex;

      wcex.cbSize = sizeof(WNDCLASSEX);

      wcex.style = CS_HREDRAW | CS_VREDRAW;
      wcex.lpfnWndProc = (WNDPROC)SplashProc;
      wcex.cbClsExtra = 0;
      wcex.cbWndExtra = 0;
      wcex.hInstance = hSplashInstance;
      wcex.hIcon = NULL;
      wcex.hCursor = NULL;
      wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
      wcex.lpszMenuName = NULL;
      wcex.lpszClassName = "SplashClass";
      wcex.hIconSm = NULL;

      return RegisterClassEx(&wcex);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow) {
      int      retval;

      // Create and display a SPLASH SCREEN
      SplashRegisterClass(hSplashInstance);
      if(!InitSplashInstance(hSplashInstance, nCmdShow)){
            return FALSE;
      }

      Sleep(2000);

      DestroyWindow(hSplashWnd);

      retval = MessageBox(NULL, "test message", "Diagnostics", MB_APPLMODAL|MB_ICONSTOP|MB_OK);
      Sleep(2000);

      return(0);
}
0
icd
Asked:
icd
  • 6
  • 3
  • 3
1 Solution
 
icdAuthor Commented:
Edited text of question
0
 
alexoCommented:
Here's your problem:

    case WM_DESTROY:
        PostQuitMessage(0);

Lose the PostQuitMessage() call.  The PostQuitMessage function indicates to Windows that a thread has made a request to terminate (quit).  This is *not* what you need.
0
 
icdAuthor Commented:
alexo.

Thanks for that answer, it certainly answered the first part of my question.

As for the second part, do you have any idea how I can get the program to repaint my splash screen when it is un-hidden?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
alexoCommented:
>> As for the second part [...]
Sorry, missed it.

Sleep(2000) is your problem.  While the thread is asleep, no message processing takes place.  Use a timer instead (SetTimer(), WM_TIMER and friends).
0
 
nietodCommented:
Alex, it is a bigger problem than that.  He also needs a message loop for the splash screen.  Currently he displays the window, initializes the program, and destroys the window in a stream.  That is typical for splash windows, but it does make them rather unreactive.  To make it more reactive, you may need to start a 2nd thread for the splash window and provide it with a message loop.
0
 
alexoCommented:
>> He also needs a message loop for the splash screen
Right.  Where are my glasses?  :-)

>> To make it more reactive, you may need to start a 2nd thread for the splash window
Why a second thread?  Using a timer should be easier.
0
 
nietodCommented:
I was thinking a second thread that could handle its window messages why the 1st thread does its initialization and then destroys the splash window and thread.  However, I think I might need to clean my glasses as there is no evidence of any initialization going on as the splash window is displayed.  
0
 
icdAuthor Commented:
The sleep(2000) is to emulate my own (real) code which carries out several seconds of initialisation. I suspect that a second thread may be the only solution here since there will be no messages processed while I carry out my initialisation. Indeed I don't have a message loop in my (real) program at all since I don't (at least until now) need one!

0
 
alexoCommented:
icd, you *should* have a message pump in your program for it to behave in a manner consistent with windows guidelines.  One example is DDE - if *any* running app is not processing messages, all DDE is blocked during this time.
0
 
alexoCommented:
And the autograder hits again!
0
 
nietodCommented:
Did it just get autograded?  That is not 21 days.  Is there a Y2K-1 problem?
0
 
alexoCommented:
Maybe EE changed the timeout?
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now