//WinMain.cpp
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinAzeProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
//设计一个窗口类
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);
wndcls.hIcon = LoadIcon(NULL, IDI_ERROR);
wndcls.hInstance = hInstance; //应用程序实例句柄由WinMain函数传进来
wndcls.lpfnWndProc = WinAzeProc;
wndcls.lpszClassName = "aze_003";
wndcls.lpszMenuName = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); //注册窗口类
//创建窗口,定义一个变量用来保存成功创建后返回的句柄
HWND hwnd;
hwnd = CreateWindow("aze_003", "first Application", WS_OVERLAPPEDWINDOW, 0, 0, 600, 500, NULL, NULL,hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL); //显示窗口
UpdateWindow(hwnd); //刷新窗口
//定义消息结构体,开始消息循环
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//编写窗口过程函数
LRESULT CALLBACK WinAzeProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[20];
sprintf(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, "char", 0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked", "message", 0);
HDC hdc;
hdc = GetDC(hwnd); //不能在响应WM_PAINT消息时调用
TextOut( hdc, 0, 50, "程序员之家!",strlen("程序员之家!") );
ReleaseDC(hwnd, hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(hwnd, &ps); //BeginPaint只能在响应WM_PAINT消息是调用
TextOut(hDC, 0, 0, "http://www.sunxin.org", strlen("http://www.sunxin.org"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if( IDYES == MessageBox(hwnd, "是否真的退出?", "message", MB_YESNO) )
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
//The MSG structure contains message information from a thread's message queue.
typedef struct tagMSG {
HWND hwnd; //消息所属的窗口,消息都是与窗口相关联的
UINT message; //the message identifier
WPARAM wParam; //指定消息的附加消息
LPARAM lParam; //指定消息的附加消息
DWORD time; //消息投递到队列中的时间
POINT pt; //鼠标的当前位置
} MSG, *PMSG;
//The WinMain function is called by the system as the initial entry point for a Windows-based application. int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance当前窗口句柄 HINSTANCE hPrevInstance, // handle to previous instance前一个打开的窗口句柄 LPSTR lpCmdLine, // command line 指定传递给应用程序的*命令行*参数 int nCmdShow // show state 指定窗口应该如何显示,如:最大(小)化、隐藏等 );
typedef struct _WNDCLASS {
UINT style; //指定*这一类型*窗口的样式,如:CS_HREDRAW、CS_VREDRAW、CS_NOCLOSE、CS_DBLCLKS
WNDPROC lpfnWndProc; //函数指针,指向窗口过程函数(窗口过程函数是一回调函数)
int cbClsExtra; //一般为0
int cbWndExtra; //同上
HINSTANCE hInstance; //指定包含窗口过程的程序的实例句柄
HICON hIcon; //指定窗口类的图标句柄
HCURSOR hCursor; //指定窗口类的光标句柄
HBRUSH hbrBackground; //指定窗口类的背景画刷句柄;当窗口发生重绘值,系统使用这里指定的画刷来查处窗口的背景
LPCTSTR lpszMenuName; //指定菜单资源的名字 **菜单并不是一个窗口**
LPCTSTR lpszClassName; //指定窗口类的名字
} WNDCLASS, *PWNDCLASS;
typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM); //LRESULT=long, CALLBACK=_stdcall WNDPROC是函数指针类型。
//The GetStockObject function retrieves a handle to one of the stock pens, brushes, fonts, or palettes. HGDIOBJ GetStockObject( int fnObject // stock object type );
wndcls.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
ATOM RegisterClass(
CONST WNDCLASS *lpWndClass // class data, 窗口类对象的指针
// Pointer to a WNDCLASS structure. You must fill the structure with the appropriate class attributes before passing it to the function.
);
HWND CreateWindow( LPCTSTR lpClassName, // registered class name 即:窗口类WNDCLASS的lpszClassName成员指定的名称(必须先注册) LPCTSTR lpWindowName, // window name 指定窗口的名字 DWORD dwStyle, // window style 指定创建窗口的样式 如:WS_OVERLAPPEDWINDOW int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent or owner window 指定被创建窗口的父窗口句柄 HMENU hMenu, // menu handle or child identifier HINSTANCE hInstance, // handle to application instance LPVOID lpParam // window-creation data 作为WM_CREATE消息的附加参数lParam传入的数据指针(一般为:NULL) );
BOOL ShowWindow( HWND hWnd, // handle to window 该参数为成功创建窗口后返回的那个窗口句柄 int nCmdShow // show state 如:SW_HIDE、SW_SHOW、SW_SHOWNORMAL、SW_SHOWMINIMIZED、SW_SHOWMAXIMIZED·· );
BOOL UpdateWindow( HWND hWnd // handle to window 创建成功后的窗口句柄 );
BOOL GetMessage( LPMSG lpMsg, // message information 指向一个消息(MSG)结构体,GetMessage从线程的消息队列中取出的消息信息将保存在该结构体对象中 HWND hWnd, // handle to window 指定接收属于哪一个窗口的消息;NULL:用于接收属于调用线程的所有窗口的窗口消息 UINT wMsgFilterMin, // first message 指定获取打的消息的最小值 UINT wMsgFilterMax // last message 如果wMsgFilterMin=0和wMsgFilterMax=0,则接收所有消息 );
//消息循环代码,一般形式
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg); //TranslateMessage函数将虚拟键消息*转换*为字符消息,被投递到调用线程的消息队列中,当下一次调用GetMessage函数时被取出
DispatchMessage(&msg); //DispatchMessage函数分派一个消息到窗口过程,有窗口过程函数对消息进行处理
//DispatchMessage实际上是将消息会传给操作系统,有操作系统调用窗口过程函数对消息进行处理(响应)
}
BOOL PeekMessage( LPMSG lpMsg, // message information HWND hWnd, // handle to window UINT wMsgFilterMin, // first message UINT wMsgFilterMax, // last message UINT wRemoveMsg // removal options );
LRESULT CALLBACK WindowProc( //窗口过程函数的名字可以随便取,如:WinAzeProc,但函数声明与定义要一致; HWND hwnd, // handle to window UINT uMsg, // message identifier 消息代码 WPARAM wParam, // first message parameter 消息代码的两个附加值 LPARAM lParam // second message parameter );
//编写窗口过程函数
LRESULT CALLBACK WinAzeProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR: //通过调用TranslateMessage函数转换得到
char szChar[20];
sprintf(szChar, "char code is %d", wParam);
MessageBox(hwnd, szChar, "char", 0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd, "mouse clicked!", "message", 0);
HDC hdc;
hdc = GetDC(hwnd); //用hdc保存GetDC函数返回的与特定窗口相关联的DC的句柄。
//GetDC()不能在响应WM_PAINT消息时调用
TextOut( hdc, 0, 50, "程序员之家!",strlen("程序员之家!") ); //TextOut利用得到的DC句柄在指定的位置(0,50)出输出一行文字
ReleaseDC(hwnd, hdc); //释放hdc
break;
case WM_PAINT: //当窗口客服区的一部分或者全部变为“无效”时,系统会发送WM_PAINT消息,通知应用程序重新绘制窗口
//窗口刚创建时,客户区是无效状态,当调用UpdateWindow函数时,会发送WM_PAINT消息给窗口过程,对窗口进行刷新
//当窗口从无到有、改变尺寸、最小化在恢复、被其他窗口遮盖后在显示时,窗口的客户区都将变为无效,此时系统会给应用程序发送WM_PAINT消息,通知应用程序重新绘制
//提示:窗口大小发生变化时,是否发生重绘,取决于WNDCLASS结构体中style成员是否设置了CS_HREDRAW和CS_VREDRAW标志
HDC hDC;
PAINTSTRUCT ps; //ps用于接收绘制的信息
hDC = BeginPaint(hwnd, &ps); //BeginPaint只能在响应WM_PAINT消息是调用
TextOut(hDC, 0, 0, "http://www.sunxin.org", strlen("http://www.sunxin.org"));
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if( IDYES == MessageBox(hwnd, "是否真的退出?", "message", MB_YESNO) )
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam); //DefWindowProc调用默认的窗口过程,对应用程序没有处理的其他消息提供默认处理。
//对于大多数的消息,应用程序可以直接调用DefWindowProc函数进行处理。
//在编写窗口过程时,应将DefWindowProc函数的调用放到default语句中,并将该函数的返回值作为窗口过程函数的返回值。
}
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有