Tag: 句柄

获取顶部窗口的句柄(按Z索引排序窗口)

我正在尝试编写一个方法,该方法接受窗口句柄列表并返回具有最高z索引的窗口的句柄等。 但徒劳无功。 任何人都可以给我一个建议怎么做?

如何在应用程序外更改表单的窗口样式?

如何更改应用程序外的表单的窗口样式?难题? 我实际上是试图移动一个forms女巫是最顶层,没有边界。 我有窗口的句柄(hWnd)。 如果保证可以工作,我可以编写数千行代码。

通过句柄获取窗口的可执行路径 – 拒绝访问

我试图通过Handle获得Window的可执行路径。 我使用以下代码来实现: [DllImport(“user32.dll”)] private static extern int GetWindowThreadProcessId(IntPtr handle, out uint processId); public string GetFilePath(IntPtr hwnd) { try { uint pid = 0; GetWindowThreadProcessId(hwnd, out pid); Process proc = Process.GetProcessById((int)pid); //Gets the process by ID. return proc.MainModule.FileName.ToString(); //Returns the path. } catch (Exception ex) { return ex.ToString(); } } 并且它工作正常,除了一些应用程序(例如TeamSpeak 3 64bit [如果重要])。 如何以编程方式克服Access Denied问题? […]

c#MainWindowHandle始终为零

我读了一些关于MainWindowHandle的线程,但是我找不到解决我的问题的方法,我正在启动一个gui应用程序,并希望通过进程对象获取MainWindowHandle,但如果我不打算将句柄值始终为零等待thread.sleep()直到gui加载。 我也尝试使用WaitForInputIdle,但它根本没有帮助。 process.Start(); process.WaitForInputIdle(1000); while (process.MainWindowHandle == IntPtr.Zero) { Thread.Sleep(100); } // do something with the handle 他永远不会离开,如果我用正常的thread.sleep替换waitforinputidle他会把握正确的句柄。 用简单的话说:我只想继续我的代码,如果我得到一个句柄!=零,但我不想等待一个静态的时间

我不能通过c#中的句柄设置窗口的透明度?

我试图设置所有窗口的透明度。 我有以下代码。 public partial class Form1 : Form { [DllImport(“user32.dll”)] static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport(“user32.dll”)] static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport(“user32.dll”)] static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); public const int GWL_EXSTYLE = -20; public const int WS_EX_LAYERED = 0x80000; public const int LWA_ALPHA […]

SafeFileHandle.Close抛出exception,但句柄有效且有效

我在WPF应用程序中遇到自定义游标的问题。 我使用以下代码来创建Cursor对象: [DllImport(“user32.dll”)] private static extern IntPtr CreateIconIndirect(ref IconInfo icon); [DllImport(“user32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo); private static Cursor CreateCursor(string cursorName, System.Drawing.Bitmap bmp, int xHotspot, int yHotspot, out SafeFileHandle handle) { IconInfo tmp = new IconInfo(); GetIconInfo(bmp.GetHicon(), ref tmp); tmp.xHotspot = xHotspot; tmp.yHotspot = yHotspot; tmp.fIcon = false; IntPtr […]

如何在C#中找到Mutex?

如何从C#中的互斥锁中找到获取互斥锁的? 当mutex.WaitOne(timeout)超时时,它返回false 。 但是,如何从互斥锁手柄中找到它? (也许使用p / invoke。) 更新 : public class InterProcessLock : IDisposable { readonly Mutex mutex; public bool IsAcquired { get; private set; } public InterProcessLock(string name, TimeSpan timeout) { bool created; var security = new MutexSecurity(); security.AddAccessRule(new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.Synchronize | MutexRights.Modify, AccessControlType.Allow)); mutex = new Mutex(false, name, out created, security); […]

使用Graphics.FromHwnd在屏幕上绘图和清除

我正在尝试创建一个程序,它获取光标下窗口的句柄,显示有关它的一些数据,并在整个窗口的顶部绘制一个填充矩形(具有非常低的alpha)。 我正在使用C#和winforms。 我已经成功了,但问题是我的draw方法是在BackgroundWorker的循环中并且它不断在窗口上制作越来越多的矩形( – >具有更高alpha的矩形)或者当将鼠标移动到另一个窗口时旧的仍然存在。 我没有设法找到一个方法来清除绘制的矩形,因为它只是在屏幕上“是”,并没有绑定到图形对象或任何东西。 我尝试过使用某些原生方法,比如 [DllImport(“User32.dll”)] public static extern Int64 SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport(“user32.dll”)] public static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase); [DllImport(“user32.dll”)] public static extern bool UpdateWindow(IntPtr hWnd); [DllImport(“user32.dll”)] public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags); 但以上都没有正常工作。 它们中的一些确实有效,但是当消息进入队列时,重绘不会立即发生,或者非常缓慢和闪烁(闪烁等)。 所以,问题是,如何使用Graphics.FromHwnd(handleOfWindowUnderCursor)“删除”我绘制的矩形? 我实际上认为它在其他窗口上绘制并不重要,因为我在试图摆脱我自己的forms上的图纸时也遇到了同样的问题(从来没有得到那个固定!)。 […]

获取没有标题的窗口的句柄..(C#)

我们如何获得没有标题的窗口的句柄? 有没有办法枚举桌面上的所有窗口并过滤没有标题的窗口(在我的情况下,只有一个)并获取它的句柄..或通过指定其他属性,如一个窗口,特定的按钮或列表框等…

为什么GetWindowText会挂起一个“关闭”的句柄而不是一个随机句柄

使用以下代码 [DllImport(“user32.dll”, EntryPoint = “GetWindowText”, ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount); public static String GetWindowText(IntPtr hWnd) { StringBuilder title = new StringBuilder(MAX_TITLE_LENGTH); int titleLength = WinAPI.GetWindowText(hWnd, title, title.Capacity + 1); title.Length = titleLength; return title.ToString(); } 如果将句柄传递给最近关闭的应用程序,GetWindowText将挂起(IE:永不返回)。 (这对我来说很奇怪,因为我原以为它会以零值返回) 传入随机句柄(如new IntPtr(123456)成功并返回无值。 有人可以解释一下这种行为吗?