Thread: opening seperate window to display bitmaps

  1. #16
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Originally posted by bennyandthejets
    Could you post the code that registers the window class "Child_SCTS"?
    Code:
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
         wndclass.lpszMenuName  = "MENUDEMO" ;
         wndclass.lpszClassName = szAppName ;
    
    
    
         wndclass.lpfnWndProc   = ChildWndProc ;
         wndclass.hIcon 		    = NULL ;
         wndclass.lpszClassName = "Child_SCTS" ;
         wndclass.cbWndExtra    = sizeof (long) ;
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  2. #17
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Hang on........

    Why a child window?
    It does not need (or want) to be a child, make it a grown up window!

    Code:
    //first fix that GDI memory leak
    //save the bitmap already in the HDC
    hOldBmp=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) ;
          }
    //Must select old bmp back to delete DC or memory leak 
    SelectObject(hOldBmp);
          DeleteDC (hdcmem);
    
    //change the create call
    //this is wrong
    hwndChild = CreateWindow ("Child_SCTS", TEXT("Wiring diagram"),
                   	WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 282, 363, hwnd, MAKEINTRESOURCE("568a"),
                      TEXT("hInstance"), NULL);
    
    //this is correct (but I used a .c file so some stricter casting may apply, I don't know if you used .cpp)
    
    //note the change to the hInstance, style and menu params
    
    hwndChild = CreateWindow ("Child_SCTS", TEXT("Wiring diagram"),
                   	WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE, 0, 0, 282, 363,hWnd,(HMENU) NULL,
                      hInstance, NULL);
    
    //in the create the load image is sightly off, try
    hBitmap = LoadBitmap (hInstance,MAKEINTRESOURCE(IDB_568a));
    That should work.

    EDIT
    You do register the class?
    if(!RegisterClass(&wndclass))
    return FALSE;
    "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

  3. #18
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Just to be picky/tricky....

    try creating the memhdc in the init or create msg.
    then create another the size of the dialog (make it global or static)
    Draw the image to it as you do in the paint
    Delete the small hdc as you do in the paint
    Keep the one the same size as the dialog

    In the paint you should be able to call GetUpdateRect() (or use the param in the BeginPaint) in your BitBlt() and just draw from your global (or static) hdc. This will be quicker.

    On the dialogs close delete the global or static hdc

    the next step is to destroy the global or static hdc when the dialog is resized and create it again the correct size, then call a paint. Functions are useful here and keep the callback uncluttered.
    "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

  4. #19
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Yes I do register the class but I took that out cause I didn't think you needed to see it but:
    Code:
         HWND     hwnd ;
         MSG      msg ;
         WNDCLASS wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
         wndclass.lpszMenuName  = "MENUDEMO" ;
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
    
         wndclass.lpfnWndProc   = ChildWndProc ;
         wndclass.hIcon 		    = NULL ;
         wndclass.lpszClassName = "Child_SCTS" ;
         wndclass.cbWndExtra    = sizeof (long) ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
    I tried your createwindow command, worked once and the loadbitmap failed. I copied your bitmap line and its giving me
    Code:
    Error:  scts.c(242,64):Undefined symbol 'IDB_568a'
    the line is the load bitmap line.
    I readded the quotations around IDB_568a and re compiled now I am back to where I started with createwindow failing. Im starting to remember why I stopped programming.
    The new createwindow call is
    Code:
                   MessageBox (NULL, TEXT  ("The menu works."), TEXT ("Hellomsg")
                   	, 0);
    
                      hwndChild = CreateWindow ("Child_SCTS", TEXT("Wiring diagram"),
                      WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE, 0, 0, 282, 363,hwnd, NULL,
                      hInstance, NULL);
    
                      if (hwndChild == NULL) {
         					char buf[256];
         					sprintf(buf, "CreateWindow failed with %d", GetLastError());
         					MessageBoxA(NULL, buf, NULL, 0);
    					}
                   return 0;
    The Load bitmap section is(I haven't fixed the GDI leak yet, fix the problem before causing new ones)
    Code:
    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;
    I hope thats enough code, if not I attached the source code file, resource file and resource header. Its all setup for Borland 5.0 IDE compiler.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  5. #20
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You do have a bitmap in your resources with the ID number.

    IDB_568a

    and have included the resource.h header in this file?

    Remember with resource ID numbers;
    IDB_568a is not text, to the compiler it is a number.
    It is easier for us to tell which is a picture of a brick between IDB_BRICK and 1234 (the int resource ID #define 'd as IDB_BRICK in resource.h)

    Where does cyClient and cxClient get a value? (not in the code posted so just checking)
    ie
    for (y = 0 ; y < cyClient ; y += cySource)
    for (x = 0 ; x < cxClient ; x += cxSource)



    I copied your code and opened a dlg using it so I know the corrections work.
    Last edited by novacain; 11-09-2003 at 09:05 PM.
    "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

  6. #21
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    SO the code works for you?
    I have cyClient and cxClient are based on the Hiword and Loword of the WM_SIZE message. its acturally IDB_568A (I just noticed and corrected where I had IDB_568a) but yes I have it in the resource file and the resource.h file. Ive been staring at the code for days and my eyes are starting to see code when I close my eyes.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  7. #22
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    When this sort of thing happens, I am often forced to start again from scratch, and then the program miraculously works. I have spent hours comparing the working version and the faulty version, and still cant find any differences. It's all good being able to spot errors and debug well, but when it gets to this point, it's useless.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #23
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>SO the code works for you?

    Yes. The drawing did not but I did not set the screen dims.

    Comment out the drawing bit. Just get the window open.

    I did not register the first class only the second.



    Instead of the loop to draw the bitmap try

    CreatePatternBrush()//with the bitmap
    then
    FillRect() or Rectangle() //use the one in the paint struct or the whole client

    Remember to delete the brush after.
    "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

  9. #24
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Originally posted by bennyandthejets
    When this sort of thing happens, I am often forced to start again from scratch, and then the program miraculously works. I have spent hours comparing the working version and the faulty version, and still cant find any differences. It's all good being able to spot errors and debug well, but when it gets to this point, it's useless.
    This is acturally what I decided to do. But I have come to the cocliusion it is something with my compiler. This is because I decided to make it easier and use dialog boxes with static Bitmaps. Although even without the bitmap the Dialog Box call failed. I think for some reason my compiler/computer is having problems with creating second windows in the program.
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  10. #25
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    What is this for?

    wndclass.cbWndExtra = sizeof (long) ;

    Try without it.

    >>I think for some reason my compiler/computer is having problems with creating second windows in the program.

    What did getlasterror return?

    Use the same wndclass (diff wndproc) and see if that helps.

    Use 'break' instead of 'return 0' in your wndprocs. This will allow them to be further processed by the default handler (as some msgs still require further processing by windows). ie WM_MOVING
    "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

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