Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of C++ SelectObject function

Status
Not open for further replies.

peternkelly

Programmer
Oct 4, 2010
1
GB
I am trying to recompile a C++ application that I originally wrote about 12 years ago. It compiled then with a V.1 C++ compiler and has since run on Windows 98, 2000, XP and Vista machines. But it will not run on my new Windows 7 machine; hence the need to recompile, which I am trying to do with a Windows 7 C++ compiler which I have recently purchased. The particular sticking point comes in the following function:


Code:
int outmap(HBITMAP hbmOut, HWND hWnd, int xx, int yy)
/* output bitmap at xx, yy */

{
HDC hDC, hdcMemory;        // display context variable
HBITMAP hbmOld;
BITMAP bm;

    GetObject(hbmOut, sizeof(bm), (LPSTR)&bm);
    /*
    Get a DC to our window.  Create a compatible memory
    DC and select our image bitmap into it so we can blit
    it to the main window DC
    */
    hDC = GetDC(hWnd);
    hdcMemory = CreateCompatibleDC(hDC);
    hdmOld = SelectObject(hdcMemory, hbmOut);    
               // error C2440 occurs for the previous line

    if(hbmOld)
    {
      /* Blit the image in the new position */
      BitBlt( hDC,          // dest DC
              xx, yy,       // dest origin
              bm.bmWidth, bm.bmHeight,    // dest extents
              hdcMemory,                  // src DC
              0,0,                        // src origin
              SRCCOPY  );                 // ROP code
      SelectObject(hdcMemory, hbmOld);
    }
  DeleteDC(hdcMemory);
  ReleaseDC(hWnd, hDC);
return(0);
}

The actual error message I get is:

Code:
[name, line no.]error C2440: '=' : cannot convert from 'HGDIOBJ' to 'HBITMAP'

Conversion from 'void*' to pointer to non- 'void' requires an explicit cast
which occurs at the point I have indicated in the source code. I have tried many variants on the above code, but the error remains. Can anyone tell me what I am doing wrong ?

peternkelly
 
is it possible that you need to cast what is being returned from selectObject:

Code:
C cast:
hdmOld = (HBITMAP)SelectObject(hdcMemory, hbmOut);
or
Code:
hdmOld = static_cast<HBITMAP>(SelectObject(hdcMemory, hbmOut));
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top