c# - How to Get the Main Window of a process(not the window handle)? -
this question has answer here:
- get wpf window hwnd 4 answers
i have been trying reference main window of process quite sometime. went through many sites looking solution. looks provides solution window handle , not window itself.
getting process's mainwindow handle quite straight forward. seem window title expected.
i went through of apis provided user32.dll couldn't find looking for.
i need reference window because want access few members of window class accomplish work. window referring wpf window.
any in regard highly appreciated:)
thanks in advance.
you can use hwndsource.fromhwnd
method.
here's simple linqpad-ready example:
void main() { var mw = new mainw(); mw.show(); var hwnd = findwindowbycaption(intptr.zero, "testwindow"); var rootvisual = system.windows.interop.hwndsource.fromhwnd(hwnd).rootvisual; mainw m2 = (mainw)rootvisual; thread.sleep(500); m2.title="is going"; thread.sleep(500); m2.title="to close..."; thread.sleep(500); m2.close(); } [system.runtime.interopservices.dllimportattribute("user32.dll", entrypoint="findwindow", setlasterror = true)] static extern intptr findwindowbycaption(intptr zeroonly, string lpwindowname); class mainw: system.windows.window { public mainw() { title = "testwindow"; width = 230; height = 100; background = system.windows.media.brushes.aliceblue; } }
Comments
Post a Comment