16,550
社区成员
发帖
与我相关
我的任务
分享
#include <Windows.h>
#include <GdiPlus.h>
#include <assert.h>
int textSize = 195;
LRESULT CALLBACK MyWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT PtStr;
HDC hDC = ::BeginPaint(hWnd, &PtStr);
::EndPaint(hWnd, &PtStr);
return 0;
}
}
return ::DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
ULONG_PTR gdiplusStartupToken;
Gdiplus::GdiplusStartupInput gdiInput;
Gdiplus::GdiplusStartup(&gdiplusStartupToken, &gdiInput, NULL);
const wchar_t * myWindowClass = L"MyWindowClass";
WNDCLASSW wc = { 0 };
wc.style = NULL;
wc.lpfnWndProc = MyWindowProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = NULL;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
wc.lpszMenuName = NULL;
wc.lpszClassName = myWindowClass;
if (!RegisterClassW(&wc))
{
assert(false);
}
HWND hWnd = CreateWindowExW(WS_EX_TOOLWINDOW, myWindowClass, L"myWindow",
WS_POPUP | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 260, 200, 550, 550, NULL, NULL, NULL, NULL);
Gdiplus::Bitmap bitmap(550, 550, PixelFormat32bppPARGB);
Gdiplus::Graphics graphics(&bitmap);
graphics.SetTextRenderingHint(Gdiplus::TextRenderingHintAntiAliasGridFit);
graphics.FillRectangle(&Gdiplus::SolidBrush(Gdiplus::Color(255, 255, 0, 255)), Gdiplus::Rect(0, 0, 550, 550));
Gdiplus::FontFamily fontFamily(L"宋体");
Gdiplus::Font font(&fontFamily, Gdiplus::REAL(textSize), Gdiplus::FontStyleUnderline, Gdiplus::UnitPixel);
Gdiplus::StringFormat strformat;
strformat.SetAlignment(Gdiplus::StringAlignmentCenter);
strformat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
//第一种方法:
Gdiplus::GraphicsPath path;
path.AddString(L"X", -1, &fontFamily, font.GetStyle(), font.GetSize(),
Gdiplus::RectF(1, 1, 500, 500),
&strformat);
graphics.DrawPath(&Gdiplus::Pen(Gdiplus::Color(255, 0, 0, 255), 3), &path);
//第二种方法:
graphics.DrawString(L"X", -1, &font,
Gdiplus::RectF(1, 1, 500, 500),
&strformat, &Gdiplus::SolidBrush(Gdiplus::Color(255, 255, 0, 0)));
HDC hDC = ::GetDC(hWnd);
Gdiplus::Graphics graphicsDC(hDC);
graphicsDC.DrawImage(&bitmap, 0, 0);
::ReleaseDC(hWnd, hDC);
MSG msg = { 0 };
while (::GetMessageW(&msg, NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessageW(&msg);
}
return (int)(msg.wParam);
}