Win32 : unable to set window title

Harshithraj1871 1,681 Reputation points
2024-01-04T07:19:08.4633333+00:00

Hi,

I'm working on win32 desktop application. I used CreateWindowExW() API to create a window.

                LPCWSTR         title = L"SAMPLE TITLE";

            // Create Window With properties

            window = CreateWindowExW (NULL,
                                      windowclassname,
                                      title,
                                      windowstyle,
                                      (int) pInstructionSet[pIndex]->GetXPos (), (int) pInstructionSet[pIndex]->GetYPos (), (int) pInstructionSet[pIndex]->GetWidth (), (int) pInstructionSet[pIndex]->GetHeight (),
                                      NULL,
                                      NULL,
                                      hinstance,
                                      NULL
            );

I created a LPCWSTR string as a title and set that title in CreateWindowExW() api, but when the window came up, i can only see the first character of my title in window title.

User's image

How to fix this issue, did I miss anything here?

Thank you.

Windows development Windows API - Win32
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2024-01-05T10:42:30.6733333+00:00

    I used a very minimal Windows Application for testing. The application only uses the "W" versions of Windows API functions to create a UNICODE Window. The Character set property of the project was configured as "Not Set".

    Sample for testing -

    #include <windows.h>
    #include <stdio.h>
    
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    LPCWSTR pwszTitle = L"Sample Title";
    
    
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
    {
        hInst = hInstance;
        WNDCLASSW wc =
        {
            CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInst, LoadIconW(NULL, MAKEINTRESOURCEW(32512)),
            LoadCursorW(NULL, MAKEINTRESOURCEW(32512)), (HBRUSH)(COLOR_WINDOW + 1), NULL, L"WindowClass"
        };
    
        if (!RegisterClassW(&wc))
            return MessageBoxW(NULL, L"RegisterClassExW failed!", L"Error", MB_ICONERROR | MB_OK);
    
        HWND hWnd = CreateWindowExW(0, wc.lpszClassName, pwszTitle, WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL);
    
        if (!hWnd)
            return MessageBoxW(NULL, L"CreateWindowExW failed", L"Error", MB_ICONERROR | MB_OK);
    
        ShowWindow(hWnd, SW_SHOWNORMAL);
        UpdateWindow(hWnd);
    
        MSG msg;
        while (GetMessageW(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    
        return (int)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
    /*
        case WM_NCCREATE:
            {
                WCHAR szMsg[128];
                LPCREATESTRUCTW lpcs = (LPCREATESTRUCTW)lParam;
                swprintf_s(szMsg, L"In WM_NCCREATE, Window Class: %s, Window Title: %s\n", lpcs->lpszClass, lpcs->lpszName);
                OutputDebugStringW(szMsg);
                return TRUE;
            }
            break;
    
        case WM_CREATE:
            {
            WCHAR szMsg[128];
            LPCREATESTRUCTW lpcs = (LPCREATESTRUCTW)lParam;
            swprintf_s(szMsg, L"In WM_CREATE, Window Class: %s, Window Title: %s\n", lpcs->lpszClass, lpcs->lpszName);
            OutputDebugStringW(szMsg);
            }
            break;
    */
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
    
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

    When the handlers for WM_NCCREATE and WM_CREATE are commented out the window caption is truncated as described by the Questioner. However, I can think of no reason for this to happen!

    Spy++ shows that a UNICODE window was created but the UNICODE text passed to CreateWindowExW was truncated anyway!

    Spy1

    Then when the WM_NCCREATE and WM_CREATE handlers are uncommented the created window has no caption at all!

    Spy2

    And the debug output from the two handlers shows the proper UNICODE text for the Window caption -

    creates

    I don't understand why passing UNICODE text to a "W" API function for a UNICODE window causes such unexpected results. It doesn't seem to me that the project character set property is relevant in this example.


1 additional answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2024-01-05T01:40:44.43+00:00

    It almost is caused by Use Multi-Byte Character Set project setting. The same behavior will arise when I change the new created Windows Desktop Application to Use Multi-Byte Character Set.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.