Thread: I copied it straight from a tutorial and it still won't work

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    I copied it straight from a tutorial and it still won't work

    The following code is copied straight from a tutorial and still gives errors!!

    #include "stdafx.h"

    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    WNDCLASS WndClass;
    WndClass.style = 0;
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.lpfnWndProc = WndProc;
    WndClass.hInstance = hInstance;
    WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW-5);
    WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    WndClass.lpszMenuName = 0;
    WndClass.lpszClassName = "WinProg";

    RegisterClass(&WndClass);
    HWND hWindow;
    hWindow = CreateWindow("WinProg","Donk2",WS_OVERLAPPEDWINDOW ,0,0,600,400,NULL,NULL,hInstance,NULL);

    ShowWindow(hWindow,nCmdShow);
    UpdateWindow(hWindow);

    MSG Message;
    while(GetMessage(&Message,NULL,0,0))
    {
    DispatchMessage(&Message);
    }
    return(Message.wParam);
    }

    LRESULT CALLBACK WndProc(HWND hWnd,UINT uiMessage,WPARAM wParam,LPARAM lParam)
    {
    switch(uiMessage)
    {
    case WM_PAINT:
    HPEN hPen;
    HPEN hPenalt;
    HBRUSH hBrush;
    HBRUSH hBrushalt;
    hBrush = CreateSolidBrush(RGB(255,100,0));
    hPen = CreatePen(PS_SOLID,2,RGB(0,255,255));
    HDC hdc;
    PAINTSTRUCT ps;
    hdc = BeginPaint(hWnd,&ps);
    hBrushalt = SelectObject(hdc,hBrush);
    hPenalt = SelectObject(hdc,hPen);
    MoveToEx(hdc,20,20,NULL);
    LineTo(hdc,100,100);
    Rectangle(hdc,120,20,240,140);
    RoundRect(hdc,260,20,420,140,20,20);

    RECT rect;
    SetRect(&rect,20,260,240,420);
    FrameRect(hdc,&rect,hBrush);
    SetRect(&rect,260,260,420,420);
    FillRect(hdc,&rect,hBrush);
    Ellipse(hdc,440,260,480,420);
    SelectObject(hdc,hBrushalt);
    SelectObject(hdc,hPenalt);
    DeleteObject(hPen);
    DeleteObject(hBrush);
    EndPaint(hWnd,&ps);
    return 0;

    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    default:
    return DefWindowProc(hWnd,uiMessage,wParam,lParam);
    }
    }

    Errors:
    Compiling...
    window2.cpp


    C:\Program Files\Microsoft Visual Studio\MyProjects\window2\window2.cpp(54) : error C2440: '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast



    C:\Program Files\Microsoft Visual Studio\MyProjects\window2\window2.cpp(55) : error C2440: '=' : cannot convert from 'void *' to 'struct HPEN__ *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Error executing cl.exe.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    use a type cast like (void*)hPen or (void*)hBrush on the offending lines
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    thanks a lot

    ..... could you give me an example - that didn't seem to work
    Last edited by Donk; 03-03-2002 at 01:15 AM.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    The error msg tells you exactly what to do and how to correct it. If you highlight the error in msvc++ and hit F1 then you will get a fuller description of the error and what it means.

    need to cast to HBRUSH then:

    (HBRUSH)variable;

    etc. (look up c++ casting too as it gives more rigorous approaches)

    If you look up the functions on msdn that are giving you problems, this will also help.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    I didn't install MSDN correctly...

    So where am I supposed to put the (HBRUSH) type-cast?

    I know how to debug, I've tried type casting everything I can think of as (HBRUSH) and then as (void*)but still get errors.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I havent loaded you code, but from what I can see;

    Code:
    hBrushalt = SelectObject(hdc,hBrush);
    hPenalt = SelectObject(hdc,hPen);
    should be changed to

    Code:
    hBrushalt = (HBRUSH)SelectObject(hdc,hBrush);
    hPenalt = (HPEN)SelectObject(hdc,hPen);
    The cause is that SelectObject() returns HGDIOBJ (Handle to GDI Object) and so therefore is not specific (As it is used for multiple GDI objects) and needs casting to the structure you have instantised.......

    There's loads of casting in WinAPI as it is C-Based...

    "oh what I would do for operating overloading in this situation...."

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    8

    Yeah thanks

    I just figured that out about 10 mins ago. I was gonna post that I found the solution when I see that you beat me to it

    Thanks a lot for the help.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Use .c (not .cpp) source files and never have the same problem again.
    "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

Popular pages Recent additions subscribe to a feed