Thread: Cannot select bitmap to DC

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    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:

    Code:
    #include <windows.h>
    #include "resource.h"
    
    #define CX_BITMAP 16
    #define CY_BITMAP 16
    
    char szAppName[] = TEXT("Bitmap test");
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    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"));
    	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(WHITE_BRUSH);
    	wndclass.lpszMenuName = NULL;
    	wndclass.lpszClassName = szAppName;
    
    	if (!RegisterClass(&wndclass))
    	{
    		return 0;
    	}
    	
        hwnd = CreateWindow(szAppName, TEXT("Basic Windows"),
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,CW_USEDEFAULT,
    		CW_USEDEFAULT,CW_USEDEFAULT,
    		NULL,NULL,hInstance,NULL);
    
    	if (!hwnd)
    		return 0;
    
    	ShowWindow (hwnd,iCmdShow);
    	UpdateWindow(hwnd);
    
    	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;
    	PAINTSTRUCT ps;
    
    	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))
    		MessageBeep(0);
    	
    		BitBlt(hdc,12,12,CX_BITMAP,CY_BITMAP,hdc,0,0,SRCCOPY);
    
    		EndPaint(hwnd,&ps);
    		return 0;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    	   return 0;
    	}
    
    	return DefWindowProc(hwnd,message,wParam,lParam);
    }
    can anyone help me out?

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

    What is the resource ID# of the bitmap?

    ie

    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
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    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
    Posts
    380
    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
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    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