实际上就是分2步 1.如何判断当前点击的是哪个窗体(获得焦点的窗体或控件) 2.失去焦点的窗体自动隐藏边框,获得焦点的窗体显示个边框知道是这个步骤,但具体通过什么方法去实现呢? 我现在获得当前出口的句柄,然后用下面的方法去实现,但报异常 POINT p = new POINT(); if (GetCursorPos(out p)) { // Point IntPtr win = WindowFromPoint(out p); System.Drawing.Rectangle rt = new System.Drawing.Rectangle(); if (GetWindowRect(win, out rt)) { Graphics gp = Graphics.FromHdc(win); gp.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 4), rt.Left, rt.Top, rt.Width, rt.Height); } } 如图所示
按照你的方法,我做了一个小demo,但是在用WindowFromPoint 获得句柄的时候报异常 Window1 w1 = new Window1(); private void Window_MouseMove(object sender, MouseEventArgs e) { System.Windows.Point p = Mouse.GetPosition(e.Source as FrameworkElement); IntPtr win = WindowFromPoint(p); System.Drawing.Rectangle rt = new System.Drawing.Rectangle(); if (GetWindowRect(win, out rt)) { w1.Left = rt.X; w1.Top = rt.Y; w1.Width = rt.Width; w1.Height = rt.Height; } }
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { this.X = x; this.Y = y; } }[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct RECT { public RECT(System.Drawing.Rectangle rectangle) { Left = rectangle.Left; Top = rectangle.Top; Right = rectangle.Right; Bottom = rectangle.Bottom; } public RECT(System.Drawing.Point location, System.Drawing.Size size) { Left = location.X; Top = location.Y; Right = location.X + size.Width; Bottom = location.Y + size.Height; } public System.Int32 Left; public System.Int32 Top; public System.Int32 Right; public System.Int32 Bottom; }[DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool GetCursorPos(out POINT pt);[DllImport("user32.dll", EntryPoint = "WindowFromPoint")] public static extern IntPtr WindowFromPoint(POINT point);[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hWnd);[System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool GetWindowRect(System.IntPtr hWnd, out RECT lpRect);/// <summary> /// 获取主窗口句柄 /// </summary> /// <param name="hWnd"></param> /// <returns></returns> IntPtr getMainWindow(IntPtr hWnd) { IntPtr ptr = GetParent(hWnd); if (ptr == IntPtr.Zero) return hWnd; else return getMainWindow(ptr); }private void timer1_Tick(object sender, EventArgs e) { POINT p; GetCursorPos(out p); IntPtr ptr = WindowFromPoint(p); ptr = getMainWindow(ptr); RECT rect; if (ptr != IntPtr.Zero) { GetWindowRect(ptr, out rect); } }
Border 的几个生要的属性:
Background:此属性设置 背景颜色
BorderBrush:此属性设置 边框颜色
BorderThickness:此属性设置 边框的宽度
CornerRadius:此属性设置 每一个角圆的弧度,
<Window
Background="Transparent" AllowsTransparency="True" WindowStyle="None">
<Border BorderBrush="Red" BorderThickness="1"></Border>
</Window>
用timer刷新去查询当前鼠标处的窗口,用api获取此窗口的大小和在屏幕的位置,把上面的窗口大小设置成鼠标处的窗口一样大小(或者大几像素),同一个位置,基本就实现你要的功能了.
参考以下api
WindowFromPoint
GetWindowRect
实际上就是分2步
1.如何判断当前点击的是哪个窗体(获得焦点的窗体或控件)
2.失去焦点的窗体自动隐藏边框,获得焦点的窗体显示个边框知道是这个步骤,但具体通过什么方法去实现呢?
我现在获得当前出口的句柄,然后用下面的方法去实现,但报异常
POINT p = new POINT();
if (GetCursorPos(out p))
{
// Point
IntPtr win = WindowFromPoint(out p);
System.Drawing.Rectangle rt = new System.Drawing.Rectangle();
if (GetWindowRect(win, out rt))
{
Graphics gp = Graphics.FromHdc(win);
gp.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 4), rt.Left, rt.Top, rt.Width, rt.Height);
}
}
如图所示
按照你的方法,我做了一个小demo,但是在用WindowFromPoint 获得句柄的时候报异常
Window1 w1 = new Window1(); private void Window_MouseMove(object sender, MouseEventArgs e)
{
System.Windows.Point p = Mouse.GetPosition(e.Source as FrameworkElement);
IntPtr win = WindowFromPoint(p);
System.Drawing.Rectangle rt = new System.Drawing.Rectangle();
if (GetWindowRect(win, out rt))
{
w1.Left = rt.X;
w1.Top = rt.Y;
w1.Width = rt.Width;
w1.Height = rt.Height; }
}
public struct POINT
{
public int X;
public int Y; public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct RECT
{
public RECT(System.Drawing.Rectangle rectangle)
{
Left = rectangle.Left;
Top = rectangle.Top;
Right = rectangle.Right;
Bottom = rectangle.Bottom;
}
public RECT(System.Drawing.Point location, System.Drawing.Size size)
{
Left = location.X;
Top = location.Y;
Right = location.X + size.Width;
Bottom = location.Y + size.Height;
}
public System.Int32 Left;
public System.Int32 Top;
public System.Int32 Right;
public System.Int32 Bottom;
}[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetCursorPos(out POINT pt);[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]
public static extern IntPtr WindowFromPoint(POINT point);[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool GetWindowRect(System.IntPtr hWnd, out RECT lpRect);/// <summary>
/// 获取主窗口句柄
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
IntPtr getMainWindow(IntPtr hWnd)
{
IntPtr ptr = GetParent(hWnd);
if (ptr == IntPtr.Zero)
return hWnd;
else
return getMainWindow(ptr);
}private void timer1_Tick(object sender, EventArgs e)
{
POINT p;
GetCursorPos(out p);
IntPtr ptr = WindowFromPoint(p);
ptr = getMainWindow(ptr);
RECT rect;
if (ptr != IntPtr.Zero)
{
GetWindowRect(ptr, out rect);
}
}
http://pan.baidu.com/s/1hqtHTEc
http://pan.baidu.com/s/1dDmw9ZN