如题,我想要的效果是选中窗体中的某个控件(比如按钮)就在其周围产生虚线框

解决方案 »

  1.   

    ControlPaint.DrawReversibleFrame(new Rectangle(),Color.Red,FrameStyle.Dashed)出现边框,原处再画一次边框消失,根据拖动控件的位置,设置mousemove动态画边框就能实现了
      

  2.   

     [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));
            }