C ++ Visual Studios 2003 GUI帮助 - 错误C2664:'TextOutA' [英] C++ Visual Studios 2003 GUI Help - error C2664: 'TextOutA'

查看:48
本文介绍了C ++ Visual Studios 2003 GUI帮助 - 错误C2664:'TextOutA'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用visual studio 2003在C ++中创建一个程序,对于一个项目,我需要创建一个应用程序。在这个用于测试目的的应用程序中,我输出一个数字来表示按下了某个键。我收到了


错误C2664:''TextOutA'':无法将参数4从''std :: string''转换为''LPCSTR''

由于错误。


我的代码是

Hi I am creating a program in C++ using visual studios 2003, and for a project I am required to create a application. In this application for testing purposes I am outputing a number to show that a certain key was pressed. I am getting

error C2664: ''TextOutA'' : cannot convert parameter 4 from ''std::string'' to ''LPCSTR''

As the error.

My code is

展开 | 选择 | 换行 | 行号

推荐答案

从[code&#93和[/ code&#93]之间的邮政编码开始,它将使阅读更容易。


问题是您正在使用来自2个不同库的组件,即C ++ STL和Windows 32位API(WIN32)。 Windows 32位API不了解C ++ STL,反之亦然。


在行中


TextOut(hDC,150,150,(第一步),2);


你试图将STL sting对象(trst)传递给WIN32函数,但WIN32函数期望一个LPCSTR类型的变量(归结为const char *如果你想查看头文件)。


字符串std对象不再处理C样式字符串,并且没有可以将字符串STL对象转换为的默认运算符C样式字符串指针。但是它确实有一个相同的功能。


MFC还有一个备用字符串类CString,它知道C风格的字符串,因为它是一个与之集成的辅助类。 WIN32 API。


你有3个选项
To start with post code between [code&#93 and [/code&#93 it will make it easier to read.

The problem is that you are using components from 2 different libraries, namely the C++ STL and the Windows 32 bit API(WIN32). The Windows 32 bit API has no knowledge of the C++ STL and vis versa.

In the line

TextOut(hDC,150,150,(trst),2);

you are trying to pass an STL sting object(trst) to WIN32 function, however the WIN32 function is expecting a variable of type LPCSTR (which boils down to const char * if you care to look through the header files).

The string std object no longer deals in C style strings and has no default operator that can convert a string STL object to the C style string pointer. However it does have a function to do the same.

The MFC also has an alternate string class CString which does know about C style strings as it is a helper class made to integrate with the WIN32 API.

You have 3 options
  1. 保留stl字符串,你需要通过的地方它到WIN32 API使用c_str成员函数将其转换为可用类型


    TextOut(hDC,150,150,(trst.c_str()),2);
  2. 使用CString,它知道如何将自己转换为LPCSTR

    CString trst;

    trst =" Hi";

    TextOut(hDC,150,150,trst,2);
  3. 回到旧的角色数组

    char trst [10];

    strcpy (第一,你好);

    TextOut(hDC,150,150,trst,2);


非常感谢你这很有效。我正在研究一些事情,我正在使用箭头键移动用户输入,这会移动一个圆圈。我遇到的唯一问题是它不重绘屏幕只是重新绘制另一个循环,所以我最终得到一个圆圈。对于如何解决这个问题,有任何的建议吗。您的帮助很明显。
Thank you very much that worked. I am also working on something were I am getting the user input using the arrow keys which moves a circle. The only problems that I am having is that it is not redrawing the screen is just redraws another circule every time so I end up with a line of circles. Any suggestions on how to fix this. You help is much aprechated.


再次3个选项
Again 3 options
  1. 在背景颜色之前重绘您的原始圆圈
  2. 如果这个constitues编辑,那么你可以通过反转位的颜色(使用xor)来绘制圆圈以使它们返回,然后再次反转它们。一旦你有了你想要的位置,就可以用所需的颜色绘制圆圈。
  3. 可能正确的方法是重绘屏幕。确定了您想要圆圈的位置并设置数据使正在更新的屏幕区域无效(如果您使整个屏幕无效,那么它将工作但速度会慢很多)并调用UpdateWindow。处理WM_PAINT消息以实际绘制窗口。这也将确保如果另一个窗口放在窗口顶部然后被移除它将被正确地重新绘制


这篇关于C ++ Visual Studios 2003 GUI帮助 - 错误C2664:'TextOutA'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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