does anyone know if you can get the coordinates of a window using the .NET framework or via pinvoking?
I would have the processID or mainwindowhandle.
HI, you can use
System.Windows.Forms.Control cr= System.Windows.Forms.Control.FromHandle(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
now u can get location of that control.
In Windows Forms API, form.Top and form.Left should do.
If all you have is the process Id, you can iterate through the process's widows using EnumWindowsProc windows API method. once you get the window's handle which you want, you can query for its size and position.
Related
Using Windows API, is there a way to get the screen where is displayed the current active window.
The rough steps are:
GetActiveWindow / GetForegroundWindow
GetWindowRect
GetDC
CreateCompatibleDC
CreateCompatibleBitmap
BitBlt
The short answer is the API cant do this directly. A given window can be interacted with through a device context, but there is no way of determining which monitor in a multi-monitor system that window is displayed on with an API call. Indeed it may be displayed on more than one.
The long answer is you can calculate it by retrieving the window's physical location on the desktop GetWindowRect(), converting it to screen coordinates with ClientToScreen() , and then calculating which of the enumerated display devices it is on.
I dont have the rep to post more than 2 links, but MSDN has the functions listed.
GetDC()
GetWindowRect()
ClientToScreen() - https://msdn.microsoft.com/en-us/library/windows/desktop/dd183434(v=vs.85).aspx
EnumDisplayDevices()
EnumDisplaySettingsEx - https://msdn.microsoft.com/en-us/library/windows/desktop/dd162612(v=vs.85).aspx
I wonder if there is any way to somehow interact with windows that are currently open on a Windows system. I am interested in getting some of their properties, namely:
Location
Dimension
is in background?
possibly window title
Preferably, I would like to do that in Java but any suggestions are welcome.
A comment by theB linked to good resources for Java. I'll run through the relevant Windows APIs, in case you want to go native with C++.
To enumerate all the top-level windows in the system, use EnumWindows. You give it a callback function with the signature of EnumWindowsProc, so it will receive each window handle as the first parameter.
You can get the window location (in screen coordinates) and dimensions with the GetWindowRect function. Pass in the window handle you received and get an LPRECT (pointer to RECT) out.
To determine whether a window is maximized, use GetWindowPlacement and check the showCmd field of the WINDOWPLACEMENT structure you receive.
Finally, to get a window's caption, use GetWindowText. (As an aside, if you want to get the text of a control in another process, you'll need to send the WM_GETTEXT message yourself.)
Do you know how I can get the CGWindow Id of any focussed window (belonging or not to the current application) ?
Thanks in advance for your help :)
Regards,
One way is to use CGWindowListCopyWindowInfo to get the list of all windows like this:
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
Examine this list to find all the windows at window level 0 (these are the normal windows).
I'm fairly certain that the array returned will be in the order the windows are layered on the screen. If not, you can sort by the "windowOrder" key. Look at the SonOfGrab sample code for more on how to use this API.
I have creating one window using CreateWindow. But is there any api which can give system screen height and width of the system. So that it will very helpful to locate windows according to the system screen.
The GetDesktopWindow() function will give you a handle to the desktop window.
You can then query this for it's size using GetWindowRect()
Edit: Note that this will give you the size of the primary display. To handle multiple monitors you will need to use GetMonitorInfo()
To get the size of the workarea on the primary monitor, the call to make is SystemParametersInfo with SPI_GETWORKAREA.
Alternativly you can pass CW_USEDEFAULT (-1) as the x-co-ordinate and windows will pick a position for the window for you.
How can I get the icon of a running application provided I know the Hwnd?
If you have the handle to the window, you can use GetClassLong:
HICON icon = (HICON)GetClassLong(window, GCL_HICON);
I you have the hwnd you can get the process ID by using the WINAPI GetWindowThreadProcessId.
With that you can create a C# Process object. Next, you can iterate through the process' ProcessModule Collection to get the filename of the executable. Finally, you can use the WINAPI function ExtractIconEx to get the icon from the path
Pinvoke has information on the two WINAPI methods
http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html
http://www.pinvoke.net/default.aspx/shell32/ExtractIconEx.html