0

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

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
Psychoman
  • 55
  • 7
  • Please clarify what you mean by `static_label`. – Drew Chapin Sep 02 '13 at 20:36
  • 1
    Is 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 Answers3

6

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.

Andon M. Coleman
  • 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
2
#include <atlstr.h>

CString a = "I know this thing ";
CString b = "foo";
SetWindowText(static_label, a + b);
RichieHindle
  • 272,464
  • 47
  • 358
  • 399
ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
1

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;
}
Drew Chapin
  • 7,779
  • 5
  • 58
  • 84