#include <windows.h>
#include <iostream>
CHAR szText[256] = { 0 };
#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
HINSTANCE g_hInst = NULL; //窗口句柄
HANDLE g_hStdout = NULL; //控制台句柄
void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
CreateWindow("BUTTON", "DIY按钮",
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
50, 50, 200, 50, hWnd, (HMENU)1001, g_hInst, NULL);
CreateWindow("BUTTON", "普通按钮", WS_VISIBLE | WS_CHILD,
400, 50, 200, 50, hWnd, NULL, g_hInst, NULL);
}
void OnDrawItem(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
LPDRAWITEMSTRUCT pDis = (LPDRAWITEMSTRUCT)lParam;
if (ODT_BUTTON == pDis->CtlType)
{
if (pDis->itemState & ODS_SELECTED)
{
//使用画刷
//1、创建 2、交换 3、销毁
HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 255));
HBRUSH hOldBrush = (HBRUSH )SelectObject(pDis->hDC, hBrush);
RoundRect(pDis->hDC, pDis->rcItem.left,
pDis->rcItem.top, pDis->rcItem.right,
pDis->rcItem.bottom,15,15);
SelectObject(pDis->hDC, hOldBrush);
DeleteObject(hOldBrush);
}
else
{
HBRUSH hBrush = CreateSolidBrush(RGB(100, 100, 255));
HBRUSH hOldBrush = (HBRUSH)SelectObject(pDis->hDC, hBrush);
RoundRect(pDis->hDC, pDis->rcItem.left,
pDis->rcItem.top, pDis->rcItem.right,
pDis->rcItem.bottom, 15, 15);
SelectObject(pDis->hDC, hOldBrush);
DeleteObject(hOldBrush);
}
//绘制按扭
/*Rectangle(pDis->hDC, pDis->rcItem.left,
pDis->rcItem.top, pDis->rcItem.right,
pDis->rcItem.bottom);*/
//绘制圆角按扭
/*RoundRect(pDis->hDC, pDis->rcItem.left,
pDis->rcItem.top, pDis->rcItem.right,
pDis->rcItem.bottom,15,15);*/
GetWindowText(pDis->hwndItem, szText, 256);
//绘制按钮文字名称
int nOldMode = SetBkMode(pDis->hDC, TRANSPARENT);//去文字背景色
DrawText(pDis->hDC, szText, strlen(szText), &pDis->rcItem,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SetBkMode(pDis->hDC, nOldMode);//在给设置回来
}
}
void OnMeasureitem(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
LPMEASUREITEMSTRUCT pMis = (LPMEASUREITEMSTRUCT)lParam;
pMis->itemHeight = 200;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_CREATE:
OnCreate(hWnd,nMsg,wParam,lParam);
break;
case WM_DRAWITEM:
OnDrawItem(hWnd, wParam, lParam);
return 0;
case WM_MEASUREITEM: //使用这项,这项对于BUTTON没有影响,但对COMBOBOXLIST有影响
OnMeasureitem(hWnd,nMsg,wParam,lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, nMsg, wParam, lParam);
}
BOOL RegisterWnd(LPSTR pszClassName)
{
WNDCLASSEX wce = { 0 };
wce.cbSize = sizeof(wce);
wce.cbClsExtra = 0;
wce.cbWndExtra = 0;
wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
wce.hCursor = NULL;
wce.hIcon = NULL;
wce.hIconSm = NULL;
wce.hInstance = g_hInst;
wce.lpfnWndProc = WndProc;
wce.lpszClassName = pszClassName;
wce.lpszMenuName = NULL;
wce.style = CS_HREDRAW | CS_VREDRAW;
ATOM atom = RegisterClassEx(&wce);
if (atom == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
HWND CreateWnd(LPSTR pszClassName)
{
HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
return hWnd;
}
void ShowWnd(HWND hWnd)
{
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
}
void Msg()
{
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
void ConsoleWnd()
{
AllocConsole();
g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
CHAR szText[] = "Debug start:\n";
WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
g_hInst = hInstance;
//ConsoleWnd();
RegisterWnd("oooo");
HWND hWnd = CreateWnd("oooo");
ShowWnd(hWnd);
Msg();
return 0;
}
时间: 2024-10-20 01:16:47