[英]Drawing Text with GDI+
I have searched for some days now to find a possibility to display text on my GDI+ application. 我已经搜索了几天,以便找到在我的GDI +应用程序上显示文本的可能性。
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. 我尝试使用GDI +的
DrawString()
函数,但MSDN上的引用不起作用,因为它与参数列表不匹配。 I am using Visual C++ 2010 Express. 我正在使用Visual C ++ 2010 Express。
I changed the MSDN example to make it compile, like this: 我更改了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);
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?
任何人都能提供一个有效的例子吗?
You are making the fairly classic mistake of not checking the return value of Graphics::DrawString(), it will tell you what you did wrong. 你正在犯这个相当经典的错误,就是不检查Graphics :: DrawString()的返回值,它会告诉你你做错了什么。 InvalidParameter is pretty likely here.
InvalidParameter很可能在这里。 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.
还不清楚这个代码在哪个上下文中运行,最好是在WM_PAINT消息处理程序中,或者你永远不会看到输出。 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. 让我们从一个完整的例子开始,从Win32项目模板生成的样板代码开始。 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:
首先提供所需的#includes:
#include <assert.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "gdiplus.lib")
Locate the WinMain function, we need to initialize GDI+: 找到WinMain函数,我们需要初始化GDI +:
// TODO: Place code here.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Status st = GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
assert(st == Ok);
if (st != Ok) return FALSE;
and at the end of the function after the message loop: 并在消息循环后的函数结束时:
GdiplusShutdown(gdiplusToken);
return (int) msg.wParam;
Now locate the window procedure (WndProc) and make the WM_PAINT case similar to this: 现在找到窗口过程(WndProc)并使WM_PAINT案例类似于:
case WM_PAINT: {
hdc = BeginPaint(hWnd, &ps);
Graphics gr(hdc);
Font font(&FontFamily(L"Arial"), 12);
LinearGradientBrush brush(Rect(0,0,100,100), Color::Red, Color::Yellow, LinearGradientModeHorizontal);
Status st = gr.DrawString(L"Look at this text!", -1, &font, PointF(0, 0), &brush);
assert(st == Ok);
EndPaint(hWnd, &ps);
} break;
Which produces this: 产生这个:
Modify this code as you see fit, the asserts will keep you out of trouble. 根据需要修改此代码,断言将使您免于麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.