public static WINDOWPLACEMENT GetWindowPlacement(IntPtr hwnd) { WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); WinUserApi.GetWindowPlacement(hwnd, ref wp); return(wp); }
public static Rectangle GetWindowRectangle(IntPtr hwnd) { RECT rect; WinUserApi.GetWindowRect(hwnd, out rect); return(rect.ToRectangle()); }
public static String GetWindowText(IntPtr handle) { StringBuilder windowText = new StringBuilder(256); WinUserApi.GetWindowText(handle, windowText, windowText.Capacity); return(windowText.ToString()); }
public static bool IsClass(IntPtr hwnd, String className) { StringBuilder sb = new StringBuilder(256); WinUserApi.GetClassName(hwnd, sb, sb.Capacity); return(sb.ToString() == className); }
public static String GetClassName(IntPtr handle) { StringBuilder className = new StringBuilder(256); WinUserApi.GetClassName(handle, className, className.Capacity); return(className.ToString()); }
public static Bitmap PrintWindow(IntPtr hwnd) { RECT windowRect; WinUserApi.GetWindowRect(hwnd, out windowRect); IntPtr hdc = WinUserApi.GetWindowDC(hwnd); Bitmap bmp = new Bitmap(windowRect.Width, windowRect.Height, PixelFormat.Format32bppArgb); Graphics gfxBmp = Graphics.FromImage(bmp); gfxBmp.FillRectangle(new SolidBrush(Color.FromArgb(0, 0, 0, 0)), new Rectangle(Point.Empty, bmp.Size)); IntPtr hdcBitmap = gfxBmp.GetHdc(); bool succeeded = WinUserApi.PrintWindow(hwnd, hdcBitmap, 0); gfxBmp.ReleaseHdc(hdcBitmap); if (!succeeded) { gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size)); } IntPtr hRgn = WinGdiApi.CreateRectRgn(0, 0, 0, 0); WinUserApi.GetWindowRgn(hwnd, hRgn); Region region = Region.FromHrgn(hRgn); if (!region.IsEmpty(gfxBmp)) { gfxBmp.ExcludeClip(region); gfxBmp.Clear(Color.FromArgb(0, 0, 0, 0)); } region.Dispose(); WinGdiApi.DeleteObject(hRgn); gfxBmp.Dispose(); WinUserApi.ReleaseDC(hwnd, hdc); return(bmp); }
public static POINT GetCursorLocation2() { POINT pt; WinUserApi.GetCursorPos(out pt); return(pt); }
public static void UpdateLayeredWindow(IntPtr hwnd, Point location, Bitmap bitmap, byte alpha) { try { BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AlphaChannelFlags.AC_SRC_OVER; blend.BlendFlags = 0; blend.AlphaFormat = AlphaChannelFlags.AC_SRC_ALPHA; blend.SourceConstantAlpha = alpha; POINT srcLocation = new POINT(); POINT dstLocation = POINT.FromPoint(location); SIZE dstSize = SIZE.FromSize(bitmap.Size); IntPtr hdc = WinUserApi.GetWindowDC(hwnd); IntPtr bmpDc = WinGdiApi.CreateCompatibleDC(hdc); IntPtr hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); IntPtr originalBitmap = WinGdiApi.SelectObject(bmpDc, hBitmap); WinUserApi.UpdateLayeredWindow(hwnd, IntPtr.Zero, ref dstLocation, ref dstSize, bmpDc, ref srcLocation, 0, ref blend, UpdateLayeredWindowFlags.ULW_ALPHA); WinGdiApi.SelectObject(bmpDc, originalBitmap); WinGdiApi.DeleteObject(hBitmap); WinGdiApi.DeleteDC(bmpDc); WinUserApi.ReleaseDC(hwnd, hdc); } catch { } }
public static int GetWindowProcessId(IntPtr hwnd) { int pid; WinUserApi.GetWindowThreadProcessId(hwnd, out pid); return(pid); }
public static bool GetMinMaxAnimation() { ANIMATIONINFO info = new ANIMATIONINFO(); info.cbSize = (uint)Marshal.SizeOf(typeof(ANIMATIONINFO)); WinUserApi.SystemParametersInfo(SystemParametersInfoParameters.SPI_GETANIMATION, 0, ref info, 0); return(info.iMinAnimate != 0); }
public static MONITORINFO GetMonitorInfo(IntPtr hMonitor) { MONITORINFO info = new MONITORINFO(); info.cbSize = Marshal.SizeOf(info); WinUserApi.GetMonitorInfo(hMonitor, ref info); return(info); }
public static GUITHREADINFO GetGUIThreadInfo(int tid) { GUITHREADINFO info = new GUITHREADINFO(); info.cbSize = Marshal.SizeOf(info); WinUserApi.GetGUIThreadInfo(tid, out info); return(info); }
public static void SetMinMaxAnimation(bool enabled) { ANIMATIONINFO info = new ANIMATIONINFO(); info.cbSize = (uint)Marshal.SizeOf(typeof(ANIMATIONINFO)); info.iMinAnimate = enabled ? 1 : 0; WinUserApi.SystemParametersInfo(SystemParametersInfoParameters.SPI_SETANIMATION, 0, ref info, SPIF.SPIF_UPDATEINIFILE); }
protected override void OnStateChanged(EventArgs e) { base.OnStateChanged(e); if (WindowState == WindowState.Maximized) { //窗口的ResizeMode设置为CanResizeWithGrip时,最大化窗口时默认位置不在屏幕左上角,调整屏幕左上角位置为:0,0 WinUserApi.MoveWindow(handle, 0, 0, (int)MaxWidth, (int)MaxHeight, false); } }
public static void FlashWindow(IntPtr hwnd, uint flags, int count) { FLASHWINFO fInfo = new FLASHWINFO(); fInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(fInfo)); fInfo.hwnd = hwnd; fInfo.dwFlags = flags; fInfo.uCount = (uint)count; fInfo.dwTimeout = 0; WinUserApi.FlashWindowEx(ref fInfo); }
public static bool IsKeyPressed(int vkCode) { if (WinUserApi.GetSystemMetrics(SystemMetrics.SM_SWAPBUTTON) != 0) { if (vkCode == VirtualKeyCodes.VK_LBUTTON) { vkCode = VirtualKeyCodes.VK_RBUTTON; } else if (vkCode == VirtualKeyCodes.VK_RBUTTON) { vkCode = VirtualKeyCodes.VK_LBUTTON; } } return((WinUserApi.GetAsyncKeyState(vkCode) & 0x8000) != 0); }
public static IntPtr GetWindowIcon(IntPtr handle, int iconType) { IntPtr icon; icon = WinUserApi.SendMessage(handle, WindowMessages.WM_GETICON, (IntPtr)iconType, IntPtr.Zero); if (icon == IntPtr.Zero) { if (iconType == IconTypeCodes.ICON_SMALL) { icon = WinUserApi.SendMessage(handle, WindowMessages.WM_GETICON, (IntPtr)IconTypeCodes.ICON_SMALL2, IntPtr.Zero); } icon = WinUserApi.GetClassLong(handle, iconType == IconTypeCodes.ICON_SMALL ? ClassLongFieldOffset.GCL_HICONSM : ClassLongFieldOffset.GCL_HICON); } return(icon); }
/// <summary> /// 显示系统菜单 /// </summary> /// <param name="window"></param> /// <param name="physicalScreenLocation"></param> private void ShowSystemMenuPhysicalCoordinates(Window window, Point physicalScreenLocation) { if (window == null) { return; } var hwnd = new WindowInteropHelper(window).Handle; if (hwnd == IntPtr.Zero || !WinUserApi.IsWindow(hwnd)) { return; } var hmenu = WinUserApi.GetSystemMenu(hwnd, false); //模式窗口只保留移动和关闭菜单 WinUserApi.EnableMenuItem(hmenu, WinUserApi.SC_MOVE, WinUserApi.MF_BYCOMMAND | WinUserApi.MF_ENABLED); WinUserApi.DeleteMenu(hmenu, WinUserApi.SC_RESTORE, WinUserApi.MF_BYCOMMAND); WinUserApi.DeleteMenu(hmenu, WinUserApi.SC_SIZE, WinUserApi.MF_BYCOMMAND); WinUserApi.DeleteMenu(hmenu, WinUserApi.SC_MINIMIZE, WinUserApi.MF_BYCOMMAND); WinUserApi.DeleteMenu(hmenu, WinUserApi.SC_MAXIMIZE, WinUserApi.MF_BYCOMMAND); #region 修改菜单名称,支持国际化 int pos = 0; //菜单项目的位置 int menuid = WinUserApi.GetMenuItemID(hmenu, pos); //菜单ID WinUserApi.ModifyMenu(hmenu, (uint)pos, WinUserApi.MF_BYPOSITION, (uint)menuid, MoveMenuContent); pos++; pos++; menuid = WinUserApi.GetMenuItemID(hmenu, pos); WinUserApi.ModifyMenu(hmenu, (uint)pos, WinUserApi.MF_BYPOSITION, (uint)menuid, CloseMenuContent); #endregion var cmd = WinUserApi.TrackPopupMenuEx(hmenu, WinUserApi.TPM_LEFTBUTTON | WinUserApi.TPM_RETURNCMD, (int)physicalScreenLocation.X, (int)physicalScreenLocation.Y, hwnd, IntPtr.Zero); if (0 != cmd) { WinUserApi.PostMessage(hwnd, WinUserApi.WM_SYSCOMMAND, new IntPtr(cmd), IntPtr.Zero); } }
private void MouseHook_MouseClickEvent(object sender, System.Windows.Forms.MouseEventArgs e) { if (this.Owner == null) { return; } //通过判断点击窗口的句柄是否是父窗口的句柄来判断是否点击的是父窗口 m_parentWindowHandle = new WindowInteropHelper(this.Owner).Handle; m_clickWindowHandle = WinUserApi.WindowFromPoint(e.X, e.Y); //如果点击的是父窗口,则闪烁模态窗口 if (m_clickWindowHandle.ToString().Equals(m_parentWindowHandle.ToString())) { m_flashWindowAnimation.Begin(); this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, new Action(() => { this.Activate(); })); } }
public static void SetOwner(IntPtr hwnd, IntPtr hwndOwner) { WinUserApi.SetWindowLong(hwnd, WindowLongFieldOffset.GWL_HWNDPARENT, hwndOwner); }
public IntPtr[] Execute() { WinUserApi.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, new MONITORENUMPROC(MonitorEnumProc), IntPtr.Zero); return(Monitors.ToArray()); }
public static void UpdateLayeredWindow(IntPtr hwnd, Point location) { POINT dstLocation = POINT.FromPoint(location); WinUserApi.UpdateLayeredWindow(hwnd, IntPtr.Zero, ref dstLocation, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, 0, IntPtr.Zero, 0); }
public static IntPtr GetTopLevelWindowFromPoint(Point pt) { IntPtr hwnd = WinUserApi.WindowFromPoint(POINT.FromPoint(pt)); return(WinUserApi.GetAncestor(hwnd, GetAncestorConstants.GA_ROOT)); }
public IntPtr[] Execute() { WinUserApi.EnumWindows(new WNDENUMPROC(GetWindowsInZOrderProc), IntPtr.Zero); return(Hwnds.ToArray()); }
public static void SetWindowRectangle(IntPtr hwnd, Rectangle rect) { WinUserApi.MoveWindow(hwnd, rect.X, rect.Y, rect.Width, rect.Height, true); }
public static POINT ClientToScreen(IntPtr hwnd, POINT pt) { WinUserApi.ClientToScreen(hwnd, ref pt); return(pt); }
public static POINT ScreenToClient(IntPtr hwnd, POINT pt) { WinUserApi.ScreenToClient(hwnd, ref pt); return(pt); }