Thread: ....this code doesn't compile, very odd error

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    ....this code doesn't compile, very odd error

    i am following theForger's tutorial and this code won't compile. the error is in case WM_PAINT:
    Code:
    #include <windows.h>
    #include "resource.h"
    
    const char g_szClassName[] = "myWindowClass";
    HBITMAP ship = NULL;
    
    //Step 4: The Window Procedure
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
    	case WM_CREATE:
    		ship = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_SHIP));
    		if(ship == NULL)
    			MessageBox(hWnd, "Could not load ship.bmp!", "Error!", MB_OK | MB_ICONEXCLAMATION);
    		break;
    	case WM_CLOSE:
    		DestroyWindow(hWnd);
    		break;
    	case WM_PAINT:
    		{
            BITMAP bm;
            PAINTSTRUCT ps;
    
            HDC hDC = BeginPaint(hWnd, &ps);
    
            HDC hDCMem = CreateCompatibleDC(hDC);
            HBITMAP hbmOld = SelectObject(hDCMem, ship);
    
            GetObject(ship, sizeof(bm), &bm);
    
            BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hDCMem, 0, 0, SRCCOPY);
    
            SelectObject(hDCMem, hbmOld);
            DeleteDC(hDCMem);
    
            EndPaint(hWnd, &ps);
    		}
            break;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case ID_FILE_EXIT:
    			PostQuitMessage(0);
    			break;
    		case ID_STUFF_GO:
    			MessageBox(hWnd, "You clicked go!", "GO!", MB_OK);
    			break;
    		}
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, Msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hWnd;
    	MSG Msg;
    
    	//Step 1: Registering the Window Class
    	wc.cbSize        = sizeof(WNDCLASSEX);
    	wc.style         = 0;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = hInstance;
    	wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
    	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm       = (HICON)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Windows Regristration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	//Step 2: Creating the Window
    	hWnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		g_szClassName,
    		"Lee's Window",
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
    		NULL, NULL, hInstance, NULL);
    
    	if(hWnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
    	//Step 3: The Message Loop
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    C:\Program Files\Microsoft Visual Studio\MyProjects\Windows Tutorial\main.cpp(28) : error C2440: 'initializing' : cannot convert from 'void *' to 'struct HBITMAP__ *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It should work in C, but if C++, cast to a (HBITMAP)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM