C# 区域填充

        protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WinUser.WM_CREATE:
break;
case WinUser.WM_PAINT:
break;
case WinUser.WM_LBUTTONDOWN:
POINT point;
point.x = WinUser.LOWORD((int)m.LParam);
point.y = WinUser.HIWORD((int)m.LParam);
ElliRgns(this.Handle, point);
break;
}
}

/// <summary>
/// 创建椭圆区域, 并进行填充
/// </summary>
/// <param name="hwnd"></param>
/// <param name="point"></param>
unsafe void ElliRgns(IntPtr hwnd, POINT point)
{
RECT rect, rectClient;
IntPtr hrgn;
IntPtr hBrush, hBrushOld;

//DC
IntPtr hdc = Win32Native.GetDC(hwnd);
Win32Native.GetClientRect(hwnd, &rectClient);

//点周围一块区域
rect.left = point.x - 40;
rect.right = point.x + 40;
rect.top = point.y - 30;
rect.bottom = point.y + 30;

//创建区域
hrgn = Win32Native.CreateEllipticRgnIndirect(&rect);

//创建画刷
hBrush = Win32Native.CreateSolidBrush(WinUser.RGB(0, 255, 0));
hBrushOld = Win32Native.SelectObject(hdc, hBrush);

//为 DC 选择画刷
Win32Native.SelectObject(hdc, hBrush);

//绘制区域
Win32Native.PaintRgn(hdc, hrgn);

//稀放资源
hBrush = Win32Native.SelectObject(hdc, hBrushOld);
Win32Native.DeleteObject(hBrush);
Win32Native.DeleteObject(hrgn);
Win32Native.DeleteDC(hdc);
}

C# 区域填充_第1张图片

你可能感兴趣的:(C#)