问题描述
我已经搜索了一些天,现在找到一个可能性在我的GDI +应用程序显示文本。
I have searched for some days now to find a possibility to display text on my GDI+ application.
我试图使用 DrawString
GDI +的函数,但是MSDN上的引用不工作,因为它与参数列表不匹配。我使用Visual C ++ 2010 Express。
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.
我更改了MSDN示例以使其编译,如下所示:
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);
我也尝试过两个函数:
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?
推荐答案
你正在做相当经典的错误,不检查Graphics :: DrawString (),它会告诉你你做错了什么。 InvalidParameter很可能在这里。在这个代码运行的上下文中也是不太清楚,更好的是在WM_PAINT消息处理程序中,否则你永远不会看到输出。也没有清理代码的证据,因为代码泄漏对象很糟糕。
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.
让我们从一个完整的例子开始,从Win32项目模板生成的样板代码。我知道你有一些这已经工作,但它可能是有趣的,其他人阅读这个答案。从提供所需的#includes开始:
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:
#include <assert.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "gdiplus.lib")
找到WinMain函数,我们需要初始化GDI +:
Locate the WinMain function, we need to initialize 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;
现在找到窗口过程(WndProc),并使WM_PAINT情况类似于:
Now locate the window procedure (WndProc) and make the WM_PAINT case similar to this:
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;
这产生了:
修改此代码,如您所见,这些声明将帮助你摆脱困境。
Modify this code as you see fit, the asserts will keep you out of trouble.
这篇关于使用GDI +绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!