c# - How to place a windows form in screen coords and close it on command? -
i working on c# console program on windows move window, part of trying create transparent box @ target destination give user visual confirmation of new location. facing 2 main problems:
- i want place form in screen coordinates
- i want able close window after user has confirmed decision.
currently have code in library console client calling this:
public static void createbox(rect rectangle) { form f = new form(); f.name = boxname; f.backcolor = color.blue; f.formborderstyle = formborderstyle.none; f.bounds = new system.drawing.rectangle(rectangle.left, rectangle.top, rectangle.right - rectangle.left, rectangle.bottom - rectangle.top); f.topmost = true; f.opacity = 0.5; application.enablevisualstyles(); task.run(() => application.run(f)); }
and after searching questions on here have come attempt close form later:
public static void removebox() { form f = application.openforms[boxname]; if (f != null) { f.close(); } }
this throwing exception coming different thread, how can close window, , how can place in screen coordinates should go?
edit:
i using attempt find box move/close unsuccessfully:
public static void createbox(rect rectangle) { form f = new form(); f.name = boxname; f.backcolor = color.aliceblue; f.formborderstyle = formborderstyle.none; f.topmost = true; f.opacity = 0.3; application.enablevisualstyles(); task.run(() => { application.run(f); }); movebox(rectangle); } public static void removebox() { intptr hwnd = findbox(new timespan(0, 0, 1)); var proc = process.getprocesses().where(p => p.handle == hwnd).single(); if (proc == null) { return; } proc.kill(); } public static void movebox(rect rect) { intptr hwnd = findbox(new timespan(0, 0, 1)); movewindow(hwnd, rect); } private static intptr findbox(timespan timeout) { datetime time = datetime.now; intptr hwnd = intptr.zero; while(datetime.now < time.add(timeout) || hwnd != intptr.zero) { hwnd = findwindowbycaption(intptr.zero, boxname); } return hwnd; }
issues this:
i can't let findbox call take long @ because goal make box appear , snap windows them user drags them , needs move move around desktop.
the p.handle == hwnd check in removebox function throws , access denied exception.
if want working put: form.checkforillegalcrossthreadcalls = false;
into constructor, not recommend however, since make whole form no longer thread safe.
Comments
Post a Comment