使用 GDI 绘制文本+
我已经搜索了几天,以找到在我的 GDI+ 应用程序上显示文本的可能性。
我尝试使用 GDI+ 的 DrawString()
函数,但 MSDN 上的参考不起作用,因为它与参数列表不匹配。我正在使用 Visual C++ 2010 Express。
我更改了 MSDN 示例以使其编译,如下所示:
LinearGradientBrush* myBrush = new LinearGradientBrush(Rect(0,0,width,height),Color::Red, Color::Yellow, LinearGradientMode::LinearGradientModeHorizontal);
Font* myFont = new Font(hdc);
RectF rect = RectF(10,10,100,100);
graphics.DrawString(TEXT("Look at this text!"),100, myFont,rect,&StringFormat(0,0), myBrush);
我还尝试了其他两个函数:
TextOut(hdc,10,10,TEXT("Text"),6);
DrawText(hdc,TEXT("Text"),0,LPRECT(0),0);
它们都没有在屏幕上显示文本。绘制直线、椭圆等都没有问题。
为什么上面的文本绘制例程不起作用?有人可以提供一个可行的例子吗?
I have searched for some days now to find a possibility to display text on my GDI+ application.
I tried using the DrawString()
function of GDI+ but the reference on MSDN does not work as it does not match with the parameter list. I am using Visual C++ 2010 Express.
I changed the MSDN example to make it compile, like this:
LinearGradientBrush* myBrush = new LinearGradientBrush(Rect(0,0,width,height),Color::Red, Color::Yellow, LinearGradientMode::LinearGradientModeHorizontal);
Font* myFont = new Font(hdc);
RectF rect = RectF(10,10,100,100);
graphics.DrawString(TEXT("Look at this text!"),100, myFont,rect,&StringFormat(0,0), myBrush);
I also tried two other functions:
TextOut(hdc,10,10,TEXT("Text"),6);
DrawText(hdc,TEXT("Text"),0,LPRECT(0),0);
None of them shows a text on the screen. Drawing lines, ellipses, etc. works with no problems.
Why doesn't the above text-drawing routine work? Can anybody provide a working example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您犯了一个相当经典的错误,即不检查 Graphics::DrawString() 的返回值,它会告诉您做错了什么。这里很可能出现 InvalidParameter。还不清楚此代码在哪个上下文中运行,最好在 WM_PAINT 消息处理程序中运行,否则您将永远不会看到输出。也没有清理代码的证据,因为代码严重泄漏了对象。
让我们从一个完整的示例开始,从 Win32 项目模板生成的样板代码开始。我知道您已经完成了其中一些工作,但其他人阅读此答案可能会很有趣。首先给出所需的 #includes:
找到 WinMain 函数,我们需要初始化 GDI+:
在消息循环之后的函数末尾:
找到窗口过程 (WndProc) 并使 WM_PAINT 情况与此类似:
现在 this:
根据需要修改此代码,断言将使您摆脱麻烦。
You are making the fairly classic mistake of not checking the return value of Graphics::DrawString(), it will tell you what you did wrong. InvalidParameter is pretty likely here. It is also quite unclear in which context this code runs, that better be inside the WM_PAINT message handler or you'll never see the output. There is also no evidence of cleanup code, as given the code leaks objects badly.
Let's work from a full example, starting from the boilerplate code generated by the Win32 Project template. I know you've got some of this already working but it could be interesting to others reading this answer. Start by giving the required #includes:
Locate the WinMain function, we need to initialize GDI+:
and at the end of the function after the message loop:
Now locate the window procedure (WndProc) and make the WM_PAINT case similar to this:
Which produces this:
Modify this code as you see fit, the asserts will keep you out of trouble.
MSDN 是你的朋友(真实的事情):
画一条线 - 代码示例:编译并运行
和
绘制字符串 -- 替换 OnPaint () 在上一篇中。
MSDN is your friend (true thing):
Drawing a Line - code sample: compile and run
and
Drawing a String -- replace OnPaint() in previous one.