opening seperate window to display bitmaps

This is a discussion on opening seperate window to display bitmaps within the Windows Programming forums, part of the Platform Specific Boards category; I'm currently designing a program that I want to open a seperate window and display a bitmap, when selected in ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    202

    opening seperate window to display bitmaps

    I'm currently designing a program that I want to open a seperate window and display a bitmap, when selected in a menu. I set up a menu and the child window and the bitmap but when ran and the menu selected it doesn't do anything. It acts likes I got nothing in the switch statment in the menu.
    What i have is
    Code:
    case IDM_WD_568A:
    hwndChild = CreateWindow (szChildClass, NULL,
                   	WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 282, 363, hwnd, 568A,
                      (HINSTANCE GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    The childWndProc is:
    Code:
    LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,
    								WPARAM wParam, LPARAM lParam)
    {
    
       static HBITMAP  hBitmap;
       static int      cxClient, cyClient, cxSource, cySource ;
       BITMAP          bitmap;
       HDC				 hdc, hdcmem;
       HINSTANCE		 hInstance ;
       int 				 x,y;
       PAINTSTRUCT 	 ps;
    
    	switch (message)
       {
       	case WM_CREATE :
          hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
    
          hBitmap = LoadBitmap ("childInstance", TEXT ("Wiring Diagram"));
    
          GetObject (hBitmap, sizeof (BITMAP), &bitmap);
    
          cxSource = bitmap.bmWidth;
          cySource = bitmap.bmHeight;
    
          return 0;
    
          case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          hdcmem = CreateCompatibleDC (hdc) ;
          SelectObject (hdcmem, hBitmap);
          for (y = 0 ; y < cyClient ; y += cySource)
          for (x = 0 ; x < cxClient ; x += cxSource)
          {
          	BitBlt (hdc, x, y, cxSource, cySource, hdcmem, 0, 0, SRCCOPY) ;
          }
    
          DeleteDC (hdcmem);
          EndPaint (hwnd, &ps);
          return 0;
    
       	case WM_DESTROY:
          DeleteObject(hBitmap);
       	return 0;
       }
    }
    What am I doing wrong?

    Ray Koons

    PS..I used to post ALOT here like 2 years ago but to a hiatus from programming but am starting to get back into it.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  2. #2
    Registered User lobo's Avatar
    Join Date
    Oct 2001
    Posts
    71
    I guess using 568A as child window identifier may be the problem?

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Try it without the WS_CHILDWINDOW flag. On another note, that flag has the same value as WS_CHILD, so you needn't type those 6 extra characters.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Originally posted by bennyandthejets
    Try it without the WS_CHILDWINDOW flag. On another note, that flag has the same value as WS_CHILD, so you needn't type those 6 extra characters.
    I tried that, didn't change anything. I also changed the name of the child window like lobo said, still no effect :-(
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try putting a call to MessageBox right before the CreateWindow code to make sure that the CreateWindow code is being reached.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    message box comes up but no childwindow
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try changing the 2nd parameter in CreateWindow to TEXT( "" ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I never see the point in using TEXT(). Just look at the definition. It has absolutely no effect whatsoever on the parameter passed, it's exactly the same.

    I just noticed that in this code, there is an imbalance of brackets. It should have caused a compile error.
    Code:
    hwndChild = CreateWindow (szChildClass, NULL,
                   	WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 282, 363, hwnd, 568A,
                      (HINSTANCE GetWindowLong(hwnd, GWL_HINSTANCE), //Need another closing bracket here
    NULL);
    I don't see how it could compile with this error.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The bracket belongs after the HINSTANCE (assuming it was not a typo on the OP's behalf):
    Code:
    hwndChild = CreateWindow (szChildClass, NULL,
                   	WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 282, 363, hwnd, 568A,
                      (HINSTANCE) /* Added missing closing bracket */GetWindowLongPtr(hwnd, GWL_HINSTANCE), NULL);
    Life is a lot easier if you have an error number:
    Code:
    if (hwndChild == NULL) {
         char buf[256];
         sprintf(buf, "CreateWindow failed with %d", GetLastError());
         MessageBoxA(NULL, buf, NULL, 0);
    }
    I never see the point in using TEXT(). Just look at the definition. It has absolutely no effect whatsoever on the parameter passed, it's exactly the same.
    You're going to get a rude shock when you start producing unicode compatible apps. :-) The TEXT macro causes a string literal to be a normal 8-bit string when unicode is not defined and a wide character string when unicode is defined.
    Code:
    #ifdef UNICODE
    #define TEXT(string) L##string
    #else
    #define TEXT(string) string
    #endif
    You should read a tutorial on making your apps unicode compatible as it will save a lot of trouble down the track. For now, it is a good idea to use the TEXT macro and use the LPTSTR and TCHAR types instead of LPSTR and char types repspectively.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Child windows can't have menus.

    MSDN
    "WS_CHILD
    Creates a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style."

    The resource ID is an int so yours, ending in an A, is invalid. IF thats what you called the menu then use MAKEINTRESOURCE()

    If you specify the WS_CHILD then the HMENU param is interpreted as the windows int resource ID.

    Your paint is going to be slow as you do all the drawing after the OS has called for a paint.
    You have a GDI leak as the bitmap is selected into a HDC, not selected out again. This will prevent the HDC from being deleted as it has not been returned to its default state.
    The bitmap will also not delete as it is still selected into a HDC (though this may be taken care of by the OS as the HDC has lost scope)

    PS if you are going to open a window to display bitmaps of differing size look at

    AdjustWindowRect()

    I have posted examples of how to use this to resize the window to the image size.
    Last edited by novacain; 11-07-2003 at 12:50 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    The error code is 0.
    This is my now reformed code:
    Code:
              case IDM_WD_568A:
                   MessageBox (NULL, TEXT  ("The menu works."), TEXT ("Hellomsg")
                   	, 0);
         				hwndChild = CreateWindow ("Child_SCTS", TEXT("Wiring diagram"),
                   	WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 282, 363, hwnd, MAKEINTRESOURCE("568a"),
                      TEXT("hInstance"), NULL);
    
                   if (hwndChild == NULL) {
         					char buf[256];
         					sprintf(buf, "CreateWindow failed with %d", GetLastError());
         					MessageBoxA(NULL, buf, NULL, 0);
    					}
                   return 0;
    I haven't changed the childwndproc so Im not gonna repost that. I get the message box to come up so it hase to be something in the createwindow call but I can't see anything wrong. Also when creating a child window the ninth parameter of the createwindow call is the handle to the child window. There is no menu and its impossible to have a menu in a child window.
    Last edited by Isometric; 11-07-2003 at 08:03 AM.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Something is missing...

    return DefWindowProc(...);

    Don't you hate that? We should make a checklist of reasons why window creation fails and post it on the FAQ board.

  13. #13
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    I was missing that but Im still getting the same result.
    The WndChildProc now looks like this:
    Code:
    LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,
    								WPARAM wParam, LPARAM lParam)
    {
    
       static HBITMAP  hBitmap;
       static int      cxClient, cyClient, cxSource, cySource ;
       BITMAP          bitmap;
       HDC				 hdc, hdcmem;
       HINSTANCE		 hInstance ;
       int 				 x,y;
       PAINTSTRUCT 	 ps;
    
    	switch (message)
       {
       	case WM_CREATE :
          hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
    
          hBitmap = LoadBitmap ("childInstance", TEXT ("Wiring Diagram"));
    
          GetObject (hBitmap, sizeof (BITMAP), &bitmap);
    
          cxSource = bitmap.bmWidth;
          cySource = bitmap.bmHeight;
    
          return 0;
    
          case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          hdcmem = CreateCompatibleDC (hdc) ;
          SelectObject (hdcmem, hBitmap);
          for (y = 0 ; y < cyClient ; y += cySource)
          for (x = 0 ; x < cxClient ; x += cxSource)
          {
          	BitBlt (hdc, x, y, cxSource, cySource, hdcmem, 0, 0, SRCCOPY) ;
          }
    
          DeleteDC (hdcmem);
          EndPaint (hwnd, &ps);
          return 0;
    
       	case WM_DESTROY:
          DeleteObject(hBitmap);
       	return 0;
       }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    I fixed up some of the WM_CREATE message but to no avail. This is really agrivatig me now.
    Code:
    LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,
    								WPARAM wParam, LPARAM lParam)
    {
    
       static HBITMAP  hBitmap;
       static int      cxClient, cyClient, cxSource, cySource ;
       BITMAP          bitmap;
       HDC				 hdc, hdcmem;
       HINSTANCE		 hInstance;
       int 				 x,y;
       PAINTSTRUCT 	 ps;
    
    	switch (message)
       {
       	case WM_CREATE :
          hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
    
          hBitmap = LoadBitmap (hInstance,MAKEINTRESOURCE("IDB_568a"));
    
          if (hBitmap == NULL) {
         	char buf[256];
         	sprintf(buf, "LoadBitmap failed with %d", GetLastError());
         	MessageBoxA(NULL, buf, NULL, 0);
    		}
    
          GetObject (hBitmap, sizeof (BITMAP), &bitmap);
    
          cxSource = bitmap.bmWidth;
          cySource = bitmap.bmHeight;
    
          return 0;
    
          case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          hdcmem = CreateCompatibleDC (hdc) ;
          SelectObject (hdcmem, hBitmap);
          for (y = 0 ; y < cyClient ; y += cySource)
          for (x = 0 ; x < cxClient ; x += cxSource)
          {
          	BitBlt (hdc, x, y, cxSource, cySource, hdcmem, 0, 0, SRCCOPY) ;
          }
    
          DeleteDC (hdcmem);
          EndPaint (hwnd, &ps);
          return 0;
    
       	case WM_DESTROY:
          DeleteObject(hBitmap);
       	return 0;
       }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  15. #15
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Could you post the code that registers the window class "Child_SCTS"?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how i create a window whith all it's elements
    By rasheed in forum Windows Programming
    Replies: 1
    Last Post: 05-31-2006, 06:53 PM
  2. Adding colour & bmps to a Win 32 Window??
    By carey_sizer in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2004, 05:55 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21