想用Gdiplus的DrawString和DrawPath两种方法,把文字画出来,作比较。但发现,很奇怪的现象

yuernknfgae 2014-07-27 08:05:30
下面代码,用Gdiplus的两种方法,画了文字。但,效果上有一个怪现象。谁帮解释下哪里的问题?


当下面那个全局变量textSize == 195时,两种效果没有重合。

当把textSize 改为 196时,两种效果能够重合。

就是说,只是改变了一下文字的大小,为什么两种效果就不重合了?

到底是什么原因?

下面代码,有主函数,直接编译该cpp就能运行的。谁帮调试下?




#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);

}































...全文
543 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuernknfgae 2014-07-27
  • 打赏
  • 举报
回复
怎么样? 新建一个空工程,在建一个cpp,把代码复制进去cpp,能不能编译通过?
引用 1 楼 mfs 的回复:
晕,我用vs2008,新建一个CPP文件,把你的代码拷入后发现都没有编绎和运行的选顶,灰掉了。。。。
yuernknfgae 2014-07-27
  • 打赏
  • 举报
回复
应该是新建一个空工程吧?麻烦再试试?
引用 1 楼 mfs 的回复:
晕,我用vs2008,新建一个CPP文件,把你的代码拷入后发现都没有编绎和运行的选顶,灰掉了。。。。
mfs 2014-07-27
  • 打赏
  • 举报
回复
晕,我用vs2008,新建一个CPP文件,把你的代码拷入后发现都没有编绎和运行的选顶,灰掉了。。。。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧