C#如何实现选中控件出现虚线框

littlyfx 2009-04-07 12:37:49
如题,我想要的效果是选中窗体中的某个控件(比如按钮)就在其周围产生虚线框
...全文
923 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
nvip 2011-11-25
  • 打赏
  • 举报
回复
[UIPermission(SecurityAction.LinkDemand, Window=UIPermissionWindow.AllWindows)]
public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style)
{
int num;
Color white;
IntPtr ptr2;
if (backColor.GetBrightness() < 0.5)
{
num = 10;
white = Color.White;
}
else
{
num = 7;
white = Color.Black;
}
IntPtr handle = System.Windows.Forms.UnsafeNativeMethods.GetDCEx(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetDesktopWindow()), System.Windows.Forms.NativeMethods.NullHandleRef, 0x403);
switch (style)
{
case FrameStyle.Dashed:
ptr2 = System.Windows.Forms.SafeNativeMethods.CreatePen(2, 1, ColorTranslator.ToWin32(backColor));
break;

default:
ptr2 = System.Windows.Forms.SafeNativeMethods.CreatePen(0, 2, ColorTranslator.ToWin32(backColor));
break;
}
int nDrawMode = System.Windows.Forms.SafeNativeMethods.SetROP2(new HandleRef(null, handle), num);
IntPtr ptr3 = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, handle), new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetStockObject(5)));
IntPtr ptr4 = System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, handle), new HandleRef(null, ptr2));
System.Windows.Forms.SafeNativeMethods.SetBkColor(new HandleRef(null, handle), ColorTranslator.ToWin32(white));
System.Windows.Forms.SafeNativeMethods.Rectangle(new HandleRef(null, handle), rectangle.X, rectangle.Y, rectangle.Right, rectangle.Bottom);
System.Windows.Forms.SafeNativeMethods.SetROP2(new HandleRef(null, handle), nDrawMode);
System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, handle), new HandleRef(null, ptr3));
System.Windows.Forms.SafeNativeMethods.SelectObject(new HandleRef(null, handle), new HandleRef(null, ptr4));
if (ptr2 != IntPtr.Zero)
{
System.Windows.Forms.SafeNativeMethods.DeleteObject(new HandleRef(null, ptr2));
}
System.Windows.Forms.UnsafeNativeMethods.ReleaseDC(System.Windows.Forms.NativeMethods.NullHandleRef, new HandleRef(null, handle));
}
littlyfx 2009-04-07
  • 打赏
  • 举报
回复
可以实现,实在是太感谢拉~
littlyfx 2009-04-07
  • 打赏
  • 举报
回复
马上去尝试,不过先谢谢拉
drls1982 2009-04-07
  • 打赏
  • 举报
回复
ControlPaint.DrawReversibleFrame(new Rectangle(),Color.Red,FrameStyle.Dashed)出现边框,原处再画一次边框消失,根据拖动控件的位置,设置mousemove动态画边框就能实现了

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧