1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
//---------------------------------------------------------------------------
#include <windows.h>
//===========================================================================
long
__stdcall WndProc(
HWND
hwnd,
UINT
msg,
WPARAM
WParam,
LPARAM
LParam)
{
switch
(msg)
{
case
WM_CLOSE:
PostQuitMessage(0);
break
;
case
WM_LBUTTONDOWN:
MessageBoxA(0,
"ok"
,
"提示"
,0);
PostQuitMessage(0);
break
;
case
WM_PAINT:
{
HDC
hdc;
hdc = GetDC(hwnd);
RECT rect;
GetClientRect(hwnd,&rect);
rect.left = rect.right/2;
rect.top = rect.bottom/2;
rect.right = rect.left+50;
rect.bottom = rect.top+30;
DrawText(hdc,
"hello"
,5,&rect,0);
ReleaseDC(NULL,hdc);
}
default
:
return
DefWindowProc(hwnd,msg,WParam,LParam);
}
return
0;
}
WINAPI WinMain(
HINSTANCE
hInstance,
HINSTANCE
hPreHinstance,
LPSTR
,
int
)
{
g_hIns = hInstance;
WNDCLASS theClass ={0};
char
* lpszClassName =
"myClass"
;
char
* lpszTitle =
"测试窗口"
;
HWND
hwnd;
MSG msg;
theClass.style = 0;
//缺省窗口风格
theClass.lpfnWndProc = WndProc;
theClass.cbClsExtra = 0;
//窗口类无扩展
theClass.cbWndExtra = 0;
//窗口实例无扩展
theClass.hInstance = hInstance;
//窗口的最小化图标为缺省图标,即窗口左上角图标
//VC中资源ID为IDI_APPLICATION
//在BCB中资源名为MAINICON
theClass.hIcon=LoadIcon(hInstance,
"MAINICON"
);
theClass.hCursor=LoadCursor(NULL,IDC_ARROW);
//窗口采用箭头光标
theClass.hbrBackground=(
HBRUSH
)(GetStockObject(WHITE_BRUSH));
//窗口背景为白色
theClass.lpszMenuName=NULL;
//窗口无菜单
theClass.lpszClassName= lpszClassName;
//窗口类名为"窗口"
if
(!RegisterClass(&theClass))
//如果注册失败 发出警告
{
MessageBeep(0);
return
FALSE;
}
hwnd=CreateWindow( lpszClassName,
//窗口类名
lpszTitle,
//窗口标题名
WS_OVERLAPPEDWINDOW,
//窗口的风格
CW_USEDEFAULT,
//窗口左上角坐标值为缺省值
CW_USEDEFAULT,
500, 300,
//窗口的宽和高
NULL,
//此窗口无父窗口
NULL,
//此窗口无子菜单
hInstance,
//创建此窗口的应用程序的当前句柄
NULL
//不使用该值
);
ShowWindow(hwnd,SW_SHOW);
UpdateWindow(hwnd);
while
(
true
)
{
if
(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if
(msg.message == WM_QUIT)
break
;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// if (GetMessage (&msg, NULL, 0, 0))
// {
// TranslateMessage (&msg) ;
// DispatchMessage (&msg) ;
// }
// else
// break;
}
return
msg.wParam;
//消息循环结束 即程序结束时 将信息返回系统
}
|
PeekMessage与GetMessage函数处理不相同,两种方式都可以.PeekMessage是不阻塞的,GetMessage是线程阻塞的,内部处理退出消息,所以不用判断退出消息,关闭时,就跳到break结束循环。要在WM_PAINT消息中绘图,可用如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
PAINTSTRUCT ps ={0};
HDC
hdc = BeginPaint(hwnd,&ps);
//HPEN hPen = GetStockObject(WHITE_PEN);
HPEN
hPen = CreatePen(PS_DASHDOTDOT,1,RGB(255 , 0 , 0 ));
HGDIOBJ
oldPen = (
HGDIOBJ
)SelectObject(hdc,hPen);
if
(!oldPen)
{
int
err = GetLastError();
char
buff[20]={0};
itoa(err,buff,10);
OutputDebugStringA(buff);
}
//画矩形
Rectangle(hdc,20,30,80,120);
//画线
MoveToEx ( hdc , 20 , 10 , NULL );
LineTo( hdc , 200 ,100);
SelectObject(hdc, oldPen);
DeleteObject(hPen);
EndPaint(hwnd,&ps);
|
本文转自Chinayu201451CTO博客,原文链接:http://blog.51cto.com/9233403/1967790
,如需转载请自行联系原作者