简体   繁体   English

C ++ DrawText和TextOut什么也不显示

[英]C++ DrawText And TextOut Displaying Nothing

Where do I use TextOut or DrawText? 在哪里使用TextOut或DrawText?

TCHAR text[]= "My First Window";
     RECT rc;
     HDC wdc = GetWindowDC(hWnd);
     GetClientRect (hWnd, &rc);
     SetTextColor(wdc, 0x00000000);
     SetBkMode(wdc,TRANSPARENT);
     rc.left=40;
     rc.top=10;
     TextOut(hdc,rc.left,rc.top,text,ARRAYSIZE(text));
     EndPath(hdc);
     SelectClipPath(hdc, RGN_AND);

I'm placing this in WM_CREATE: 我将其放在WM_CREATE:

The result is a blank window. 结果是一个空白窗口。 I can provide more code if need be, but it's just a standard Win32 blank window. 如果需要,我可以提供更多代码,但这只是一个标准的Win32空白窗口。

This is my first real Win32 application and I've googled and searched for an hour without finding the answer to my question. 这是我第一个真正的Win32应用程序,我已经搜索了一个小时,却没有找到问题的答案。

Thank you 谢谢

Windows doesn't work like that. Windows不能那样工作。 You can't just paint once and expect what you painted to be displayed forever. 您不能只绘画一次就可以期待您的绘画永远显示。 Your window probably isn't even visible when WM_CREATE is handled. 处理WM_CREATE时,您的窗口可能甚至不可见。

(as an aside, you are also leaking wdc in the above code, and interchanging wdc with hdc ). wdc ,您还会在上面的代码中泄漏wdc ,并将wdchdc互换)。

You need to handle the WM_PAINT message and do your painting in there. 您需要处理WM_PAINT消息并在那里进行绘画。 Call BeginPaint() to get an HDC that you can draw on, and call EndPaint() when finished. 调用BeginPaint()获得可绘制的HDC,并在完成时调用EndPaint()

You should get a beginner's book in Win32 programming as handling WM_PAINT is pretty basic stuff. 处理WM_PAINT是非常基础的工作,因此您应该在Win32编程中得到一本初学者的书。 Start with the MSDN documentation: 从MSDN文档开始:

Painting and Drawing 绘画与素描

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM