简体   繁体   English

C ++问题与TextOut()gdi函数LPCSTR参数

[英]c++ issue with the TextOut() gdi function LPCSTR parameter

I am trying to use the TextOut function to paint words on my window, and the following method works fine for me: 我正在尝试使用TextOut函数在窗口上绘制单词,并且以下方法对我来说效果很好:

HDC hdc = GetDC(windowHandle);
TextOut(hdc, 10, 10, TEXT("Hello World"), 16);
ReleaseDC(windowHandle, hdc);

And this outputs :Hello World 而这个输出:你好世界

All good so far, however when I do the following method: 到目前为止一切都很好,但是当我执行以下方法时:

HDC hdc = GetDC(windowHandle);
string myString = "Hello World";
TextOut(hdc, 10, 10, myString.c_str(), 16);
ReleaseDC(windowHandle, hdc);

the program outputs: Hello World#$%^& 程序输出:Hello World#$%^&

and the #$%^& part are actually other square symbols that I am not sure how to write on the keyboard. 和#$%^&部分实际上是我不确定如何在键盘上写的其他方形符号。 I understand that the forth parameter of the TextOut function is type LPCSTR, and using the .c_str() function after my string should output the LPCSTR variable correctly, and so it does since the program runs, however why do I get teh #$%^& included at the end of Hello World and how might I go about fixing that issue? 我知道TextOut函数的第四个参数是LPCSTR类型,并且在我的字符串后使用.c_str()函数应该正确输出LPCSTR变量,所以自程序运行以来它确实如此,但是为什么我得到#$% ^&包含在Hello World的末尾,我该如何解决该问题? I do need to use the second method and not the first because my program will generate strings which then I would like to output to my window. 我确实需要使用第二种方法而不是第一种方法,因为我的程序将生成字符串,然后将其输出到我的窗口。

According to the documentation of TextOut ( http://msdn.microsoft.com/en-us/library/windows/desktop/dd145133%28v=vs.85%29.aspx ) the fith parameter reflects the length of the string. 根据TextOut的文档( http://msdn.microsoft.com/zh-cn/library/windows/desktop/dd145133%28v=vs.85%29.aspx),fith参数反映了字符串的长度。 You are saying 16 here, however, it's only 11. 您在这里说的是16 ,但是只有11。

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

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