Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


error C2440: '=' : cannot convert from 'HGDIOBJ' to 'HBRUSH'

I'm learning so pretty sure this is a basic/easy fix...

I copied this right out of a (relatively dated) Addison Wesley book. I have seen similar problems like this when googling but none that led me to figure out this specific problem.



WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)

WNDCLASS WndClass; = 0;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = 0;
WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);//(HBRUSH) (COLOR_WINDOW+1);
WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "WinProg";


HWND hWindow;
hWindow = CreateWindow("WinProg", "Window",
0,0, 600,460, NULL,NULL,

ShowWindow (hWindow, SW_SHOW);

UpdateWindow (hWindow);

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

UINT uiMessage,
WPARAM wParam,
LPARAM lParam)
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;
hdc = BeginPaint (hWnd,&ps);
hBrushalt = SelectObject (hdc, hBrush); //errors here
hPenalt = SelectObject (hdc, hPen); //and here
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;

return 0;
return DefWindowProc(hWnd,


  • red888red888 Posts: 25
    Well in case another beginner has experienced this, it was an easy fix. Strict type checking is on by default in VS so reinterpret_cast solved my conversion problem-
    ex: hBrushalt = (HBRUSH)SelectObject(hdc, hBrush);

    Now I have another problem. The app compiles and runs but nothing displays. I can see the process, yet it is invisible.

    Thanks for any responses.
  • untiountio Posts: 14
    Probably you are stressed. You have missed:
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    Before RegisterClass. Othewise, the hIcon contains, probably, an invalid value.

    I hope that this can be useful.
  • red888red888 Posts: 25
    Yes! I figured that out earlier today when I looked at it again and found that retarded mistake, I should have looked at it more carefully but I am glad this did happen because now I have to ask this question : why does leaving out the icon field results in this behavior? I mean why don't A: I get an error message from the compiler, or B: have the window display, just without icon? How come it runs, but does not appear (I actually think I see it flash on the screen).

    Thanks for the response by the way.
  • AsmGuru62AsmGuru62 Posts: 6,519
    [color=Blue]If this was copied from a book - I suggest that you get another book: Petzold's "Programming Windows" for example.

    1. WndClass.hInstance = 0;

    I am not sure why it even works. The instance in WNDCLASS must be set to hInstance (1st parameter) from WinMain().

    2. Your message loop is missing TranslateMessage() call - it only has DispatchMessage(). That means if you want to get messages from keyboard into this application (like WM_CHAR) - they will never come.[/color]
  • untiountio Posts: 14
    You are right and I thinked that the window did not work mainly for the instance mistake, but the program runs and the serious error was in the icon handle.

    The problem when hIcon is not defined is that then the class is not registered with RegisterClass and this function returns zero. This condition is not tested and the aplication continues but it does not runs right.

  • red888red888 Posts: 25
    Thanks for the response that explains a lot!

    To test this out I commented out the hicon line and did some simple error checking:

    MessageBox(NULL, "Window Registration Failed!", "Error!",
    return 0;

    and got my own little error message popup. I actually copied the above code from another tut- until now I didn't really get why I needed this.
    As for the book... I looked ahead and translateMessage as well as the instance being use is there, it looks like its just building up to it or something.
  • red888red888 Posts: 25
    Thanks for the response that explains a lot!

    hInstance = 0; was a typo on my part.

    I guess this is why stuff like:
    MessageBox(NULL, "Window Registration Failed!", "Error!",
    return 0;
    is necessary.

    As for the book... I looked ahead and translateMessage is explained and used eventually, it looks like its just building up to it or something.
  • AsmGuru62AsmGuru62 Posts: 6,519
    [color=Blue]That's all good, but what is really neccessary is that when declaring your structure you should set all fields of it to zero:[/color]
    WNDCLASS wc = {0};
    [color=Blue]This sets the [b]hIcon[/b] field to zero and then Windows provides its default application icon for this window. Another advantage is that you do not need to zero out each member separately later in code - it is all done in one "={0};" statement by a compiler. The final code will be smaller.[/color]
Sign In or Register to comment.