Visual C++

Moderators: Lundin
Number of threads: 379
Number of posts: 695

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

Report
Error in Building Posted by VinayKhare on 2 Mar 2009 at 11:33 PM
Hello everybody, I have started learning windows programming few days before. I am referring "Let Us C" for making a plot in my mind, then I will switch to "Charles Petzold". I got some doubt in the following code:
#include <windows.h>

HINSTANCE hInst;

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

BOOL InitInstance ( HINSTANCE, int, char*);

void OnDestroy(HWND);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	MSG m;
	/* perform application initialization */
	InitInstance(hInstance,nShowCmd,"Title");
	/* Message Loop */
	while ( GetMessage(&m,0,0,0) )
		DispatchMessage(&m);
	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		OnDestroy(hWnd);
		break;
	default:
		return ( DefWindowProc(hWnd,message,wParam,lParam) );
	}
	return 0;
}

BOOL InitInstance( HINSTANCE hInstance, int nCmdShow, char* pTitle)
{
	char classname[ ] = "MyWindowClass" ;
	HWND hWnd ;

	WNDCLASSEX wcex ;
	wcex.cbSize			= sizeof ( WNDCLASSEX ) ; 
	wcex.style			= CS_HREDRAW | CS_VREDRAW ;
	wcex.lpfnWndProc	= ( WNDPROC ) WndProc ;
	wcex.cbClsExtra		= 0 ;
	wcex.cbWndExtra	= 0 ;
	wcex.hInstance		= hInstance ;
	wcex.hIcon			= NULL ;
	wcex.hCursor		= LoadCursor ( NULL, IDC_ARROW ) ;
	wcex.hbrBackground	= ( HBRUSH )( COLOR_WINDOW + 1 ) ;
	wcex.lpszMenuName	= NULL ;
	wcex.lpszClassName	= classname ;
	wcex.hIconSm		= NULL ;

	if ( !RegisterClassEx ( &wcex ) )
		return FALSE ;

	hInst = hInstance ; // Store instance handle in our global variable

	hWnd = CreateWindow ( classname, pTitle, 
					WS_OVERLAPPEDWINDOW,
					CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, 
					NULL, hInstance, NULL ) ;
	if ( !hWnd )
	    return FALSE ;
   
	ShowWindow ( hWnd, nCmdShow ) ;
	UpdateWindow ( hWnd ) ;

	return TRUE ;
}

void OnDestroy(HWND hWnd)
{
	PostQuitMessage(0);
}


Q1:-When tracing the above code, I have not passed through the WndProc function. may be in InitInstance(), but after WndProc, "()" is not used(it may be variable).
So I remove def of WndProc from above code.
Code Compiles well but on building the above code two errors occur.
Why so?

Q2:What is that WNDCLASSEX class? where it is defined. and in the above code how it is being used?

Q3: I am bit confused with the Handles. HWND or HINSTANCE.
How many types of handles are there? what are their specific functions??

Thank You,

Waiting for Reply...

Report
Re: Error in Building Posted by Lundin on 3 Mar 2009 at 12:59 AM
: Q1:-When tracing the above code, I have not passed through the
: WndProc function. may be in InitInstance(), but after WndProc, "()"
: is not used(it may be variable).

You only enter WndProc when your window is receiving a message, such as a mouse click. So if you sit in your debugger and stare at the code, the window is likely not receiving any messages, as it will be in the background.


: So I remove def of WndProc from above code.
: Code Compiles well but on building the above code two errors occur.
: Why so?

You must have a WndProc for each window or the code won't work.


: Q2:What is that WNDCLASSEX class? where it is defined. and in the
: above code how it is being used?

That one contains a lot, namely most info about your main window. See the Win API documentation for details.

: Q3: I am bit confused with the Handles. HWND or HINSTANCE.
: How many types of handles are there? what are their specific
: functions??

There are plenty of handles in the Win API. In reality, handle is just a fancy word for pointer. Think of HWND as a pointer to a window and HINSTANCE as a pointer to an application.



 

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.