Windows API最简单的程序
//首先搭建框架;一般窗口框架:1:注册窗口类,2:创建窗口 3:更新窗口,4:显示窗口;
//捕获鼠标,设置鼠标过滤器;分别响应鼠标的事件;
//头文件
#include "windows.h"
//全局变量
HINSTANCE hInst;
//函数声明
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd );
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
//WinMain方法。
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd )
{
//变量声明:消息变量,注册窗口类的结构,窗口句柄;
MSG msg;
HWND hwnd;
WNDCLASSEX wcx;
BOOL fGotMessage;
//变量赋值
hInst = hInstance;
//注册窗口类
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW |CS_VREDRAW;
wcx.lpfnWndProc = MainWndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL,IDC_ARROW);
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "MainWClass";
wcx.hIconSm = (HICON)LoadImage(hInstance,
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
if (!RegisterClassEx(&wcx))
{
//显示错误信息;
return (FALSE);
}
//创建窗口
hwnd = CreateWindow("MainWClass","鼠标跟踪窗口",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
(HWND)NULL,(HMENU)NULL,
hInstance,
(LPVOID)NULL);
if (!hwnd)
{
//显示错误信息;
return (FALSE);
}
//显示窗口
ShowWindow(hwnd,nShowCmd);
//更新窗口;
UpdateWindow(hwnd);
//消息回路的设计
while(fGotMessage = GetMessage(&msg,(HWND)NULL,0,0) !=0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//消息处理函数回调方法.
LRESULT CALLBACK MainWndProc(HWND hwnd,
UINT uMsg,
WPARAM wparam,
LPARAM lparam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd,uMsg,wparam,lparam);
}
}
//俩个必不可少的方法:WinMain()和MainWndProc();