[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern bool RedrawWindow(IntPtr hwnd, COMRECT rcUpdate, IntPtr hrgnUpdate, int flags);
[DllImport("User32.dll")]
public extern static IntPtr GetDC(System.IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hWnd,out Rect lpRect);
//获取窗口边框矩形尺寸
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//获取指定句柄窗口矩形位置大小
Rect rect = new Rect();
GetWindowRect(winHandle, out rect);
//画矩形边框
IntPtr DesktopHandle = GetDC(IntPtr.Zero);
Graphics g = System.Drawing.Graphics.FromHdc(DesktopHandle);
Pen pen = new Pen(Color.Black, 2);
g.DrawRectangle(pen, rect.Left, rect.Top, winWidth, winHeight);
我是通过GetWindowRect获取查看的窗口位置及大小,然后在桌面上指定位置绘制矩形,
矩形是绘制到桌面上的,闪烁效果用的RedrawWindow,刷新桌面,屏幕会有点闪,效果不太好。
如果将矩形绘制到指定窗口上,然后用 RedrawWindow刷新指定窗口是不是效果要好点?
或者有其他的方法?
------解决方案--------------------------------------------------------
[DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, //目标设备的句柄
int nXDest, // 目标对象的左上角的X坐标
int nYDest, // 目标对象的左上角的X坐标
int nWidth, // 目标对象的矩形的宽度
int nHeight, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXSrc, // 源对象的左上角的X坐标
int nYSrc, // 源对象的左上角的X坐标