Jump to content
  • Advertisement
Sign in to follow this  

INT to LPCSTR? [C++ Win32 API]

This topic is 3634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I'm still new to Win32 API [C++] and was wondering how I would do a certain thing. I have a static text and would like to maybe use a for loop (1-60) with a sleeping time of 1 seconds and convert that integer then use it to set the text. How would I do this? I tryed to use SetWindowText and used a (LPCSTR) cast on the second parameter where my integer would be. So would the question be converting int to LPCSTR? I'm not sure... How would I do this? I appreciate your time and thank you.

Share this post

Link to post
Share on other sites
The quickest way is using itoa.

int iValue = 0;
SetWindowText(hwnd, itoa(iValue));

To get more customized text, you can use _snprintf (link to printf for other % modifiers) (or many of the other functions to build strings):

char txt[32] = {0};
_snprintf(txt, 31, "Value is %i", iValue);
SetWindowText(hwnd, txt);

Share this post

Link to post
Share on other sites
An alternative is std::stringstream:

#include <sstream>

int value = 42;
std::stringstream stream;
stream << "Value is " << 42;
// std::stringstream::str() returns a std::string instance
// std::string::c_str() returns a const char pointer
// So stream.str().c_str() returns a const char pointer.

The benefit of this is that it will work with any types that have overloaded << and >>.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!