#include <Windows.h>
HINSTANCE g_hInstance;
HWND g_hButton;
void OnCreate(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
LPCREATESTRUCT ps = LPCREATESTRUCT(lParam);
//MessageBox(hWnd,ps->lpszName,TEXT(""),MB_OK);
g_hButton = CreateWindowEx(0,TEXT("BUTTON"),TEXT("BUTTON"),WS_CHILD|WS_VISIBLE,
0,0,100,100,hWnd,HMENU(1001),g_hInstance,NULL);
}
void OnSize(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
int nWidth = LOWORD(lParam);
int nHeight = HIWORD(lParam);
int nX = (nWidth - 100) / 2;
int nY = (nHeight - 100) / 2;
MoveWindow(g_hButton,nX,nY,100,100,TRUE);
}
bool OnSysCommand(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (wParam)
{
case SC_CLOSE:
if (IDOK == MessageBox(NULL, TEXT("是否要退出程序?"), TEXT("提示"), MB_OKCANCEL | MB_ICONWARNING))
{
return true;
}
}
return false;
}
void OnCommand(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (wParam)
{
case 1001:
if (IDOK == MessageBox(NULL, TEXT("是否退出程序"), TEXT("提示"), MB_OKCANCEL | MB_ICONWARNING))
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
}
break;
}
}
LRESULT CALLBACK WndProc(HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
OnCreate(hWnd,uMsg,wParam,lParam);
break;
case WM_SIZE:
OnSize(hWnd, uMsg, wParam, lParam);
break;
case WM_SYSCOMMAND:
if (true == OnSysCommand(hWnd, uMsg, wParam, lParam))
{
break;
}
else
{
return 0;
};
//为什么加上这个消息,就不支持窗口拖拽?
//是因为无法处理最大化、最小化窗口,也无法处理拖拽了吗?
case WM_COMMAND:
OnCommand(hWnd,uMsg,wParam,lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
void RegisterWnd(LPCTSTR lpClassName)
{
WNDCLASSEX wce;
wce.style = CS_HREDRAW | CS_VREDRAW;
wce.lpfnWndProc = WndProc;
wce.cbSize = sizeof(WNDCLASSEX);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hInstance = g_hInstance;
wce.hIcon = 0;
wce.hCursor = 0;
wce.hIconSm = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE);
wce.lpszClassName = lpClassName;
wce.lpszMenuName = 0;
if (!RegisterClassEx(&wce))
{
MessageBox(NULL, TEXT("注册失败"), TEXT("提示"), MB_OK);
}
}
HWND CreateWnd(LPCTSTR lpClassName)
{
HWND hWnd = CreateWindowEx(0, lpClassName, L"HELLO",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
return hWnd;
}
void DisplayWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Message()
{
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
g_hInstance = hInstance;
RegisterWnd(L"hello");
HWND hWnd = CreateWnd(L"hello");
DisplayWnd(hWnd);
Message();
return 0;
}