c# - SetWindowPos moves window to different position every time -
i have windows application (which writing in c#) starts maximized window without borders.
when user clicks on button in application, want restore window (that is, remove maximized state), resize size , move bottom-right corner of screen.
my problem call setwindowpos(), while resizing window correctly, not place in bottom-right corner of screen. does, yet other times window placed elsewhere on screen (almost if "jumping" around, reason ignore).
what doing wrong?
here code. note pass -1 second parameter setwindowpos because want window on top of every other window.
public void moveappwindowtobottomright() { process process = process.getcurrentprocess(); intptr handler = process.mainwindowhandle; showwindow(handler, 9); // sw_restore = 9 int x = (int)(system.windows.systemparameters.primaryscreenwidth - 380); int y = (int)(system.windows.systemparameters.primaryscreenheight - 250); nativepoint point = new nativepoint { x = x, y = y }; clienttoscreen(handler, ref point); setwindowpos(handler, -1, point.x, point.y, 380, 250, 0); } [structlayout(layoutkind.sequential)] public struct nativepoint { public int x; public int y; }
you should remove these lines:
nativepoint point = new nativepoint { x = x, y = y }; clienttoscreen(handler, ref point); and change call to:
setwindowpos(handler, -1, x, y, 380, 250, 0); calling clienttoscreen() makes no sense coordinates have screen coordinates.
your window gets different positions every time because when call clienttoscreen() create new coordinates based on window's current position. means every time function called coordinates different.
also, if want take taskbar size account should utilize screen.workingarea property instead of systemparameters.primaryscreen***:
int x = (int)(screen.primaryscreen.workingarea.width - 380); int y = (int)(screen.primaryscreen.workingarea.height - 250);
Comments
Post a Comment