删除ControlPaint.DrawReversibleLine之后的旧行

时间:2019-05-24 03:06:40

标签: c# image paint picturebox

我在删除ControlPaint.DrawReversibleLine和DrawReversibleFrame之后的旧行时遇到问题。我确实删除了,但是在Mouse_Move时,屏幕(用户控件和图片框)保留了旧行。如何完全删除旧行?

我已经在stackoverflow上搜索过此类内容,所有答案都很相似,但不适用于我的情况。

//a picturebox1 on usercontrol.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    //...other code
    ControlPaint.DrawReversibleLine(HorizontalSelectionPT1, HorizontalSelectionPT2, Color.Red); //erase old Horizontal line
    ControlPaint.DrawReversibleLine(VerticalSelectionPT1, VerticalSelectionPT2, Color.Red); //erase old vertical line
    Point point = PointToScreen(base.Location);
    HorizontalSelectionPT1 = new Point(point.X, Cursor.Position.Y);
    HorizontalSelectionPT2 = new Point(point.X + base.Width, Cursor.Position.Y);
    VerticalSelectionPT1 = new Point(Cursor.Position.X, point.Y);
    VerticalSelectionPT2 = new Point(Cursor.Position.X, point.Y + base.Height);
    ControlPaint.DrawReversibleLine(HorizontalSelectionPT1, HorizontalSelectionPT2, Color.Red); //Draw new line
    ControlPaint.DrawReversibleLine(VerticalSelectionPT1, VerticalSelectionPT2, Color.Red); //Draw new line

    //draw selected frame                    
   ControlPaint.DrawReversibleFrame(pbCanvas.RectangleToScreen(rectangle_0), Color.Black, FrameStyle.Dashed); //erase old frame
   rectangle_0.Width = e.X - rectangle_0.X;
   rectangle_0.Height = e.Y - rectangle_0.Y;                    
   ControlPaint.DrawReversibleFrame(pbCanvas.RectangleToScreen(rectangle_0), Color.Black, FrameStyle.Dashed); //Draw new frame

   //...other codes
}

很难捕获屏幕截图,我使用了Camera。 enter image description here

1 个答案:

答案 0 :(得分:-1)

我已用GDI32 API替换了所有ControlPaint.DrawReverseFrame,以解决擦除旧行的问题。无论我使用ControlPaint.DrawReversibleLine尝试使用哪种方法,都会产生不良效果或闪烁。

   private static int ArgbToRGB(int rgb)
   {
    return ((rgb >> 16 & 0x0000FF) | (rgb & 0x00FF00) | (rgb << 16 & 0xFF0000));
    }

    public enum PenStyles
    {
        PS_OLID = 0,
        PS_DASH = 1,
        PS_DOT = 2,
        PS_DASHDOT = 3,
        PS_DASHDOTDOT = 4,
        PS_NULL = 5,
        PS_INSIDEFRAME = 6,
        PS_USERSTYLE = 7,
        PS_ALTERNATE = 8,
        PS_STYLE_MASK = 0x0000000F,
        PS_ENDCAP_ROUND = 0x00000000,
        PS_ENDCAP_SQUARE = 0x00000100,
        PS_ENDCAP_FLAT = 0x00000200,
        PS_ENDCAP_MASK = 0x00000F00,
        PS_JOIN_ROUND = 0x00000000,
        PS_JOIN_BEVEL = 0x00001000,
        PS_JOIN_MITER = 0x00002000,
        PS_JOIN_MASK = 0x0000F000,
        PS_COSMETIC = 0x00000000,
        PS_GEOMETRIC = 0x00010000
        PS_TYPE_MASK = 0x000F0000
    }
    public enum GDIDrawingMode
    {
        R2_BLACK = 1,
        R2_NOTMERGEPEN = 2,
        R2_MASKNOTPEN = 3,
        R2_NOTCOPYPEN = 4,
        R2_MASKPENNOT = 5,
        R2_NOT = 6,
        R2_XORPEN = 7,
        R2_NOTMASKPEN = 8,
        R2_MASKPEN = 9,
        R2_NOTXORPEN = 10,
        R2_NOP = 11,
        R2_MERGENOTPEN = 12,
        R2_COPYPEN = 13,
        R2_MERGEPENNOT = 14,
        R2_MERGEPEN = 15,
        R2_WHITE = 16            
    }
[DllImport("gdi32.dll", CharSet = CharSet.Auto, EntryPoint = "Rectangle", ExactSpelling = true, SetLastError = true)]
public static extern bool GDIDrawRectangle(IntPtr hDC, int left, int top, int right, int bottom);

[DllImport("gdi32.dll")]
public static extern int SetROP2(IntPtr hDC, int fnDrawMode);

[DllImport("gdi32.dll")]
public static extern bool MoveToEx(IntPtr hDC, int x, int y, ref Point p);

[DllImport("gdi32.dll")]
public static extern bool LineTo(IntPtr hdc, int x, int y);

[DllImport("gdi32.dll")]
public static extern IntPtr CreatePen(int fnPenStyle, int nWidth, int crColor);

[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObj);

[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObj);

public static void DrawXORRectangle(Graphics graphics, Color penColor, Rectangle rectangle)
    {            
        IntPtr hDC = graphics.GetHdc();
        IntPtr hPen = CreatePen((int)PenStyles.PS_DOT, 1, ArgbToRGB(penColor.ToArgb()));
        IntPtr hOldPen = SelectObject(hDC, hPen);
        SetROP2(hDC, (int)GDIDrawingMode.R2_NOTXORPEN);
        GDIDrawRectangle(hDC, rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
        SelectObject(hDC, hOldPen);
        DeleteObject(hPen);
        graphics.ReleaseHdc(hDC);
    }

enter image description here