10 out of 17 rated this helpful - Rate this topic

SetWindowText function

Applies to: desktop apps only

Changes the text of the specified window's title bar (if it has one). If the specified window is a control, the text of the control is changed. However, SetWindowText cannot change the text of a control in another application.

Syntax

BOOL WINAPI SetWindowText(
  __in      HWND hWnd,
  __in_opt  LPCTSTR lpString
);

Parameters

hWnd [in]

Type: HWND

A handle to the window or control whose text is to be changed.

lpString [in, optional]

Type: LPCTSTR

The new title or control text.

Return value

Type:

Type: BOOL

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

If the target window is owned by the current process, SetWindowText causes a WM_SETTEXT message to be sent to the specified window or control. If the control is a list box control created with the WS_CAPTION style, however, SetWindowText sets the text for the control, not for the list box entries.

To set the text of a control in another process, send the WM_SETTEXT message directly instead of calling SetWindowText.

The SetWindowText function does not expand tab characters (ASCII code 0x09). Tab characters are displayed as vertical bar (|) characters.

Examples

For an example, see Sending a Message.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Unicode and ANSI names

SetWindowTextW (Unicode) and SetWindowTextA (ANSI)

See also

Reference
GetWindowText
WM_SETTEXT
Conceptual
Windows

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Windows 64 Bit bug

Hello Ben

I discovered the same. Here is a code how to reproduce the problem.

If the String pointer passed to WM_SETTEXT or to SetWindowText() is not at an even address in memory the funtion/message will not set the WindowText and return TRUE.
___________________________________

BYTE* u8_Mem = (BYTE*)GlobalAlloc(GMEM_FIXED, 100)  + 1;

WCHAR* u16_Text = (WCHAR*)u8_Mem;

wcscpy(u16_Text, L"Hello World");

BOOL b_Ret = SetWindowTextW(h_Wnd, u16_Text);
___________________________________

If you remove the "+1" in the first line the code will work.

This is absurd.
It seems that Windows assumes that a String pointer that has not an even address is an invalid pointer.
I think Microsoft should use more intelligent ways to check memory addresses.
At least the function should return FALSE and an error code in GetLastError() indicating that the function failed.

I observed this only on Windows 7 - 64 Bit version no matter if the application was compiled as 32 Bit or 64 Bit.

If you use structs that are BYTE aligned and contain strings you may easily run into this problem.

A note about SetWindowTextW
Note that SetWindowTextW, the Unicode version of the function, requires the argument to be aligned (i.e. on an 8 or 16 byte boundary), otherwise the function may fail. This is also true of several other API functions.
Possible VB9 declaration
Friend Declare Auto Function SetWindowText Lib "user32.dll" (ByVal hwnd As Int32, ByVal lpString As String) As Boolean