I'd like to create an application where I can resize Windows forms.
I can iterate through windows and get certain properties like so for example:
How to iterate through window elements using JNA?
I've found similar ones, but can I resize and relocate these windows rather than just getting their attributes like title and rect? I'd like to sort all visible windows as a tile but I can't figure out how I can manipulate their position.
For example, I want to grab and resize Total Commander and Notepadd++ (the 2 visible windows) to be on the left and right half of the screen.
Is it possible? If anyone can show me a snippet I'd be grateful.
Easiest way would be to enumerate all window instances in system and call proper WinAPI function like described here
C#: You can obtain all the windows you can use this code:
internal static class WindowsEnumeratorNativeMethods {
[return: MarshalAs(UnmanagedType.Bool)]
internal delegate Boolean EnumerationCallback(IntPtr handle, IntPtr parameter);
[DllImport("User32.dll",
CallingConvention = CallingConvention.Winapi,
ExactSpelling = true,
EntryPoint = "EnumWindows",
CharSet = CharSet.Unicode,
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean EnumWindows(EnumerationCallback lpEnumFunc,
IntPtr lParam);
}
...
List<IntPtr> handles = new List<IntPtr>();
WindowsEnumeratorNativeMethods.EnumWindows(
(h, p) => {
handles.Add(h);
return true;},
IntPtr.Zero);
Next you should find out the required windows (Notepad++ etc). You can do it
by looking for windows' captions (see GetWindowText API function); to move/size
the window use SetWindowPos API function.
While implementing API wrappers, you may find this useful:
http://www.pinvoke.net/
Related
I'm building a universal Windows app and need to lock down the desktop so that the user can't escape out of the program. So I need to temporarily disable the Windows key and ctrl + alt + del.
I was able to add a event handler like so:
Window.Current.CoreWindow.KeyDown += (s, e) =>
{
if(e.VirtualKey == VirtualKey.LeftWindows || e.VirtualKey == VirtualKey.RightWindows)
{
e.Handled = true;
}
}
But the keypress event still fires.
I also found a method using what I believe are Windows hooks from the user32 lib.
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterHotKey(IntPtr hwnd, int id, uint fsModifiers, uint vk);
[DllImport("user32", SetLastError = true)]
public static extern int UnregisterHotKey(IntPtr hwnd, int id);
[DllImport("kernel32", SetLastError = true)]
I found some sample code here, but I think I'll have to tweak it for a simple Windows key press.
Is the Windows hook method (as used in the example link) the way to go?
I think Kiosk apps for assigned access was very close to your requirement. This document describes how to implement a kiosk app. You could use the Lock framework and assigned access to create a kiosk app that enables users to interact with a single app on a device.
Set up a kiosk on Windows 10 Pro, Enterprise, or Education for your reference.
I am trying to destroy a child window created by a third party application. I tried to use WinApi to make this happen but it seems that DestroyWindow(hwnd) doesn't do the trick. I indeed get a handle to work with but nothing happens, the child window remains. I tried to use CloseWindow(hwnd) just to verify the child window can be modified and yes, CloseWindow(hwnd) minimizes the Child Window but I want to close it.
This is my Code:
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseWindow(IntPtr hWnd);
private static void CloseChildWindow();
{
IntPtr _hwnd = IntPtr.Zero;
if (WindowController.FindWindow(null, "System Alert") != IntPtr.Zero)
{
_hwnd = WindowController.FindWindow(null, "System Alert");
DestroyWindow(_hwnd); //Returns error code 5: Access Denied
CloseWindow(_hwnd); //Minimizes the window therefore _hwnd is correct
}
}
Any ideas on what is happening or a workaround would be appreciated, thanks in advance.
EDIT1: Destroywindow returns code error 5: Access Denied. Any ideas on a workaround?
The documentation for the win32 API DestroyWindow is pretty clear:
A thread cannot use DestroyWindow to destroy a window created by a
different thread.
You may be able to inject a DLL in the third party application, hooking the correct thread and then issuing the DestroyWindow call from here, but I will not recommand that, as it may have weird side effects (read: crash). Some applications don't react well when their windows are destroyed under their feet.
The proper way is probably to fake an user interaction, using regular Windows message (as WM_CLOSE), or SendInput, or UI Automation.
If the user has no way to close that window, you are out of luck.
I am attempting to scrape data from a datagrid inside a windows application.
I have successfully scraped the data from textboxes in the application using the User32.dll and its methods such as GetWindow to find the handles and then using SendMessage to read the content.
[DllImport("user32.dll", EntryPoint = "GetWindow", SetLastError = true)]
internal static extern IntPtr GetWindow(IntPtr lpWindHandle, uint lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
My problem is that these methods don't seem to return any values for a DataGrid or table.
Does anyone have any advice on how I might be able to achieve this?
This is possible by using UI Automation API. I hope these links help you find the needed solution: Discussion of similar problem,UI Automation Support for the DataGrid Control Type. Although specific solution depends on many implementation details of the datagrid.
Since getting a satisfactory answer on SuperUser is very difficult, I want to rephrase this question and ask:
Is there any way to programmatically detect a mouse was plugged in the usb port, and change the cursor speed in windows (perhaps through an API)?
I'd like to use C#, but I'm open to any language that can run on a windows 7 machine.
I don't know about the detection but you can use P/Invoke to the SystemParametersInfo api using
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, String pvParam, SPIF fWinIni);
with the uiAction as (SPI_SETMOUSESPEED) = 0x0071
I want to send a pressKey event to a certain application which is not the active application IN Windows so I have to use the sendMessage/postMessage api calls.
However, I need to know the exact child window that is active IN the application and send the pressKey message to it...
I was using GetTopWindow and GetWindow(GW_CHILD) api calls to get the top child window of the main window, and do it again with the obtained child window to get the top grandchildWindow, and keep doing it until I found a childwindow with no more childwindows. This works great for some applications but in some cases it doesn't. Sometimes the parent window is the active window, not one of its childwindows, so getting the parent's top child window will not work cause I will be sending a message to the wrong window.
The only way I found of doing this (getting the handler of the actual active window) was using the GuiThreadInfo api call but it only works if the target application is the active one IN Windows. As I mentioned in the beginning, it isn't so the handler comes null.
I can bring the application to the top using setForegroundWindow api call but I DON'T want to do this. I also tried the AttachThreadInput and GetFocus api calls, but again, they only work if the target application is the active application IN windows.
Any ideas? Thanks
I assume from the things that you have tried that you know how to get a handle to your main window, but if you don't just leave a comment and I will post a snippet for that.
I combined a few things that I found on the net to figure this out, but the main one is this one. I don't have a great app to test this with, but it works in a simple case. One exception is that I think if you use tool windows in your application it will not find that as it is coded because I think the GetLastActivePopup method doesn't include them (not sure about that, and didn't test that case).
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetLastActivePopup(IntPtr hWnd);
[DllImport("user32.dll", ExactSpelling = true)]
static extern IntPtr GetAncestor(IntPtr hwnd, uint gaFlags);
const uint GA_PARENT = 1;
const uint GA_ROOT = 2;
const uint GA_ROOTOWNER = 3;
public static IntPtr GetAppActiveWindow(IntPtr hwnd)
{
IntPtr activeAppWindow = IntPtr.Zero;
if (hwnd != IntPtr.Zero)
{
//Get the root owner window (make sure we are at the app window
//if you already have a handle to the main window shouldn't have
//to do this but I put it in just in case
hwnd = GetAncestor(hwnd, GA_ROOTOWNER);
while ((activeAppWindow =
GetLastActivePopup(hwnd)) != activeAppWindow)
{
if (IsWindowVisible(activeAppWindow))
break;
hwnd = activeAppWindow;
}
}
return activeAppWindow;
}
If you know the Window title and the Window class name, take a look at FindWindow() and FindWindowEx() and see if those meet your needs.
FindWindow(): http://msdn.microsoft.com/en-us/library/ms633499.aspx
FindWindowEx(): http://msdn.microsoft.com/en-us/library/ms633500(VS.85).aspx