[RESOLVED] GDI Object Failing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] GDI Object Failing

  1. #1
    Join Date
    Oct 2011
    Posts
    23

    [RESOLVED] GDI Object Failing

    I'm writing a program that will display bitmaps in its view. It is a view without a document. When the CBitmap does the LoadBitmap(nID) function, the CGDIObject does not initialize the CBitmap object and obtain the m_hObject value - it remains NULL. I have used the exact same code elsewhere, and it works fine, can anyone tell me how it may work in one place, and not another?

    Here is some of the code:

    Code:
    in CChildView.h file derived from CView
        CBitmap
          m_bmpServer_0,
          m_bmpServer_1,
          m_bmpServer_2,
          m_bmpServer_3,
          m_bmpServer_4,
          m_bmpServer_5;
    ==================================================
    
    
    in CChildView.cpp file in init() function
    // fails to initialize, CGDIObject m_hObject = NULL
        m_bmpServer_0.LoadBitmap( IDB_SERVER0 );
        m_bmpServer_1.LoadBitmap( IDB_SERVER1 );
        m_bmpServer_2.LoadBitmap( IDB_SERVER2 );
        m_bmpServer_3.LoadBitmap( IDB_SERVER3 );
        m_bmpServer_4.LoadBitmap( IDB_SERVER4 );
        m_bmpServer_5.LoadBitmap( IDB_SERVER5 );
    ==================================================
    	
    in CChildView.cpp file in OnDraw(CDC* pDC) function
    	CBitmap *
          pBitmap[] =
          {
              &m_bmpServer_0,
              &m_bmpServer_1,
              &m_bmpServer_2,
              &m_bmpServer_3,
              &m_bmpServer_4,
              &m_bmpServer_5,
          };
    	BITMAP
          bm;
        CDC
          dcMem;
        int
          i,
          nOffSet,
          nYpos;
    
        dcMem.CreateCompatibleDC(pDC);
    
        for ( i = 1; i < 7; i++ )
        {
          nOffSet = (i - 1) * 115;
          pBitmap[i]->GetObject( sizeof(BITMAP), &bm );
          dcMem.SelectObject(pBitmap[i]);
          nYpos = 76 + nOffSet;
          pDC->BitBlt( 10, nYpos, bm.bmWidth, bm.bmHeight, &dcMem, 0, 0, SRCCOPY );
        }
    
        dcMem.DeleteDC();

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,544

    Re: GDI Object Failing

    Quote Originally Posted by He_That_Is View Post
    ... When the CBitmap does the LoadBitmap(nID) function, the CGDIObject does not initialize the CBitmap object and obtain the m_hObject value - it remains NULL.
    Did you check the return value of LoadBitmap? Isn't it zero (FALSE)?
    Does your resource file contain these bitmaps?
    Are the resource IDs in the resource file the same as you use in your CChildView class?
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,640

    Re: GDI Object Failing

    One thing you need to change:

    Code:
    for ( i = 1; i < 7; i++ )
    {
          nOffSet = (i - 1) * 115;
          pBitmap[i]->GetObject( sizeof(BITMAP), &bm );
    You are using some invalid indices for pBitmap

  4. #4
    Join Date
    Oct 2011
    Posts
    23

    Red face Re: GDI Object Failing

    The return from the LoadBitmap() is FALSE (0), should be returning TRUE (1). The resource,h contains all of the resource ID's they are all correct and accurate, and the same as I am using in CChildView. The indices are correct, and work fine in another project. This really has ma stumped.

    Thank you so much for your assistance, I really do appreciate it.

    --Victor

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,640

    Re: GDI Object Failing

    Quote Originally Posted by He_That_Is View Post
    The indices are correct, and work fine in another project. This really has ma stumped.

    Thank you so much for your assistance, I really do appreciate it.

    --Victor
    How are the indices correct ?... in the for loop the value of i are : 1,2,3,4,5,6

    the valid indices for pBitmap are : 0,1,2,3,4,5

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,544

    Re: GDI Object Failing

    1. Try to Rebuild All
    2. Try to load your .exe in the IDE as a resouirce and check whether bitmaps are there and if they are - with which IDs
    Victor Nijegorodov

  7. #7
    Join Date
    Oct 2011
    Posts
    23

    Smile Re: GDI Object Failing

    I took a closer to the resource info, and found a very interesting dilema. resource has the exact same resource ID's with the correct values, however, when I open the Resource ID editor (right-click on .rc file and select "Resource ID") the data names are exactly the same, but all of the values are different. I have never seen anything like this before. I have corrected the value discrepancies, and now it works fine. Thank you for mentioning the resources as this drew me in to look closer.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

    Re: GDI Object Failing

    Quote Originally Posted by He_That_Is View Post
    I took a closer to the resource info, and found a very interesting dilema. resource has the exact same resource ID's with the correct values, however, when I open the Resource ID editor (right-click on .rc file and select "Resource ID") the data names are exactly the same, but all of the values are different. I have never seen anything like this before. I have corrected the value discrepancies, and now it works fine. Thank you for mentioning the resources as this drew me in to look closer.
    You still didn't answer Philip.

    Those indices are not correct. You have a buffer overrun. Whether they "work" in another project doesn't matter. When you overrun the array buffer, anything can happen, including "working", crashing, working today and not tomorrow, etc.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center