Monday, October 17, 2005

Windows Programming

Lecture Notes (open in IE)

Comparison between command line (dos) C++ and Windows version

// std. version

#include
//entry point
void main (void)
{
printf(:\n There can be only one!!!");
}


// Windows version

//simple message box

#define WIN32_LEAN_AND_MEAN
#include //main header
#include //lot of cool macros

//main entry point for all windows programs
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
//call message box API with NULL for parent

//window handle

parent message string title
| | |
MessageBox (NULL,"There can be only one!!!", "My first windows program", MB_OK|MB_ICONEXCLAMATION); <--- style options

//exit
return(0);

} // end WinMain


Window class

everything is a window , including buttons and toolbars

WNDCLASSEX winclass; // holds the class that we create
HWND handle; // generic window handle
Msg message; // generic message

Declare windows fields

Register window class

Create windows

Display window - ShowWindow(hwnd, SW_SHOW); , UpdateWindow()

Event Handler

Processing Messages

case WM_CREATE:
{
// do initialization stuff here

return(0); //return success
} break;

case WM_PAINT:
{
// simply validate the window
hdc=BeginPaint(hwnd, &ps);

// you would do all your painting here
EndPaint(hwnd, &ps);

return(0); // return success
} break;

case WM_DESTROY:
{
// kill the application, this sends the WM_QUIT message
PostQuitMessage(0);
return(0); // return success
}break;

default: break;
} //end switch

Main Event Loop

// enter main event loop

while(GetMessage(&msg,NULL,0,0))
{
// Translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);

} // end while

// return to the Windows desktop like this
return (msg.wParam);


Different Main Event Loop

while(TRUE)
{
// if there is a message in the queue, get it
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)
{
// test if this is a quit
if (msg.message==WM_QUIT)
break;

//translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);

} // end if

} // end while


Handle the windows event messages yourself by adding sections to the switch statement in the callback (WinProc). Eg. close window, force repaint, kill application

Postmessage - send to queue for normal processing
Postmessage(hwnd, WM_USER, w, l);

Sendmessage - send message to window bypassing to queue and requiring immediate response
Sendmessage(hwnd, WM_USER, w, l);

wparam and lparam are parameters and will contain information depending on the message being sent to the window

Movable Window

case WM_MOVE:
{
// extract the position
int xpos = LOWORD (lparam);
int ypos = HIWORD (lparam);

// get a graphics context
hdc=GetDC(hwnd);

// set the foreground color to green
SetTextColor(hdc, RGB(0,255,0));

// set the background color to white
SetBkColor(hdc, RGB(255,255,255));

// set the transparency mode to Opaque
SetBkMode(hdc, OPAQUE);

// draw the size of the window
sprintf(buffer, "WM_MOVE called - New Position = (%d,%d)",xpos, ypos);

TextOut(hdc,0,0,buffer, strlen(buffer));

//release the dc back
ReleaseDC (hwnd,hdc);

} break;

Keyboard

To process the key presses, use the message WM_CHAR

wparam = ASCII character code
lparam = bit code for key state

case WM_CHAR:
{
// get the character
char ascii_code = wparam;
unsigned int key_state = lparam;

//get a graphics context
hdc=GetDC(hwnd);

//set the foreground color to cyan
SetTextColor(hdc, RGB(0.255,255));

//set the background color to black
SetBkColor(hdc,RGB(0,0,0));

//set the transparency mode to OPAQUE
SetBkMode(hdc,OPAQUE);

//print the ascii code and key state
sprintf(buffer, "WM_CHAR: Character = %c", ascii_code);
TextOut(hdc,0,0,buffer,strlen(buffer));

//release the dc back
ReleaseDC(hwnd, hdc);
}break;

You can bypass the message passing state by using GetAsynchKeyState()

case WM_KEYDOWN:
{
int virtual_code = (int) wparam;
int key_bits = (int) lparam;

switch(virtual_code)
{
case VK_RIGHT:
case VK_LEFT:
default:
} // end switch

} // end case


for WM_MOUSEMOVE, wparam - button state bits, lparam - loword is x position, hiword is y position










0 Comments:

Post a Comment

<< Home