c# - How do i use SetWindowPos? -
intptr handle = process.mainwindowhandle; if (handle != intptr.zero) { setwindowpos(handle, 0, 0, 0, 0, 0, swp_nozorder | swp_nosize | swp_showwindow); } then when call in constructor example setwindowpos should give ? handle fine know should be. resr 0,0,0,0,0,0 , should values fro swp_nozorder , swp_nosize ?
what want bring handle front , center of screen. bring front know how i'm using setforegroundwindow(intptr hwnd); , it's working fine. how use setwindowpos force in center of screen ?
before can center it, first must know how big is. can accomplished getwindowrect() api. after it's matter of calculating center position taking account size of screen:
public partial class form1 : form { [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool getwindowrect(intptr hwnd, out rect lprect); [structlayout(layoutkind.sequential)] public struct rect { public int left; // x position of upper-left corner public int top; // y position of upper-left corner public int right; // x position of lower-right corner public int bottom; // y position of lower-right corner } private const int swp_nosize = 0x0001; private const int swp_nozorder = 0x0004; private const int swp_showwindow = 0x0040; [dllimport("user32.dll", setlasterror=true)] static extern bool setwindowpos(intptr hwnd, intptr hwndinsertafter, int x, int y, int cx, int cy, int uflags); process process; public form1() { initializecomponent(); process = process.getprocessesbyname("calc").firstordefault(); } private void button1_click(object sender, eventargs e) { if (process == null) return; intptr handle = process.mainwindowhandle; if (handle != intptr.zero) { rect rct; getwindowrect(handle, out rct); rectangle screen = screen.fromhandle(handle).bounds; point pt = new point(screen.left + screen.width / 2 - (rct.right - rct.left) / 2, screen.top + screen.height / 2 - (rct.bottom - rct.top) / 2); setwindowpos(handle, intptr.zero, pt.x, pt.y, 0, 0, swp_nozorder | swp_nosize | swp_showwindow); } } }
Comments
Post a Comment