callback function for application window

sorry for the lengthiness of this post.

Page 1 of 1

3 Replies - 1306 Views - Last Post: 18 February 2009 - 01:46 AM Rate Topic: -----

#1 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

callback function for application window

Post icon  Posted 17 February 2009 - 11:53 PM

hi, i'm trying to understand a simple hello world winCE application, but the code is seriously too complicated!

HelloCE.h:
//======================================================================
// Header file
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//================================================================
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0])) 

//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT {							// Structure associates
	UINT Code;								 // messages 
											   // with a function. 
	LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
}; 
struct decodeCMD {							 // Structure associates
	UINT Code;								 // menu IDs with a 
	LRESULT (*Fxn)(HWND, WORD, HWND, WORD);	// function
};

//----------------------------------------------------------------------
// Function prototypes
//
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);

// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

// Message handlers
LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);



HelloCE.cpp:
//======================================================================
// HelloCE - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2007 Douglas Boling
//======================================================================
#include <windows.h>				 // For all that Windows stuff
#include "helloce.h"				 // Program-specific stuff

//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("HelloCE");
HINSTANCE hInst;					 // Program instance handle

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
	WM_PAINT, DoPaintMain,
	WM_DESTROY, DoDestroyMain,
};

//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					LPWSTR lpCmdLine, int nCmdShow) {
	MSG msg;
	int rc = 0;
	HWND hwndMain;

	// Initialize this instance.
	hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
	if (hwndMain == 0) return 0x10;

	// Application message loop
	while (GetMessage (&msg, NULL, 0, 0)) {
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}
	// Instance cleanup
	return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
	WNDCLASS wc;
	HWND hWnd;

	// Save program instance handle in global variable.
	hInst = hInstance;

#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
	// If Windows Mobile, only allow one instance of the application
	hWnd = FindWindow (szAppName, NULL);
	if (hWnd) {
		SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));	
		return 0;
	}
#endif

	// Register application main window class.
	wc.style = 0;							 // Window style
	wc.lpfnWndProc = MainWndProc;			 // Callback function
	wc.cbClsExtra = 0;						// Extra class data
	wc.cbWndExtra = 0;						// Extra window data
	wc.hInstance = hInstance;				 // Owner handle
	wc.hIcon = NULL,						  // Application icon
	wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
	wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
	wc.lpszMenuName = NULL;				   // Menu name
	wc.lpszClassName = szAppName;			 // Window class name

	if (RegisterClass (&wc) == 0) return 0;

	// Create main window.
	hWnd = CreateWindow (szAppName,		   // Window class
						 TEXT("HelloCE"),	 // Window title
						 // Style flags
						 WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
						 CW_USEDEFAULT,	   // x position
						 CW_USEDEFAULT,	   // y position
						 CW_USEDEFAULT,	   // Initial width
						 CW_USEDEFAULT,	   // Initial height
						 NULL,				// Parent
						 NULL,				// Menu, must be null
						 hInstance,		   // Application instance
						 NULL);			   // Pointer to create
											  // parameters
	if (!IsWindow (hWnd)) return 0;  // Fail code if not created.

	// Standard show and update calls
	ShowWindow (hWnd, nCmdShow);
	UpdateWindow (hWnd);
	return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
	return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam, 
							  LPARAM lParam) {
	INT i;
	//
	// Search message list to see if we need to handle this
	// message.  If in list, call procedure.
	//
	for (i = 0; i < dim(MainMessages); i++) {
		if (wMsg == MainMessages[i].Code)
			return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
	}
	return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
					 LPARAM lParam) {
	PAINTSTRUCT ps;
	RECT rect;
	HDC hdc;

	// Get the size of the client rectangle
	GetClientRect (hWnd, &rect);

	hdc = BeginPaint (hWnd, &ps); 
	DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect, 
			  DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	EndPaint (hWnd, &ps); 
	return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam, 
					   LPARAM lParam) {
	PostQuitMessage (0);
	return 0;
}



I understand the registration of the windows class, the creation of a window, but i have problem understanding the part about callback function.


MY QUESTIONS __________________________________
basically, what does this mean:
const struct decodeUINT MainMessages[] = {
WM_PAINT, DoPaintMain,
WM_DESTROY, DoDestroyMain,
};

does it mean that
MainMessages[0].code = WM_PAINT,
*MainMessages[0].Fxn = DoPaintMain,
MainMessages[1].code = WM_DESTROY,
*MainMessages[1].Fxn = DoDestroyMain?

my way of interpreting that is that MainMessages[] is an array, consisting of 4 elements which are of the type decodeUINT.
(this means that DoPaintMain is also a struct?!)

in the for loop:
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}

what does if (wMsg == MainMessages[i].Code) mean?
how does this code work?

Is This A Good Question/Topic? 0
  • +

Replies To: callback function for application window

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: callback function for application window

Posted 18 February 2009 - 12:58 AM

decodeUINT associates a message identifier with a function to process that message.

MainMessages is an array of decodeUINT structures. Each structure is assigned a message identifier and a pointer to a function that processes the message.

Quote

does it mean that
MainMessages[0].code = WM_PAINT,
*MainMessages[0].Fxn = DoPaintMain,
MainMessages[1].code = WM_DESTROY,
*MainMessages[1].Fxn = DoDestroyMain?


Yep, that's correct.

Quote

(this means that DoPaintMain is also a struct?!)


DoPaintMain and DoDestroyMain are both pointers to functions. DoPaint is associated with the WM_PAINT message and DoDestroyMain is associated with a WM_DESTROY message.

Quote

for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}

what does if (wMsg == MainMessages[i].Code) mean?
how does this code work?


wMsg is the message that was sent to the window procedure. wMsg == MainMessages[i].Code checks if the message that was sent to the window matches a message in the MainMessages structure array. If it does, it calls the function that was associated with the message so it can process the message.

For example, if the message was WM_PAINT (wMsg == WM_PAINT),

if (wMsg == MainMessages[i].Code)


will be TRUE for MainMessages[i] where i is 0 because MainMessages[0].code == WM_PAINT:

const struct decodeUINT MainMessages[] = {
	WM_PAINT, DoPaintMain, // MainMessages[0].code == WM_PAINT
	WM_DESTROY, DoDestroyMain,
};


Since a matching message was found, the code calls the message handler that was associated with WM_PAINT (DoPaintMain):

return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
.
Was This Post Helpful? 0
  • +
  • -

#3 redbloodcell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 10-February 09

Re: callback function for application window

Posted 18 February 2009 - 01:17 AM

[quote]

Quote

does it mean that
MainMessages[0].code = WM_PAINT,
*MainMessages[0].Fxn = DoPaintMain,
MainMessages[1].code = WM_DESTROY,
*MainMessages[1].Fxn = DoDestroyMain?


Yep, that's correct.
[/quote

Does this mean that when you declare an array of structures, the elements you list will be automatically recognized pair-by-pair, with each pair corresponding to the message-identifier and pointer-to-function of ONE structure?
so the array index actually goes 0,0,1,1,2,2,3,3,4,4...
Was This Post Helpful? 0
  • +
  • -

#4 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: callback function for application window

Posted 18 February 2009 - 01:46 AM

That's correct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1