本文是在Dev-Cpp下完成,新建一个Windows Application即可,已经生成好模板。
此处贴上模板程序
#include <windows.h>
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG Msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&Msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&Msg); /* Translate key codes to chars if present */
DispatchMessage(&Msg); /* Send it to WndProc */
}
return Msg.wParam;
}
运行效果如下:
已经做好了一个可直接运行的模板,下面我们为它添加一个要处理的消息就可以了。
Windows应用程序都是消息驱动的,所谓消息驱动机制即围绕消息的产生和处理来运行一个Windows应用程序(个人理解)。
创建一个Windows应用程序的流程就是:(此模板中缺少了3和4)
1.注册窗口类(RegisterClassEx);
2.创建窗口(CreateWindowEx);
3.在桌面上显示窗口(ShowWindow);
4.更新窗口客户区(UpdateWindow);
5.进入无限的消息获取和处理的循环。
我们主要要写的程序就在消息处理函数WndProc里面,这是个回调函数,借用百度百科一句话:
Windows程序在处理消息时使用了“回调函数”的特殊函数。这个函数由应用程序定义,但并不由应用程序来调用,而是供操作系统或者其子系统来调用的。这种调用通常在某一事件发生,或者在窗口或字体被枚举时发生。Windows向程序员所能发送的消息多达百种,但是,对于一般的应用程序来说,只是其中的一部分有意义。
模板程序里已经处理了quit消息,我们在界面上显示消息需要处理WM_PAINT消息,加上如下代码:
#define PI 3.1415926
case WM_PAINT: {
HDC hdc;
PAINTSTRUCT ps;
RECT rt;
int x, y;
GetClientRect( hwnd, &rt );
MoveToEx( hdc, 0, (rt.bottom-rt.top)/2, NULL );
// 绘制横坐标轴
LineTo( hdc, (rt.right-rt.left), (rt.bottom-rt.top)/2 );
for ( x = 0 ; x < rt.right-rt.left ; ++ x )
{
y = (1-sin( 2*PI*(x*1.0/(rt.right-rt.left)) ))*((rt.bottom-rt.top)/2);
LineTo( hdc, x, y );
MoveToEx(hdc, x, y, NULL);
}
EndPaint( hwnd, &ps );
break;
}
运行即可看到正弦曲线:
实现绘图图形的逻辑代码主要是在这个循环里面:
for ( x = 0 ; x < rt.right-rt.left ; ++ x )
{
y = (1-sin( 2*PI*(x*1.0/(rt.right-rt.left)) ))*((rt.bottom-rt.top)/2);
LineTo( hdc, x, y );
MoveToEx(hdc, x, y, NULL);
}
修改代码内的逻辑内容也可以实现其它图形的绘制。