2008年4月2日 星期三

Windows Programming 簡介

Windows Programming Introduction

Contents:

1. Windows History

2. Aspect of Windows

3. Windows Programming with Pure API

(i) A simple code with MessageBox

(ii) A simple code with CreateWindow

Preface

The word “Windows” means that Microsoft Windows OS in the text.

Windows History

1983/11 – Microsoft announce Windows

1985/11 – Microsoft release Windows 1.0

1987/11 – Windows 2.0

1990/05/02 – Windows 3.0 (16-bit protected-mode)

1992/04 – Windows 3.1 (True Type Font, OLE)

1993/07 - Window NT

1995/08 – Windows 95

1998/06 – Window 98

Aspect of Windows

1. Both Window 98 and NT are 32-bit preemptive multitasking and multithreading graphical operation system.

* Earlier versions of Windows used a system of multitasking called “non-preemptive”. This meant that Windows did not use the system timer to slice processing time between the various programs running under the system.

2. Programs running in Windows can share routines that are located in other files called “dynamic-link libraries”. Windows includes a mechanism to link the program with the routines in the dynamic-link libraries.

Kernel: krnl386.exe for 16-bit, and kernel32.dll for 32-bit.

Kernel handles OS kernel traditional routines like memory management, I/O, and tasking.

User: user.exe for 16-bit, and user32.dll for 32-bit

User refers to the user interface, and implements all the windowing logic.



GDI: gdi.exe for 16-bit, and gdi32.dll for 32-bit.

GDI is the Graphics Device Interface, which allows a program to display text and graphics on the screen and printer.

Windows Programming with Pure API


A very simple code with MessageBox

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int nCmdShow)

{

MessageBox(NULL, TEXT("Hello World!"), TEXT("First Example"), MB_OK | MB_ICONINFORMATION);

return 0;

}

1. The Header Files

The “windows.h” is a master include file that includes other Windows header files. And the most important and most basic of these header files are:

windef.h: Basic type definitions

winnt.h: Type definitions for Unicode support.

winbase.h: Kernel functions.

winuser.h: User interface functions.

wingdi.h: Graphics device interface functions.

2. Program Entry Point

WinMain:

hInstance

[in] Handle to the current instance of the application.

hPrevInstance

[in] Handle to the previous instance of the application. This parameter is always NULL. If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first.

lpCmdLine

[in] Pointer to a null-terminated string specifying the command line for the application, excluding the program name. To retrieve the entire command line, use the GetCommandLine function.

nCmdShow

[in] Specifies how the window is to be shown. This parameter can be one of the following values.

See MSDN

A very simple code with CreateWindow

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int nCmdShow)

{

static TCHAR szAppName[] = TEXT("Hello Window");

HWND hWnd;

MSG msg;

WNDCLASS wndclass;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

wndclass.hInstance = hInstance;

wndclass.lpfnWndProc = WndProc;

wndclass.lpszClassName = szAppName;

wndclass.lpszMenuName = NULL;

wndclass.style = CS_HREDRAW | CS_VREDRAW;

if(!RegisterClass(&wndclass))

{

MessageBox(0, TEXT("This program requires Windows NT!"), szAppName, MB_OK | MB_ICONERROR);

return 0;

}// if(!RegisterClass(&wndclass))

hWnd = CreateWindow(szAppName,

TEXT("The Hello Program"),

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL);

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

while(GetMessage(&msg, NULL, 0, 0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}// while(GetMessage(&msg, NULL, 0, 0))

return msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

HDC hDC;

PAINTSTRUCT ps;

RECT rect;

switch(msg)

{

case WM_CREATE:

return 0;

case WM_PAINT:

hDC = BeginPaint(hWnd, &ps);

GetClientRect(hWnd, &rect);

DrawText(hDC, TEXT("Hello World~~~~"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

EndPaint(hWnd, &ps);

return 0;

case WM_DESTROY:

PostQuitMessage(0);

return 0;

}// switch(msg)

return DefWindowProc(hWnd, msg, wParam, lParam);

}

An Architectural Overview

* Remark: “Windows sends a message to the program” is meant that Widnows calls a function within the program – a function that you write and which is an essential part of your program’s code.

1. A windows including sub-windows like button, radio box, check box, and etc. is always created based on a “window class”.

2. When a Windows program begins execution, Windows creates a “message queue” for the program.

3. Message Loop and WndProc

WM_CREATE:

WM_PAINT:

WM_DESTROY:

PostQuitMessage(0) -> return msg.wParam

沒有留言: