📄 1.txt
字号:
Winsdk程序基本框架如下:(一个主入口函数和一个窗口函数)
WINMAIN()
{
申明一个窗口类;
注册该窗口类;
生成一个窗口;
消息循环;
}
WINPROC()
{
针对不同的消息做不同的处理;//switch case default
}
WINMAIN是入口
字符模式C程序中,main()函数是入口,这里WINMAIN函数是入口。
消息循环的结构:
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
3个API函数GetMessage从消息队列取消息、TranslateMessage翻译消息(主要是产生字符消息并再次放到消息队列)、DispatchMessage分发消息到具体的窗口函数让其处理。
消息结构如下:
typedef struct tagMSG
{
HWND hwnd ;
UINT message ;
WPARAM wParam ;
LPARAM lParam ;
DWORD time ;
POINT pt ;
}
MSG, * PMSG ;
。。。。hwnd 接收讯息的视窗代号。在HELLOWIN程式中,这一参数与CreateWindow传回的hwnd值相同,因为这是该程式拥有的唯一视窗。
。。。。message 讯息识别字。这是一个数值,用以标识讯息。对於每个讯息,均有一个对应的识别字,这些识别字定义於Windows表头档案(其中大多数在WINUSER.H中),以字首WM(「window message」,视窗讯息)开头。例如,使用者将滑鼠游标放在HELLOWIN显示区域之内,并按下滑鼠左按钮,Windows就在讯息伫列中放入一个讯息,该讯息的message栏位等於WM_LBUTTONDOWN。这是一个常数,其值为0x0201。
。。。。wParam 一个32位元的「message parameter(讯息参数)」,其含义和数值根据讯息的不同而不同。
。。。。lParam 一个32位元的讯息参数,其值与讯息有关。
。。。。time 讯息放入讯息伫列中的时间。
。。。。pt 讯息放入讯息伫列时的滑鼠座标。
Windows SDK程序的例子来看看我们的框架。
//example.cpp
#include //一定要包含该头文件
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//消息处理函数的声明,定义放在主函数后面
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("Hello");//注册的窗口类的名称,此处为"Hello"
WNDCLASS wndclass;//定义了一个窗口类
//以下为注册一个窗口类
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;//此处关联了定义的消息处理函数
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;//实例句柄
wndclass.hIcon=NULL;//窗口的图标,我们这里不设置
wndclass.hCursor=LoadCursor(NULL,IDC_ARROR);//设置光标
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);//画刷
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;//类名称
//判断是否成功
if(!RegisterClass(&wndclass))//注册窗口
{
MessageBox(NULL,TEXT("窗口注册失败"),szAppName,MB_ICONERROR);
return 0;
}
//下面开始建立窗口
HWND hwnd;
hwnd=CreateWindow(szAppName,TEXT("The Hello Program"),
CW_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
UpdateWindow(hwnd);
//接下来是消息循环,程序不断的从消息队列中取消息,让消息处理函数处理
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//消息处理函数定义
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM)//此例子中我们只是处理简单的消息
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(hwnd,message,wParam,lParam);
}
/***********************************************************************************
一例
*********************************************************************************/
#include "stdafx.h" //注意,这个向导产生的头文件不能去掉
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine//命令行参数, int iCmdShow//显示窗口命令,可以控制窗口的大小等.)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wc ;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = WndProc ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = 0 ;
wc.hInstance = hInstance ;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wc.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wc.lpszMenuName = NULL ;
wc.lpszClassName = szAppName ;
if (!RegisterClass (&wc))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("欢迎你的到来!"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC //pionter to device hdc ;
PAINTSTRUCT ps ;
RECT rect ; //defines the coordinates of the upper-left and lower-right corners of a rectangle
switch (message)
{
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("你好,欢迎你来到VC之路!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -