c# - Given a window, how can I determine if it is part of a winforms application? -



c# - Given a window, how can I determine if it is part of a winforms application? -

i have handles main form of winforms application, , window i'm trying check (which may or may not part of application). i've tried iterating using getparent, doesn't seem work.

what i'm trying observe modal window (such msgbox), it's controls, , send button click message if controls fulfill requirements (like button).

now, while can observe if modal window open, , can find focused window, have no thought if focused window modal window detected. essentially, if open model window , open different program, tries find controls of external program.

the code below:

if (pf.visible && !pf.canfocus) //is modal window { ///todo: check if active window kid of main window ///gets info of active window string currentactivewindow = getactivewindowtitle(); intptr currentactivehandle = getactivewindowhandle(); ///gets 'children' or controls of active window var hwndchild = enumallwindows(currentactivehandle); ///iterate on kid windows foreach (intptr element in hwndchild) { const int nchars = 256; stringbuilder buff = new stringbuilder(nchars); intptr handle = getforegroundwindow(); string windowelementtype = getwindowclassname(element); ///check if "windows" buttons if (getwindowtext(element, buff, nchars) > 0 && windowelementtype=="button") { string windowelement = buff.tostring(); if (windowelement.tolower()=="ok") { ///send button click message const int bm_click = 0x00f5; sendmessage(element, bm_click, intptr.zero, intptr.zero); } } } }

a convenience function (c++) determine whether 2 windows identified hwnd belong same process this:

bool ownedbysameprocess(hwnd hwnd1, hwnd hwnd2) { if ( ::iswindow(hwnd1) && ::iswindow(hwnd2) ) { dword procid1 = 0x0; dword procid2 = 0x0; ::getwindowthreadprocessid(hwnd1, &procid1); ::getwindowthreadprocessid(hwnd2, &procid2); homecoming ( procid1 == procid2 ); } homecoming false; }

the getwindowthreadprocessid not subject uipi (user interface privilege isolation) , succeed given valid input. homecoming values ids , not need cleaned up.

translated c#:

public class helper { [dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool iswindow(intptr hwnd); [dllimport("user32.dll")] static extern uint getwindowthreadprocessid(intptr hwnd, out uint lpdwprocessid); public static bool ownedbysameprocess(intptr hwnd1, intptr hwnd2) { if ( !iswindow(hwnd1) ) throw new argumentexception("hwnd1"); if ( !iswindow(hwnd2) ) throw new argumentexception("hwnd2"); uint procid1 = 0; getwindowthreadprocessid(hwnd1, out procid1); uint procid2 = 0; getwindowthreadprocessid(hwnd2, out procid2); homecoming ( procid1 == procid2 ); } }

c# .net winforms winapi

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -