Example #1
0
        private static string GetPrimaryMonitorDeviceName()
        {
            string name = null;

            if (SafeNativeMethods.GetSystemMetrics(NativeConstants.SM_CMONITORS) != 0)
            {
                NativeStructs.POINT point = new NativeStructs.POINT
                {
                    x = 0,
                    y = 0
                };

                IntPtr hMonitor = SafeNativeMethods.MonitorFromPoint(point, NativeConstants.MONITOR_DEFAULTTOPRIMARY);

                NativeStructs.MONITORINFOEX monitorInfo = new NativeStructs.MONITORINFOEX
                {
                    cbSize = (uint)Marshal.SizeOf(typeof(NativeStructs.MONITORINFOEX))
                };

                if (SafeNativeMethods.GetMonitorInfoW(hMonitor, ref monitorInfo))
                {
                    name = monitorInfo.szDeviceName.TrimEnd('\0');
                }
            }

            return(name);
        }
Example #2
0
        private bool ShowElementTip(string szTitle, string szText)
        {
            if (this.m_toolTip == null)
            {
                this.m_toolTip             = new ToolTip();
                this.m_toolTip.IsBalloon   = true;
                this.m_toolTip.ToolTipIcon = ToolTipIcon.Warning;
            }
            this.m_toolTip.RemoveAll();
            Control editingControl = this.dataGridView1.EditingControl;

            if (editingControl == null || editingControl.IsDisposed)
            {
                return(false);
            }

            NativeStructs.POINT nPoint = new NativeStructs.POINT();
            if (!NativeMethods.User32.GetCaretPos(ref nPoint))
            {
                return(false);
            }

            Graphics graphics = editingControl.CreateGraphics();
            SizeF    size     = graphics.MeasureString(szText, editingControl.Font);

            graphics.Dispose();

            Point point = new Point(nPoint.x - 20, nPoint.y - 60 - (int)size.Height);

            this.m_toolTip.ToolTipTitle = szTitle;
            this.m_toolTip.Show(szText, editingControl, point, 3000);
            return(true);
        }
            /// <summary>
            /// Sets the bitmap of this layered window.
            /// </summary>
            /// <param name="bitmap">The bitmap.</param>
            public void SetBitmap(Bitmap bitmap)
            {
                //Test if the bitmap is compatible
                if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
                {
                    throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
                }

                //Get the device contexts
                IntPtr screenDc   = NativeMethods.GetDC(IntPtr.Zero);
                IntPtr memDc      = NativeMethods.CreateCompatibleDC(screenDc);
                IntPtr hBitmap    = IntPtr.Zero;
                IntPtr hOldBitmap = IntPtr.Zero;

                try
                {
                    //Get handle to the new bitmap and select it into the current device context
                    hBitmap    = bitmap.GetHbitmap(Color.FromArgb(0));
                    hOldBitmap = NativeMethods.SelectObject(memDc, hBitmap);

                    //Set parameters for layered window update
                    NativeStructs.SIZE          newSize        = new NativeStructs.SIZE(bitmap.Width, bitmap.Height);
                    NativeStructs.POINT         sourceLocation = new NativeStructs.POINT(0, 0);
                    NativeStructs.POINT         newLocation    = new NativeStructs.POINT(this.Left, this.Top);
                    NativeStructs.BLENDFUNCTION blend          = new NativeStructs.BLENDFUNCTION();
                    blend.BlendOp             = NativeConstants.AC_SRC_OVER;
                    blend.BlendFlags          = 0;
                    blend.SourceConstantAlpha = 255;
                    blend.AlphaFormat         = NativeConstants.AC_SRC_ALPHA;

                    //Update the window
                    NativeMethods.UpdateLayeredWindow(this.Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, NativeConstants.ULW_ALPHA);
                }
                finally
                {
                    //Release the device context
                    NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                    if (hBitmap != IntPtr.Zero)
                    {
                        NativeMethods.SelectObject(memDc, hOldBitmap);
                        NativeMethods.DeleteObject(hBitmap);
                    }

                    //Delete the device context
                    NativeMethods.DeleteDC(memDc);
                }
            }
Example #4
0
        public void SetBitmap(Bitmap bitmap, byte opacity)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new FormatException("Format32bppArgb bitmap required");
            }

            IntPtr screenDc  = NativeMethods.GetDC(IntPtr.Zero);
            IntPtr memDc     = NativeMethods.CreateCompatibleDC(screenDc);
            IntPtr hBitmap   = IntPtr.Zero;
            IntPtr oldBitmap = IntPtr.Zero;

            try
            {
                hBitmap   = bitmap.GetHbitmap(Color.FromArgb(0));
                oldBitmap = NativeMethods.SelectObject(memDc, hBitmap);

                NativeStructs.SIZE          size        = new NativeStructs.SIZE(bitmap.Width, bitmap.Height);
                NativeStructs.POINT         pointSource = new NativeStructs.POINT(0, 0);
                NativeStructs.POINT         topPos      = new NativeStructs.POINT(Left, Top);
                NativeStructs.BLENDFUNCTION blend       = new NativeStructs.BLENDFUNCTION();
                blend.BlendOp             = NativeConsts.AC_SRC_OVER;
                blend.BlendFlags          = 0;
                blend.SourceConstantAlpha = opacity;
                blend.AlphaFormat         = NativeConsts.AC_SRC_ALPHA;

                NativeMethods.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, NativeConsts.ULW_ALPHA);
            }
            finally
            {
                NativeMethods.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    NativeMethods.SelectObject(memDc, oldBitmap);
                    NativeMethods.DeleteObject(hBitmap);
                }
                NativeMethods.DeleteDC(memDc);
            }
        }
Example #5
0
        private void WindowFinder_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isSearching)
            {
                EndSearch();
            }

            // Grab the window from the screen location of the mouse.
            Point newPoint = PointToScreen(new Point(e.X, e.Y));

            NativeStructs.POINT windowPoint = NativeStructs.POINT.FromPoint(newPoint);
            IntPtr foundWindow = NativeMethods.WindowFromPoint(windowPoint);

            // Do we have a valid window handle?
            if (foundWindow != IntPtr.Zero)
            {
                // Give it another try, it might be a child window (disabled, hidden, etc.).
                // Offset the point to be a client point of the active window.
                if (NativeMethods.ScreenToClient(foundWindow, ref windowPoint))
                {
                    // Check if there is some hidden/disabled child window at this point.
                    IntPtr childWindow = NativeMethods.ChildWindowFromPoint(foundWindow, windowPoint);
                    if (childWindow != IntPtr.Zero)
                    {
                        // Great, we have the inner child.
                        foundWindow = childWindow;
                    }
                }
            }

            // Is this the same window as the last detected one?
            if (window.Handle != foundWindow)
            {
                if (window.SetWindowHandle(foundWindow))
                {
                    InvokeActiveWindowChanged();
                }
            }
        }
Example #6
0
 public extern static bool MoveToEx(
     IntPtr hdc,
     int X,
     int Y,
     out NativeStructs.POINT lpPoint);