#include <windows.h>
#include <strsafe.h>
/*
任务:去掉标题栏和边框
*/
//#define LineHeight 15 这是自己猜测的行高,不要这样做
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
static TCHAR szClassName[] = TEXT("MyWindows2");
HWND hwnd;
MSG msg;
//注册一个窗口类
//注册窗口类
WNDCLASS wndclass;
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szClassName;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.lpszMenuName = NULL;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.style = CS_HREDRAW;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, L"Error", L"Error", MB_ICONERROR);
return 0;
}
//创建窗口
hwnd = CreateWindow(
szClassName,
TEXT("MyFirstPractice"),
//方法一
//WS_OVERLAPPEDWINDOW &~WS_CAPTION &~WS_SYSMENU &~WS_SIZEBOX,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
//此处分发消息,调用消息回调函数,
//同时消息回调函数的返回值作为DIspatchMessage的返回值返回,
//一般讲这个返回值忽略
DispatchMessage(&msg);
/*
The return value specifies the value returned by the window procedure. Although its meaning depends on the message being dispatched, the return value generally is ignored.
返回值指定窗口过程返回的值。尽管它的含义依赖于发送的消息,但是返回值通常被忽略。
*/
}
return msg.wParam;
}
/*
WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
和
typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG, *PMSG;
前四个参数一致
操作系统直接使用msg前四位作为参数传入窗口回调函数
*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
TCHAR szBuffer[128];
size_t iTarget;
TEXTMETRIC tm;
static int cxChar, cyChar;
switch (message)
{
case WM_CREATE:
//在创建时就设置为静态全局变量,在后面的消息触发是就可以直接使用了
hdc = GetDC(hwnd);
//获得当前字体的尺寸
GetTextMetrics(hdc,&tm);
//系统获取行距等信息,不要自己猜测
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
case WM_SIZE:
; LONG_PTR s = GetWindowLongPtr(hwnd, GWL_STYLE);
//s = s &~WS_CAPTION &~WS_SYSMENU&~WS_SIZEBOX;
s = s&~WS_SIZEBOX;
SetWindowLongPtr(hwnd,GWL_STYLE, s);
break;
case WM_LBUTTONDOWN:
if (IDYES == MessageBox(NULL, L"Are you sure to quit?", L"Info", MB_YESNO)){
PostQuitMessage(0);
}
break;
case WM_NCLBUTTONUP:
MessageBox(NULL, L"Clicked on Not client Area", L"Info", NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
//SetTextAlign(hdc, GetTextAlign(hdc) | TA_CENTER);
int i = 0;
for (; i < 10;i++)
{
//使用安全的字符串方法,防止溢出等原因
StringCchPrintf(szBuffer, 128, TEXT("%d: %s"),i+1,TEXT("I love C++"));
//wsprintf(szBuffer, TEXT("%d: %s"),i+1, TEXT("I love C++"));
StringCchLength(szBuffer, 128, &iTarget);
//取代了lstrlen
//TextOut(hdc, 0,i*LineHeight, szBuffer, lstrlen(szBuffer));
TextOut(hdc, cxChar, i*cyChar, szBuffer, iTarget);
}
//TextOut(hdc, 400, 300, L"this is my second program", 20);
//DrawText(hdc, L"this is my second program", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hwnd, &ps);
break;
case WM_CLOSE:
if (IDYES == MessageBox(NULL, L"Are you sure to quit?", L"Info", MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
MessageBox(NULL, L"GUI is close,deal sql before quit", L"Info", MB_OK);
PostQuitMessage(0);
break;
case WM_QUIT:
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}