GetPosition returns negative values on x position when window is near left edge of screen on Windows (Issue #22547)

242 views
Skip to first unread message

guzmanpaco

unread,
Jun 16, 2022, 10:17:29 PM6/16/22
to wx-...@googlegroups.com, Subscribed

Describe the bug
We use a combination of GetPosition, SetPosition and wxDisplay::GetFromPoint in our application. If I put the window on upper left of the screen on Windows (whole window is still on the screen), GetPosition returns -20,0 instead of 0,0 on wxWidgets 3.1.0 and 3.1.7. If I use wxDisplay::GetFromPoint to get monitor number of that point on wxWidgets 3.1.0, it will return monitor 0. On 3.1.7, this returns -1 (position not found in any connected monitor). We think that GetPosition should not return negative values on x position due to it still being on the main display. SetPosition also has similar behavior where setting a value of 0,0 puts the window on upper left of screen but shifted to the right by a few pixels.

Expected vs observed behaviour
When a window is at left side of the screen, GetPosition should return 0... instead of negative values in Windows. When SetPosition(0,0) is set on Window, it should be placed exactly at the upper left corner. Actual is it is placed on the upper left corner but it is shifted a few pixels to the right.

Patch or snippet allowing to reproduce the problem
Attached is a modified minimal.cpp (modified minimal sample project).
minimal.txt

To Reproduce
Steps to reproduce the behaviour, please make them as detailed as possible, e.g.

  1. Run minimal

  2. Move window to upper left of screen

  3. Press About

  4. Use debugger to check value returned by GetPosition().

  5. Close about window and check that window is placed exactly at upper left corner

Platform and version information

  • wxWidgets version you use: 3.1.7
  • wxWidgets port you use: wxMSW
  • OS and its version: Win10 19044


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/22547@github.com>

PB

unread,
Jun 17, 2022, 1:52:43 AM6/17/22
to wx-...@googlegroups.com, Subscribed

Expected vs observed behaviour When a window is at left side of the screen, GetPosition should return 0... instead of negative values in Windows.

I do not think this is correct. Windows uses invisible window borders, so even if the window appears to be aligned with the left screen client area edge, its position is in fact several pixels off to the left (it's 8 for me on Win10 @125% DPI scaling). See for example this thread on wxForum (including my code dealing with the borders towards the end of the thread): https://forums.wxwidgets.org/viewtopic.php?p=197789. Of course, this applies in the other direction as well.

FWIW, this is an actual minimal example showing the positioning issue

#include <wx/wx.h>

class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(nullptr, wxID_ANY, "Test")
    {
        new wxButton(this, wxID_ANY, "Move frame to (0,0)");
        Bind(wxEVT_BUTTON, [this](wxCommandEvent&) { SetPosition(wxPoint(0, 0)); } );

        m_timer.Start(250);        
        m_timer.Bind(wxEVT_TIMER, [this](wxTimerEvent&)
        {
            const wxPoint pos = GetPosition();

            SetTitle(wxString::Format("left = %d, top = %d", pos.x, pos.y));        
        });
    }    
private:
     wxTimer m_timer;
};

class MyApp : public wxApp
{
public:
    bool OnInit() override
    {
        (new MyFrame())->Show();
        return true;
    }
}; wxIMPLEMENT_APP(MyApp);

If one does not trust position values returned from wxWidgets, they are easy to verify with a tool such as Microsoft Spy++.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/22547/1158516748@github.com>

VZ

unread,
Jun 18, 2022, 11:32:22 AM6/18/22
to wx-...@googlegroups.com, Subscribed

Closed #22547 as not planned.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issue/22547/issue_event/6833268092@github.com>

VZ

unread,
Jun 18, 2022, 11:32:23 AM6/18/22
to wx-...@googlegroups.com, Subscribed

I agree that this isn't a bug. It might well be unexpected, but this is how MSW behaves and I don't think there is any reasonable way to "fix" it, we can't lie about the actual windows position.

I'd also strongly recommend using wxDisplay::GetFromWindow() if this is what you need instead of mucking with positions at all.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/issues/22547/1159484218@github.com>

Reply all
Reply to author
Forward
0 new messages