Search code examples
c++visual-studiowxwidgets

Positioning the wxFrame object


Using the very simple wxFrame example form the official wxWidgets site I'd like to set the created window position jut to the very upper left screen corner

so I've added the simple frame->SetPosition(wxPoint(0, 0)) line. Unfortunately for some bizarre reason there is some little, but UNWANTED "margin" from the left side as shown on the enter image description here. What is that and how to get rig of that gap to position the window precisely as expected?


Solution

  • As a code supplement to VZ. 's answer.

    Use DwmGetWindowAttribute to get the BOUNDS and subtract WindowRect

    #include<dwmapi.h>
    #pragma comment(lib,"Dwmapi.lib")
    

    .

    bool MyApp::OnInit()
    {
        MyFrame* frame = new MyFrame();
        frame->Show(true);
        HWND hwnd = frame->GetHWND();
        RECT extendedFrameBounds = { 0 };
        HRESULT hr = DwmGetWindowAttribute(hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &extendedFrameBounds, sizeof(extendedFrameBounds));
    
        if (SUCCEEDED(hr))
        {
    
            RECT windowRect;
            GetWindowRect(hwnd, &windowRect);
    
    
            int offsetX = extendedFrameBounds.left - windowRect.left;
            int offsetY = extendedFrameBounds.top - windowRect.top;
    
            frame->SetPosition(wxPoint (-offsetX, -offsetY));
           
        }
        return true;
    }