Sign in to follow this  

Help with Win32 GDI

This topic is 3565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using visual c++ 2005 and I'm trying to display a bitmap in the window. It works fine if I put the code in the WM_PAINT message but I need to do it outside of it. How come it doesn't work when I put it in a separate function? Code is below... Any help is appreciated, thanks!

#include <windows.h>
#include "Resource.h"

HINSTANCE hInst;
LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
void drawBitmap(HWND hWnd);

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX  WndCls;
    static char szAppName[] = "BitmapTest";
    MSG         Msg;

	hInst       = hInstance;
    WndCls.cbSize        = sizeof(WndCls);
    WndCls.style         = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
    WndCls.lpfnWndProc   = WindProcedure;
    WndCls.cbClsExtra    = 0;
    WndCls.cbWndExtra    = 0;
    WndCls.hInstance     = hInst;
    WndCls.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    WndCls.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndCls.lpszMenuName  = NULL;
    WndCls.lpszClassName = szAppName;
    WndCls.hIconSm       = LoadIcon(hInstance, IDI_APPLICATION);
    RegisterClassEx(&WndCls);

    CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, szAppName, "Bitmap Test", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    while( GetMessage(&Msg, NULL, 0, 0) )
    {
        TranslateMessage(&Msg);
        DispatchMessage( &Msg);
    }

    return static_cast<int>(Msg.wParam);
}

LRESULT CALLBACK WindProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
     switch(Msg)
    {
	case WM_CREATE:
		drawBitmap(hWnd);
		break;
	case WM_DESTROY:
	    PostQuitMessage(WM_QUIT);
	    break;
	default:
	    return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}

void drawBitmap(HWND hWnd)
{
	HDC wndDC, bmpDC;
	HBITMAP bmp;

	wndDC = GetDC(hWnd);

	bmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1));

	bmpDC = CreateCompatibleDC(wndDC);

	SelectObject(bmpDC, bmp);

	BitBlt(wndDC, 10, 10, 10, 10, bmpDC, 0, 0, SRCCOPY);

	DeleteDC(bmpDC);

	DeleteObject(bmp);
}


Share this post


Link to post
Share on other sites
A good place to put your drawing code is in your WM_PAINT handler and then call InvalidateRect() when you want to cause the window to be repainted. Right now, your bitmap isnt being drawn because WM_PAINT is coming after WM_CREATE, thereby erasing everything you did in WM_CREATE.

Share this post


Link to post
Share on other sites

This topic is 3565 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this