Exemplo n.º 1
0
        public static WINDOWPLACEMENT GetWindowPlacement(IntPtr hwnd)
        {
            WINDOWPLACEMENT wp = new WINDOWPLACEMENT();

            WinUserApi.GetWindowPlacement(hwnd, ref wp);
            return(wp);
        }
Exemplo n.º 2
0
        public static Rectangle GetWindowRectangle(IntPtr hwnd)
        {
            RECT rect;

            WinUserApi.GetWindowRect(hwnd, out rect);
            return(rect.ToRectangle());
        }
Exemplo n.º 3
0
        public static String GetWindowText(IntPtr handle)
        {
            StringBuilder windowText = new StringBuilder(256);

            WinUserApi.GetWindowText(handle, windowText, windowText.Capacity);
            return(windowText.ToString());
        }
Exemplo n.º 4
0
        public static bool IsClass(IntPtr hwnd, String className)
        {
            StringBuilder sb = new StringBuilder(256);

            WinUserApi.GetClassName(hwnd, sb, sb.Capacity);
            return(sb.ToString() == className);
        }
Exemplo n.º 5
0
        public static String GetClassName(IntPtr handle)
        {
            StringBuilder className = new StringBuilder(256);

            WinUserApi.GetClassName(handle, className, className.Capacity);
            return(className.ToString());
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public static POINT GetCursorLocation2()
        {
            POINT pt;

            WinUserApi.GetCursorPos(out pt);
            return(pt);
        }
Exemplo n.º 8
0
 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
     {
     }
 }
Exemplo n.º 9
0
        public static int GetWindowProcessId(IntPtr hwnd)
        {
            int pid;

            WinUserApi.GetWindowThreadProcessId(hwnd, out pid);
            return(pid);
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        public static MONITORINFO GetMonitorInfo(IntPtr hMonitor)
        {
            MONITORINFO info = new MONITORINFO();

            info.cbSize = Marshal.SizeOf(info);
            WinUserApi.GetMonitorInfo(hMonitor, ref info);
            return(info);
        }
Exemplo n.º 12
0
        public static GUITHREADINFO GetGUIThreadInfo(int tid)
        {
            GUITHREADINFO info = new GUITHREADINFO();

            info.cbSize = Marshal.SizeOf(info);
            WinUserApi.GetGUIThreadInfo(tid, out info);
            return(info);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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);
            }
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
 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);
 }
Exemplo n.º 17
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);
        }
Exemplo n.º 18
0
        /// <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);
            }
        }
Exemplo n.º 19
0
        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(); }));
            }
        }
Exemplo n.º 20
0
 public static void SetOwner(IntPtr hwnd, IntPtr hwndOwner)
 {
     WinUserApi.SetWindowLong(hwnd, WindowLongFieldOffset.GWL_HWNDPARENT, hwndOwner);
 }
Exemplo n.º 21
0
 public IntPtr[] Execute()
 {
     WinUserApi.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, new MONITORENUMPROC(MonitorEnumProc), IntPtr.Zero);
     return(Monitors.ToArray());
 }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
        public static IntPtr GetTopLevelWindowFromPoint(Point pt)
        {
            IntPtr hwnd = WinUserApi.WindowFromPoint(POINT.FromPoint(pt));

            return(WinUserApi.GetAncestor(hwnd, GetAncestorConstants.GA_ROOT));
        }
Exemplo n.º 24
0
 public IntPtr[] Execute()
 {
     WinUserApi.EnumWindows(new WNDENUMPROC(GetWindowsInZOrderProc), IntPtr.Zero);
     return(Hwnds.ToArray());
 }
Exemplo n.º 25
0
 public static void SetWindowRectangle(IntPtr hwnd, Rectangle rect)
 {
     WinUserApi.MoveWindow(hwnd, rect.X, rect.Y, rect.Width, rect.Height, true);
 }
Exemplo n.º 26
0
 public static POINT ClientToScreen(IntPtr hwnd, POINT pt)
 {
     WinUserApi.ClientToScreen(hwnd, ref pt);
     return(pt);
 }
Exemplo n.º 27
0
 public static POINT ScreenToClient(IntPtr hwnd, POINT pt)
 {
     WinUserApi.ScreenToClient(hwnd, ref pt);
     return(pt);
 }