How can you do stuff like SetWindowText( static_label, "I know this thing" + myString )
?

- 7,779
- 5
- 58
- 84

- 55
- 7
-
Please clarify what you mean by `static_label`. – Drew Chapin Sep 02 '13 at 20:36
-
1Is there anything in your question that is not answered by [the `SetWindowText()` documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633546(v=vs.85).aspx) ? Are you asking how to concatenate two strings *during* a call to `SetWindowText()` (thereby making the actual call completely irrelevant)? – WhozCraig Sep 02 '13 at 20:42
-
Also, it is not clear what operator overloading has to do with calling a function. – Jon Sep 02 '13 at 20:42
-
Did you mean to do `SetWindowText( static_label, "I know this thing" + myString)` ? Note the difference in the location of the quotation marks. If so, what is the **type** of `myString`? Is it a `CString`, an `std::string`, `char*` or something else? – Drew Chapin Sep 02 '13 at 20:45
3 Answers
This question has nothing to do with operator overloading, or overloading in general for that matter.
If you are referring to how SetWindowText (...)
can be used to set the title for a dialog window and a static label it is because HWND
is a generic handle.
If on the other hand, you are asking how to concatenate text, you can use a std::string
and call .c_str (...)
to get a null-terminated string that the Win32 API wants.

- 42,359
- 2
- 81
- 106
-
One can also provide define an overload of `SetWindowText` that calls `c_str()` for you and then calls the Win32 version. – Ben Voigt Sep 02 '13 at 21:01
#include <atlstr.h>
CString a = "I know this thing ";
CString b = "foo";
SetWindowText(static_label, a + b);

- 272,464
- 47
- 358
- 399

- 10,337
- 2
- 15
- 15
-
1Doesn't `CString` require MFC? @Psychoman did not say whether or not he is using MFC. – Drew Chapin Sep 02 '13 at 20:46
-
-
If you can do this stuff with CString without MFC , then it's what I wanted thx. – Psychoman Sep 03 '13 at 04:56
-
@Psychoman, Looks like there is still a dependency on using ATL. You can do it with `std::string` without having have it depend on anything other than the standard C++ libraries. – Drew Chapin Sep 04 '13 at 23:28
Here's how to do it using only the standard C++ libraries (and obviously the Windows API). It's a little more complicated than using CString
(ATL) though. However, if you plan on releasing your code as Open Source, this method would probably be better since it will allow others to compile the code using compilers other than Visual C++ (e.g. MingW).
#include <string>
#include <Windows.h>
HWND static_label;
int main() {
// ...
std::string a = "Hello ";
std::string b = "World!";
std::string c = a + b;
SetWindowText(static_label, c.c_str());
// ...
return 0;
}
Another way without having to use b
or c
#include <string>
#include <Windows.h>
HWND static_label;
int main() {
// ...
std::string a = "Hello ";
SetWindowText(static_label, std::string(a+"World!").c_str());
// ...
return 0;
}

- 7,779
- 5
- 58
- 84