How can i hide the window icon and minimize and maximize button from titlebar using windows api in java swing using, com.sun.jna.platform ?

AndronikosG 40 Reputation points
2024-09-12T20:45:37.04+00:00

So basically im new to the windows api and Im trying to hide both maximize, minimize buttons as well as the titlebar icon using the windows api and the java native access library. Here is what i tried so far

private void disableTitleBarIcon() {    
        WinDef.HWND hwnd = new WinDef.HWND(Native.getComponentPointer(this)); 
        int style = User32.INSTANCE.GetWindowLong(hwnd, User32.GWL_STYLE); 
        User32.INSTANCE.SetWindowLong(hwnd, User32.GWL_STYLE, style & ~User32.WS_SYSMENU | User32.WS_CAPTION); 
        User32.INSTANCE.SetWindowPos(hwnd, null, 0, 0, 0, 0, 
        WinUser.SWP_NOMOVE | WinUser.SWP_NOSIZE | WinUser.SWP_NOZORDER | WinUser.SWP_FRAMECHANGED);   
    }

For now I tried disabling SYSMENU but this takes the x button away as well, how can i fix this?

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,332 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,592 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 45,326 Reputation points
    2024-09-13T10:31:53.52+00:00

    This worked well -

    SetWindowLongPtr(hWnd, GWL_EXSTYLE, GetWindowLongPtr(hWnd, GWL_EXSTYLE) | WS_EX_DLGMODALFRAME);
    SetClassLongPtr(hWnd, GCLP_HICON, NULL);
    SetClassLongPtr(hWnd, GCLP_HICONSM, NULL);
    SetWindowLongPtr(hWnd, GWL_STYLE, GetWindowLongPtr(hWnd, GWL_STYLE) & ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX));
    SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
    

    Before -

    Before

    After -

    After

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. RLWA32 45,326 Reputation points
    2024-09-12T22:47:11.85+00:00

    A typical Win32 desktop application created with WS_OVERLAPPEDWINDOW style -

    Before

    auto exstyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
    exstyle |= WS_EX_TOOLWINDOW;
    SetWindowLongPtr(hWnd, GWL_EXSTYLE, exstyle);
    SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
    

    After -

    After


  2. Jeanine Zhang-MSFT 9,666 Reputation points Microsoft Vendor
    2024-09-13T08:03:36.2333333+00:00

    To hide the window icon, you could try to use WS_EX_DLGMODALFRAME:

     SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_DLGMODALFRAME);
    

    To hide the minimize and the maximize button, when you create a Win32 desktop application, you could remove WS_MINIMIZEBOX and WS_MAXIMIZEBOX

       HWND hWnd = CreateWindowW(szWindowClass, szTitle, /*WS_OVERLAPPEDWINDOW*/ WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME ,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
    

    enter image description here

    0 comments No comments

  3. Castorix31 84,546 Reputation points
    2024-09-13T08:10:45.55+00:00

    This works for me on Windows 10 22H2, in WM_CREATE of a C++/Win32 app (otherwise it must be refreshed) :

    // Remove caption icon
    SetClassLongPtr(hWnd, GCLP_HICON, (LONG_PTR)NULL);
    SetWindowLong(hWnd, GWL_EXSTYLE, GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_DLGMODALFRAME);
    // Remove minimize/maximize
    SetWindowLong(hWnd, GWL_STYLE, GetWindowLong(hWnd, GWL_STYLE) & ~(WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
    
    

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.