How can you in C # make the form resize depending on the size and position of the third-party window, knowing its hWnd? That is, you need to somehow create an event that would notify about it. Is it possible If you need to use hooks, then how to make such a C # hook?
- oneI think that in order not to make a garden, it is easier to call the WinAPI function of GetWindowRect. It is necessary, of course, to write boring DllImport, etc. - Alexey Sarovsky
- How width / height to learn is clear. How do you know if a third-party window has resized? This is the question. You can probably call GetWindowRect by timer, but somehow this is not very ... - egeo
- Look towards CallWindowProc . - BlackWitcher
- The windows of someone else's program? Without getting into the deep intestines - no way. With someone else's window IMHO easier and safer timer. - An a Student
|
1 answer
As a result, made based on the following code:
using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace ConsoleApplicationMy { class Program { [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags); [DllImport("user32.dll")] static extern bool UnhookWinEvent(IntPtr hWinEventHook); delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime); const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B; const uint WINEVENT_OUTOFCONTEXT = 0; static IntPtr myHandle; static WinEventDelegate procDelegate; static IntPtr hhook; static void Main(string[] args) { myHandle = FindWindow("Notepad", "Безымянный — Блокнот"); if (myHandle == IntPtr.Zero) { return; } procDelegate = new WinEventDelegate(WinEventProc); hhook = SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, IntPtr.Zero, procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT); MessageBox.Show(""); UnhookWinEvent(hhook); } static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { if (myHandle == hwnd) { Console.WriteLine(DateTime.Now.ToString()); } } } } |