C
C#9mo ago
Florian Voß

❔ need help fixing Bug in WPF app

I got code to place windows on the screen where the MainWindow is located by grabbing it's handler. I then set window's top to the handler's top location and same for left. On most devices works as expected, window spawns on top left edge of mainwindow's screen. However on two clients out of roughly 20, there is a bug. On those clients, when opening on a certain screen of the three screens they're using, window spawns in nirvana, far more in the top RIGHT than visible. on the other two screen's they're using the window spawns in the center of screen on top rather than left top. On the other 18 clients, whatever screen you use it will always work as expected and spawn on top left edge of mainwindow's screen Those two clients facing the bugg have the exact same environment as the 18 clients where it works as expected, that is same Hardwares, same OS and same display settings. I'm lost how to debug this. there is no error in the event viewer. when you move the window using windows key + arrows it moves into appearance
public void PositionOnMainWindowScreen(Window window)
{
var mainwindowSource = PresentationSource.FromVisual(this) as HwndSource;
var mainWindowScreen = Screen.FromHandle(mainwindowSource.Handle);
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = mainWindowScreen.WorkingArea.Left;
window.Top = mainWindowScreen.WorkingArea.Top;
}
public void PositionOnMainWindowScreen(Window window)
{
var mainwindowSource = PresentationSource.FromVisual(this) as HwndSource;
var mainWindowScreen = Screen.FromHandle(mainwindowSource.Handle);
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = mainWindowScreen.WorkingArea.Left;
window.Top = mainWindowScreen.WorkingArea.Top;
}
7 Replies
Florian Voß
Florian Voß9mo ago
I can't even tell if its my code that needs to be improved or whether its OS issue or possibly even hardware issue. I'm very lost on this ^^' I can't find a difference worth mentioning between the 2 clients facing the bug and the 18 clients not facing it
Bailey
Bailey9mo ago
Hi, I really do not know if the following helps. In the pst I had somekind of the same issue. The cause was using different screensizes monitors when entering the server. The only option was to set the windows position with the hwnd. No error or something just behaviour of windows remembering where to put a window. When I remember correctly using the GetWindowRect. However when a window is minimized, Position is incorrect,
Insire
Insire9mo ago
whats the requirement here? is the new window supposed to open anywhere on the main screen, or is top left of mainscreen non ngeotiable? in terms of debugging, you start with logging everything relevant: screen resolutions, window sizes, states(maximized, minimized, normal) and positions for both windows also, track whether a window is in the middle of closing
Jester
Jester9mo ago
the only think i can think of is that maybe you have an issue with - and + in some math. screen coordinates can be negative with multiple displays
Florian Voß
Florian Voß9mo ago
the monitors are in fact having different screen resolutions. But that should not be producing said error, the clients where it works as expected have that too.
The only option was to set the windows position with the hwnd. No error or something just behaviour of windows remembering where to put a window. When I remember correctly using the GetWindowRect. However when a window is minimized, Position is incorrect
I don't understand this part, could you explain again pls? its supposed to spawn on the screen where mainwindow is located, which can be any screen, doesn't have to be the mainscreen. Spawning top left is not a requirement, thats what I chose because it looked the best I have compared all of that and everything mentioned here is same for all the clients except the positions of the windows ofc I'm not doing any math with coordinates. The code I've shown is the only code in my program responsible for screen positioning
Insire
Insire9mo ago
if you just care that the new window is on the same screen as the mainwindow, you can try
public static void PositionOnMainWindowScreen(Window window)
{
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = System.Windows.Application.Current.MainWindow.Left;
window.Top = System.Windows.Application.Current.MainWindow.Top;
}
public static void PositionOnMainWindowScreen(Window window)
{
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Left = System.Windows.Application.Current.MainWindow.Left;
window.Top = System.Windows.Application.Current.MainWindow.Top;
}
or pass in another window reference. you can also play around with setting the window owner and adding WindowStartupLocation.CenterOwner; i usually try to stay away from using OS native stuff, if i havent read its docs, which in this i didnt
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.