使用 GDI 绘制文本+

发布于 2024-12-02 15:25:30 字数 730 浏览 3 评论 0原文

我已经搜索了几天,以找到在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2024-12-09 15:25:30

您犯了一个相当经典的错误,即不检查 Graphics::DrawString() 的返回值,它会告诉您做错了什么。这里很可能出现 InvalidParameter。还不清楚此代码在哪个上下文中运行,最好在 WM_PAINT 消息处理程序中运行,否则您将永远不会看到输出。也没有清理代码的证据,因为代码严重泄漏了对象。

让我们从一个完整的示例开始,从 Win32 项目模板生成的样板代码开始。我知道您已经完成了其中一些工作,但其他人阅读此答案可能会很有趣。首先给出所需的 #includes:

#include <assert.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "gdiplus.lib")

找到 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;

在消息循环之后的函数末尾:

GdiplusShutdown(gdiplusToken);
return (int) msg.wParam;

找到窗口过程 (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;

现在 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:

#include <assert.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib, "gdiplus.lib")

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;

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;

Which produces this:

enter image description here

Modify this code as you see fit, the asserts will keep you out of trouble.

飘逸的'云 2024-12-09 15:25:30

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文