一直比较关注本质的东西。VC封装了很多,想了解一下比较麻烦。
先从了解WinMain()入口函数与WndProc()消息处理函数开始吧。
大气象
#include <windows.h>
#include <mmsystem.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); //声名消息处理函数(处理windows和接收windows消息)
//hInstance:系统为窗口分配的实例号,2和3忘了.4是显示方式
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ; //窗体名
HWND hwnd;//句柄
MSG msg;//消息体
WNDCLASS wndclass;//这义一个窗体类实例
//设置窗体参数
wndclass.style = CS_HREDRAW | CS_VREDRAW ; //样式
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;//窗体实例名,由windows自动分发
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;//显示上面的图标titlte
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;//窗口光标
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;//背景刷
wndclass.lpszMenuName=NULL;
wndclass.lpfnWndProc=WndProc;//设置窗体接收windws消息函数
wndclass.lpszClassName= szAppName;//窗体类名
if (!RegisterClass (&wndclass))//注册窗体类
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
};
//创建一个窗体。已分配内存。返回一个窗体句柄
hwnd = CreateWindow( szAppName, // window class name
TEXT ("The Hello Program"), // 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) ;
ShowWindow (hwnd,iCmdShow);//显示窗口
UpdateWindow (hwnd) ;//更新窗体
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);//翻译消息并发送到windows消息队列
DispatchMessage (&msg) ;//接收信息
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//消息的处理程序
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
//PlaySound(TEXT("HelloWin.wav"),NULL,SND_FILENAME|SND_ASYNC);
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
TextOut(hdc,0,0,"大气象",strlen("大气象"));
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
#include <mmsystem.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); //声名消息处理函数(处理windows和接收windows消息)
//hInstance:系统为窗口分配的实例号,2和3忘了.4是显示方式
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ; //窗体名
HWND hwnd;//句柄
MSG msg;//消息体
WNDCLASS wndclass;//这义一个窗体类实例
//设置窗体参数
wndclass.style = CS_HREDRAW | CS_VREDRAW ; //样式
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;//窗体实例名,由windows自动分发
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;//显示上面的图标titlte
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;//窗口光标
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;//背景刷
wndclass.lpszMenuName=NULL;
wndclass.lpfnWndProc=WndProc;//设置窗体接收windws消息函数
wndclass.lpszClassName= szAppName;//窗体类名
if (!RegisterClass (&wndclass))//注册窗体类
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"), szAppName, MB_ICONERROR) ;
return 0 ;
};
//创建一个窗体。已分配内存。返回一个窗体句柄
hwnd = CreateWindow( szAppName, // window class name
TEXT ("The Hello Program"), // 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) ;
ShowWindow (hwnd,iCmdShow);//显示窗口
UpdateWindow (hwnd) ;//更新窗体
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage (&msg);//翻译消息并发送到windows消息队列
DispatchMessage (&msg) ;//接收信息
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)//消息的处理程序
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
//PlaySound(TEXT("HelloWin.wav"),NULL,SND_FILENAME|SND_ASYNC);
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
TextOut(hdc,0,0,"大气象",strlen("大气象"));
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
新建Visual C++空项目。在源文件目录里新建一个cpp文件。
输入以上代码,直接运行。如图: