Graphics.DrawLine没有在MDI容器客户区上绘制任何内容。

时间:2014-06-30 12:18:35

标签: c# winforms mdi

我正在尝试增强名为SnapFormExtender的this CodeProject project。该项目扩展了MDIParent表单,并将其子表单“捕捉”到彼此及其容器边缘。

我遇到的问题是它在MDI容器内移动表单时会过于频繁地“捕捉”子表单,导致表单在用户将其移动到另一个表单或容器边缘附近时抖动。

为了解决这个问题,我想绘制一个可能的新位置的轮廓,而不是重绘表单,当用户完成移动/调整表单大小时,我将实际更改表单位置。

我目前的问题是,我似乎无法在Graphics.DrawLine的MDIParent表单上画一条线。下面是我用来尝试在MDIParent上绘制线条的方法。 (为了尽可能简洁,我已将整个代码发布到http://pastebin.com/uBREMh5g以供进一步审核)

在下面的代码中,将parentForm设置在SnapFormExtender中,并将其添加到表单中。这个类用于捕捉其子表单的MDIParent。 Form form是子表单,如果用户要立即停止移动表单,则Point possibleLocation是表单的新位置(捕捉位置)。

private void DrawPossiblePosition(Form form, Point possibleLocation)
{
    float[] dashValues = { 2, 2 };
    Console.Out.WriteLine("Drawing possible position");
    using (Pen blackPen = new Pen(Color.Yellow, 5))
    {
        blackPen.DashPattern = dashValues;
        Console.Out.WriteLine("Got a black dashed pen");
        using (Graphics parentGfx = parentForm.CreateGraphics())
        {
            int xLoc = possibleLocation.X;
            int yLoc = possibleLocation.Y;
            Console.Out.WriteLine("Got gfx object, drawing lines");
            parentGfx.DrawLine(blackPen, new Point(xLoc, yLoc), new Point(xLoc+form.Width, yLoc));
            parentGfx.DrawLine(blackPen, new Point(xLoc+form.Width, yLoc), new Point(xLoc+form.Width, yLoc+form.Height));
            parentGfx.DrawLine(blackPen, new Point(xLoc + form.Width, yLoc + form.Height), new Point(xLoc, yLoc + form.Height)); 
            parentGfx.DrawLine(blackPen, new Point(xLoc, yLoc + form.Height), new Point(xLoc, yLoc));
        }
    }
}

此代码执行时没有错误,并显示所有Console.Out.WriteLines。只要在MDIParent中移动子表单,就会调用它。

我能想到的是,因为调用所有的print语句是MDIParent忽略了我的DrawLines,或者它经常重新绘制,因为表单被移动了所有DrawLines OnPaint一旦子表单移动单个像素,就会删除1}}。但我不认为是这种情况,因为我从来没有看到屏幕上的线条闪烁。

我需要一个特殊的时间/方法来绘制这些线吗?

或者

如果设置了标志,我是否应该覆盖MD​​IParents {{1}}方法以选择性地绘制指定位置/维度的行,并从SnapFormExtender类设置标志和维度?

0 个答案:

没有答案