Drawtext()本身不起作用 [英] Drawtext() not behaving itself

查看:73
本文介绍了Drawtext()本身不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行它时(它是一个子窗口),我得到的只是字符串的4个字母.我认为矩形足够大.我不正确使用Drawtext()吗?


when i run this, (it''s a child window), all i get is 4 letters of the string. i figure the rectangle is plenty big enough. am i using Drawtext() incorrectly?


LRESULT CALLBACK printout(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	static PAINTSTRUCT paint;
	static RECT textbox;
	static LPCTSTR blah = "this is a test";

	switch(msg)
	{
		case WM_CLOSE:
		{
                        DestroyWindow(hwnd);
			break;
		}
                case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
		case WM_PAINT:
		{
			hdc = BeginPaint(hwnd, &paint);

				SetRect(&textbox, 20, 20, 300, 60);
				DrawText(hdc, blah, sizeof(blah), &textbox, DT_VCENTER);

			EndPaint(hwnd, &paint);

		}
		default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

推荐答案

您正在使用不正确的第二个参数,该参数应该传递要绘制的字符数.它等于4.为什么?这与GDI无关.这是因为您可能不了解sizeof的作用.您期望它会给您字符串的长度吗?没办法.

让我猜猜.您使用的是Windows的32位版本,对吗? 32位是4个字节.您的blah是一个指针,您正在使用它的大小,它的大小只是一个指针的恒定大小,在您的情况下为4个字节.指针的大小不是字符串的长度.例如,对于字符串的长度,请使用strlen,或使用正确处理长度的系统类型std:string.

—SA
You''re using incorrect second parameter which is supposed to pass number of characters to draw. And it equals to exactly 4. And why? It all has nothing to do with GDI. This is because you probably don''t understand what sizeof does. Do you expect it will give you the length of the string? No way.

Let me guess. You''re using 32-bit version of Windows, correct? 32 bits is 4 bytes. Your blah is a pointer, you''re using its its size which is just the constant size of a pointer, 4 bytes in your case. Size of a pointer is not a length of string. For length of string, for example, use strlen, or use the system type std:string which properly handles lengths.

—SA


这篇关于Drawtext()本身不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆