Windows programming madness (API, Non-MFC)

 

Let's talk about threads.

Processes are programs that are running.

These processes have something called threads.

Unlike DOS, Windows can run many applications at once.

The memory is what determines how many applications can run.

If you have lots of memory, obviously, you can run a lot of applications at once.

Windows manages all these programs in memory using threads.

Threads request CPU time.

Windows places all your threads in a queue, and depending on the

priority of this thread, it will then give it to the CPU, thus executing

the instructions in that thread.

Windows returns a message to you programs, each time it

processes a thread.

 

 

Let's talk about messages.

 

Windows processes all kinds of message, user input, and system messages.

Messages is the heart of Windows programming.

 

 

Okay we are done with messages for now.

MDI ( Multiple Document Interface ) are

applications that can have serveral child windows open.

Ex. In Word, you have have a lot of different word documents

open.

 

Just like the class hierarchy you learned in C++.

Every window in Windows derives from a basic window.

Every application is the son of the Desktop window.

The Desktop window has no parent.

 

Creating a generic Windows program.

1-The program must create one window.

2-The program must register that window with the operating system.

3-The program must handle messages sent from the operating system

to the window.

 

All Windows programs use a resource file, which tells the compiler

the characteristics about the window to create. Ex. Menu items, buttons, etc.

 

Below is the source code for a generic windows application:

 

// A complete windows program

// To create a windows program you need a WinMain, and a CallBack function

// A callback function, is a function called by the operating system.

// Includes

#define WIN32_LEAN_AND_MEAN // just some bullshit

 

#include <windows.h>

#include <windowsx.h>

#include <stdio.h>

#include <math.h>

// Defines

// defines for windows

#define WINDOW_CLASS_NAME "WINCLASS1"

// above is the windows class name

 

// GLOBALS ////////////////////////////////////////////////

HWND main_window_handle = NULL; // save space for the window handle

// For now initialize it to null. Later in the code, we will assign it.

 

 

 

 

 

/******************************************************************************/

/******************************************************************************/

/******************************************************************************/

// Functions

// A call back function is a function that calls itself

// In this callback function, we find the switch case

// for our messages to be handled

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)

{

// this is the main message handler of the system

PAINTSTRUCT ps; // used in WM_PAINT

HDC hdc; // handle to a device context

 

// Below is the message handler

switch(msg)

{

case WM_CREATE:

{

// do initialization

return(0);

} break;

case WM_PAINT:

{

// simply validate the window

hdc = BeginPaint(hwnd,&ps);

EndPaint(hwnd,&ps);

return(0);

} break;

case WM_DESTROY:

{

// kill the application

PostQuitMessage(0);

return(0);

} break;

default:break;

} // end switch

// process any messages that we didn't take care of

return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

/******************************************************************************/

/******************************************************************************/

/******************************************************************************/

 

 

 

 

 

 

 

 

 

 

/******************************************************************************/

/******************************************************************************/

/******************************************************************************/

// WINMAIN ////////////////////////////////////////////////

int WINAPI WinMain( HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)

{

WNDCLASS winclass; // this will hold the class we create

HWND hwnd; // generic window handle

MSG msg; // generic message

// first fill in the window class stucture

winclass.style = CS_DBLCLKS | CS_OWNDC |

CS_HREDRAW | CS_VREDRAW;

winclass.lpfnWndProc = WindowProc;

winclass.cbClsExtra = 0;

winclass.cbWndExtra = 0;

winclass.hInstance = hinstance;

winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

winclass.hCursor = LoadCursor(NULL, IDC_ARROW);

 

/**************Important********************************/

/* one of the two will work on your PC,

just comment out the one, you do not need */

winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

//winclass.hbrBackground = GetStockObject(BLACK_BRUSH);

 

winclass.lpszMenuName = NULL;

winclass.lpszClassName = WINDOW_CLASS_NAME;

 

 

// register the window class

if (!RegisterClass(&winclass))

return(0);

 

 

// create the window

if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class

"Feel the madness, feel the flow, I'm the super-duper GI JOE",// title

WS_OVERLAPPEDWINDOW | WS_VISIBLE,

0,0, // x,y

500,300, // width, height

NULL, // handle to parent

NULL, // handle to menu

hinstance,// instance

NULL))) // creation parms

return(0);/* returns and closes program, if error creating window */

 

 

 

// save the window handle in a global

main_window_handle = hwnd;

// enter main event loop

while(1)

{

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

/** main processing goes here **/

} // end while

// return to Windows like this

return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////

/******************************************************************************/

/******************************************************************************/

/******************************************************************************/

 

/* end of copy, and paste code */

And now to sum things up…

 

1-Have a callback function, with a switch case to handle

the messages. This callback function is your windows procedure.

 

2-Create a WinMain function.

3-You need to create a WNDCLASS structure, and fill it out.

4-Register your WNDCLASS structure.

5-Create the Window.

6-Show the Windows.

7-Have a while(1) loop, for message to be received.

 

Please pardon me if my notes are sloopy, I had to much coffee this morning.

I left a lot of things out, because I already know a lot of stuff.

But, I hope you learned something.