Thread: Cannot select bitmap to DC

  1. #1
    Join Date
    Feb 2003

    Cannot select bitmap to DC

    ok, here is the problem, I cannot get a bitmap to be selected to a DC, my code is as follows:

    #include <windows.h>
    #include "resource.h"
    #define CX_BITMAP 16
    #define CY_BITMAP 16
    char szAppName[] = TEXT("Bitmap test");
    HINSTANCE hInst;
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    	HWND hwnd;
    	MSG msg;
    	WNDCLASS wndclass;
    	hInst = hInstance;
    	GreenLight= LoadBitmap(hInst,TEXT("GreenLight")); = 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(WHITE_BRUSH);
    	wndclass.lpszMenuName = NULL;
    	wndclass.lpszClassName = szAppName;
    	if (!RegisterClass(&wndclass))
    		return 0;
        hwnd = CreateWindow(szAppName, TEXT("Basic Windows"),
    	if (!hwnd)
    		return 0;
    	ShowWindow (hwnd,iCmdShow);
    	while(GetMessage (&msg,NULL,0,0))
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	return msg.wParam;
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    	HBITMAP GreenLight;
    	HDC  hdc,hdc1,hdcMem;
    	int i;
    	switch (message)
    	case WM_CREATE:
    		GreenLight= LoadBitmap(hInst,TEXT("GreenLight"));
    		return 0;
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd,&ps);
    		hdcMem = CreateCompatibleDC(hdc);
    	if	(!SelectObject(hdcMem,GreenLight))
    		return 0;
    	case WM_DESTROY:
    	   return 0;
    	return DefWindowProc(hwnd,message,wParam,lParam);
    can anyone help me out?

  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    What value does GreenLight get when you try to load?
    If LoadBitamp() returns zero call GetLastError()

    What is the resource ID# of the bitmap?


    if the bmp's resource is called IDB_BITMAP1

    //load from the apps resource file
    hBMP = LoadBitmap( hInst, MAKEINTRESOURCE( IDB_BITMAP1 ));

    Your paint also has a huge leak in it.

    You create a hdc, change its contents then don't DeleteDC() (or even put it back the way you found it). Look up some other threads here or MSDN for DeleteDC() and ReleaseDC().

    You should always catch the return from SelectObject(), so you can return this GDI resource back to the HDC prior to freeing the HDC.
    "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. #3
    Join Date
    Feb 2003
    Ok, I should of provided more detail. The bitmap loaded into GreenLight is valid, as it works fine if I use it as a background brush, the program even loads up perfectly, it just wont display with the BitBlt command, and i have always had this problem with MSVC 5.0, even using Petzolds as a reference.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Either make GreenLight global or static. Then change the second hdc in BitBlt from hdc to hdcMem.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  5. #5
    Join Date
    Feb 2003
    thanks, the static is what made it work, thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  3. screenshots before a window pops up?
    By MadHatter in forum Game Programming
    Replies: 13
    Last Post: 12-31-2002, 11:09 AM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  5. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM