Sign in to follow this  

INT to LPCSTR? [C++ Win32 API]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this