2 Replies - 918 Views - Last Post: 21 February 2012 - 04:30 AM Rate Topic: -----

#1 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Passing WNDPROC as a function parameter question

Posted 13 February 2012 - 08:38 AM

Hi, i'm currently messing around with Win32 programming to get a better understanding of how it all works, Currently i'm looking into child windows and interaction with the main application and have come to a problem. I'm wishing to have a separate WNDPROC function for the child windows so that on the closing of one of the children it doesn't end the application from the PostQuitMessage call in the main windows WndProc function from the WM_DESTROY message call. What I have so far is a function to create windows where you can specify if it has a parent or not but trying to add an extra parameter so that you can attach a separate WNDPROC to the windows of your choice, the only problem though is that on running the application it doesn't seem to have linking the WndProc call to the windows and from searching I've failed to find a solution to this problem. Below is the current code that I have (not Presently I only have one WndProc function till I get it actually working again):

/* file: main.cpp */
/* This is a demonstration of Win32 Programming */

/* Includes */
#include <windows.h>

/* Function prototyes */
HWND Window( __in WNDPROC, __in_opt HWND Parent, __in char* title, __in char* cName, __in HINSTANCE, __in int x, __in int y, __in int width, __in int height, __in DWORD style, __in DWORD StyleEx);
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

/* WinMain - the entry point to the application */
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){
	MSG Msg = {0};	// the message handler for the main application
	MSG cMsg = {0};	// the child window message handler

	/* Create an instance of the window */
	//hWnd = CreateMainWindow("Win32 01", "MyWindow", hInstance, 0, 0, 640,480);
	HWND hWnd = Window( WndProc, NULL, "Main Window", "MyWin32", hInstance, 0, 0, 640, 480, WS_OVERLAPPEDWINDOW, WS_EX_CLIENTEDGE);

	/* Create a child window */
	HWND Child1 = Window( NULL, hWnd, "Child Window 1", "MyWin32", hInstance, 0, 0, 312, 225, WS_CHILDWINDOW, WS_EX_STATICEDGE);
	HWND Child2 = Window( NULL, hWnd, "Child Window 2", "MyWin32", hInstance, 312, 0, 312, 225, WS_CHILDWINDOW, WS_EX_STATICEDGE);
	HWND Child3 = Window( NULL, hWnd, "Child Window 3", "MyWin32", hInstance, 0, 225, 312, 225, WS_CHILDWINDOW, WS_EX_STATICEDGE);
	HWND Child4 = Window( NULL, hWnd, "Child Window 4", "MyWin32", hInstance, 312, 225, 312, 225, WS_CHILDWINDOW, WS_EX_STATICEDGE);
		

	while(Msg.message != WM_QUIT)
	{
		// If there are Window messages for the main window then process them.
		if(PeekMessage( &Msg, hWnd, 0, 0, PM_REMOVE ))
		{
            TranslateMessage( &Msg );
            DispatchMessage( &Msg );
		}
		// Otherwise, Process the child windows.
		else
        {	
			
        }
	}
	
	return Msg.wParam;
}

/* Function Definitions */
HWND Window( __in WNDPROC WndProcr, __in_opt HWND Parent, __in char* title, __in char* cName, __in HINSTANCE hInstance, __in int x, __in int y, __in int width, __in int height, __in DWORD Style, __in DWORD StyleEx){
	HWND hWnd;
	WNDCLASSEX wcex;	// the window class descriptor

	/* Firstly fill out the Window class structure */
	wcex.cbSize = sizeof(WNDCLASSEX);		// the size of the structure
	wcex.cbClsExtra = 0;					// no extra bytes
	wcex.cbWndExtra = 0;					// no extra bytes for the window
	wcex.style = CS_HREDRAW | CS_VREDRAW;	// style of the window
	wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hIconSm = LoadIcon( NULL, IDI_WINLOGO );
	wcex.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
	wcex.hInstance = hInstance;
	wcex.lpfnWndProc = WndProcr;
	wcex.lpszClassName = cName;
	wcex.lpszMenuName = NULL;

	RegisterClassEx(&wcex);

	/* Create the window */
	hWnd = CreateWindowEx(	StyleEx,
        					cName,
        					title,
        					Style,
        					x, y,
							width, height,
        					Parent, NULL,
							hInstance,
							NULL);

	if( !hWnd ){
		MessageBox( NULL, "Creation of window Failed.", "Error", MB_OK | MB_IConerror );
		return NULL;
	}

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	return hWnd;
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
	switch (uMsg){
	case WM_CREATE:	break;
	case WM_CLOSE:	DestroyWindow(hWnd); break;
	case WM_DESTROY: PostQuitMessage(0); break;
	case WM_KEYDOWN: if( wParam == VK_ESCAPE ) 
						 PostQuitMessage(0);
						break;

	default: return DefWindowProc(hWnd, uMsg, wParam, lParam); break;
	}
	return 0;
}



Please advise.
Regards
Dave

Is This A Good Question/Topic? 0
  • +

Replies To: Passing WNDPROC as a function parameter question

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1344
  • View blog
  • Posts: 4,606
  • Joined: 19-February 09

Re: Passing WNDPROC as a function parameter question

Posted 14 February 2012 - 10:40 PM

The problem seems to be that you are trying to re-register the same class name.
Was This Post Helpful? 1
  • +
  • -

#3 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Passing WNDPROC as a function parameter question

Posted 21 February 2012 - 04:30 AM

sorry for the delay in reply, thanks for the reply it most likely is that, I need to pay more attention next time. Thanks again though :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1