#include<windows.h>
#pragma comment(lib, "winmm")
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE PrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hellowin");
HWND hwnd; //窗口句柄
MSG msg; //消息结构
WNDCLASS wndclass; //窗口类
wndclass.style = CS_HREDRAW | CS_VREDRAW; //设置窗口风格
wndclass.lpfnWndProc = WndProc; //设置窗口处理程序
wndclass.cbClsExtra = 0; //额外的空间
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; //'程序的实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // 获取自定义的图标,第一个参数设置为NULL,第二个参数是标识图标的的标识符
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //设置鼠标,当鼠标进入客户区时变成箭头,IDC_ARROW为系统预定义的鼠标指针
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //使用 GetStockObject()函数获得白色画刷的句柄
wndclass.lpszMenuName = NULL; //设置不带菜单;
wndclass.lpszClassName = szAppName; //设置窗口类的名字
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要windows NT"), szAppName, MB_ICONERROR);
return 0;
} //注册窗口类
hwnd = CreateWindow(szAppName, //窗口类的名字
TEXT("第二次写这个程序 "), //窗口标题
WS_OVERLAPPEDWINDOW, //设置窗口的风格
CW_USEDEFAULT, //设置窗口显示的初始位置x
CW_USEDEFAULT, //设置窗口显示的初始位置y
CW_USEDEFAULT, //设置窗口的的尺寸x
CW_USEDEFAULT, //设置窗口的尺寸y
NULL, //父窗口句柄
NULL, //菜单句柄
hInstance, //程序的实例句柄
NULL); //创建的参数
//使用CreateWindw()函数创建窗口,并范返回窗口的句柄
ShowWindow(hwnd, iCmdShow); // 显示窗口
UpdateWindow(hwnd); //重绘窗口
while (GetMessage(&msg, NULL, 0, 0)) //从消息队列中获取消息,并填写到消息结构变量msg中
{
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); //使客户区有效
GetClientRect(hwnd, &rect);
DrawText(hdc, TEXT("i writte this for twice"), -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);
}