C/C++ Windows API

Moderators: Lundin
Number of threads: 443
Number of posts: 1215

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
error C2440: '=' : cannot convert from 'HGDIOBJ' to 'HBRUSH' Posted by red888 on 22 Nov 2009 at 12:41 PM
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.




#include <windows.h>

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

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

{
	WNDCLASS WndClass;

	WndClass.style = 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";

	RegisterClass(&WndClass);

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

	ShowWindow (hWindow, SW_SHOW);

	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); //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;
		
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	default:
		return DefWindowProc(hWnd,
							 uiMessage,
							 wParam,
							 lParam);
	}
}

Report
Window does not display Posted by red888 on 28 Nov 2009 at 7:27 PM
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.
Report
Re: Window does not display Posted by untio on 29 Nov 2009 at 10:58 AM
Hi,
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.
Report
Re: Window does not display Posted by red888 on 30 Nov 2009 at 8:58 PM
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.
Report
Re: Window does not display Posted by AsmGuru62 on 1 Dec 2009 at 5:18 AM
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.
Report
Re: Window does not display Posted by untio on 2 Dec 2009 at 9:23 AM
Hi,
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.


Report
Re: Window does not display Posted by red888 on 2 Dec 2009 at 11:46 AM
Thanks for the response that explains a lot!

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

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        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.
Report
Re: Window does not display Posted by red888 on 2 Dec 2009 at 11:54 AM
Thanks for the response that explains a lot!

hInstance = 0; was a typo on my part.

I guess this is why stuff like:
    if(!RegisterClassEx(&WndClass))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        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.
Report
Re: Window does not display Posted by AsmGuru62 on 3 Dec 2009 at 5:34 AM
That's all good, but what is really neccessary is that when declaring your structure you should set all fields of it to zero:
WNDCLASS wc = {0};

This sets the hIcon 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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.