Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SetWindowText (user32)
 
.
Summary

C# Signature:

/// <summary>
///     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.
///     <para>
///     Go to https://msdn.microsoft.com/en-us/library/windows/desktop/ms633546%28v=vs.85%29.aspx for more
///     information
///     </para>
/// </summary>
/// <param name="hwnd">C++ ( hWnd [in]. Type: HWND )<br />A handle to the window or control whose text is to be changed.</param>
/// <param name="lpString">C++ ( lpString [in, optional]. Type: LPCTSTR )<br />The new title or control text.</param>
/// <returns>
///     If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.<br />
///     To get extended error information, call GetLastError.
/// </returns>
/// <remarks>
///     If the target window is owned by the current process, <see cref="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, <see cref="SetWindowText" /> sets the text for the control, not for the list box entries.<br />To set the
///     text of a control in another process, send the WM_SETTEXT message directly instead of calling
///     <see cref="SetWindowText" />. The <see cref="SetWindowText" /> function does not expand tab characters (ASCII code
///     0x09). Tab characters are displayed as vertical bar(|) characters.<br />For an example go to
///     <see cref="!:https://msdn.microsoft.com/en-us/library/windows/desktop/ms644928%28v=vs.85%29.aspx#sending">
///     Sending a
///     Message.
///     </see>
/// </remarks>
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);

VB.Net Signature:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean
End Function

VB Signature:

Declare Auto Function SetWindowText Lib "user32" (ByVal hWnd As IntPtr, ByVal lpstring As String) As Boolean

User-Defined Types:

None.

Notes:

The handle used cannot be used to make a Form from Form.FromHandle() or from Control.FromHandle().

Tips & Tricks:

Please add some!

C# Sample Code:

SetWindowText(Process.GetCurrentProcess().MainWindowHandle, "Amazing!");

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
Note
This SetWindowText will set text to the controls(textbox for example) in the same process. If they exist in other processes, use SendMessage instead... - jp

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions