簡體   English   中英

如何在C#中的GraphicsPath對象上縮放矩形或形狀時,如何修復圖片框刷新時鼠標滾輪上下延遲?

[英]How to fix the delay between mouse wheel up and down on the picture box refresh while scaling a rectangle or a shape on the GraphicsPath object in C#?

1.當用戶在圖片框上向上鼠標按下鼠標時,我正在嘗試縮放矩形。
2.完成5個鼠標滾輪后,如果你將鼠標滾輪向下移動,矩形仍然保持向上擴展(擴展)。
3.任何解決方案嗎?

GraphicsPath path=new GraphicsPath();
private float scale=1.0F;
private bool ActiveWheel=false;
public Form1()
{
    path.AddRectangle(new Rectangle(10,10,50,100));
}
private void PictureBox1_Paint(object sender,PaintEventArgs e)
{
    if(ActiveWheel)
    {
        ActiveWheel=false;
        ScaleRectangle(e);

    }
else
{
   e.Graphics.DrawPath(Pens.Red,path);
}

}
private void PictureBox1_MouseWheel(object sender,MouseEventArgs e)
{
    ActiveWheel=true;
    scale=Math.Max(scale+Math.Sign(e.Delta)*0.1F,0.1F);
    pictureBox1.Refresh();
}
}
private void ScaleRectangle(PaintEventArgs e)
{
    var matrix=new Matrix();
    matrix.Scale(scale,scale,MatrixOrder.Append);
    path.Transform(matrix);
    e.Graphics.DrawPath(Pens.Blue,path);
}

任何解決方案或想法如何在鼠標滾輪和鼠標滾輪之間沒有延遲的情況下突然縮小或放大形狀(參見2.如果想要實際看到o / p)。

只需在MouseWheel()事件中調整縮放值,然后在Paint()事件中調整ScaleTransform()GRAPHICS曲面(而不是Path本身)並繪制:

public partial class Form1 : Form
{

    private GraphicsPath path = new GraphicsPath();
    private float scale = 1.0F;

    public Form1()
    {
        InitializeComponent();
        path.AddRectangle(new Rectangle(10, 10, 50, 100));
        pictureBox1.MouseWheel += PictureBox1_MouseWheel;
        pictureBox1.Paint += pictureBox1_Paint;
    }

    private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        scale = Math.Max(scale + Math.Sign(e.Delta) * 0.1F, 0.1F);
        pictureBox1.Invalidate();
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.DrawPath(Pens.Red, path);
    }

}

---編輯---

它完全縮放,你能告訴我如何只縮放圖形路徑對象和頂部,左邊必須固定,意味着沒有縮放頂部,左邊的點?

在這種情況下,轉換為矩形的左上角,縮放,然后轉換回原點。 現在繪制您的UNCHANGED矩形:

    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.TranslateTransform(10, 10);
        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.TranslateTransform(-10, -10);
        e.Graphics.DrawPath(Pens.Red, path);
    }

如果你有其他元素在不同的位置和/或比例繪制,那么你可以在之前和之后重置圖形表面(每個“元素”可以做相同類型的事情來定位自己並縮放自己):

    private void PictureBox1_Paint(object sender, PaintEventArgs e)
    {

        // possibly other drawing operations

        e.Graphics.ResetTransform();
        e.Graphics.TranslateTransform(10, 10);
        e.Graphics.ScaleTransform(scale, scale);
        e.Graphics.TranslateTransform(-10, -10);
        e.Graphics.DrawPath(Pens.Red, path);
        e.Graphics.ResetTransform();

        // possibly other drawing operations

    }

這種方法很好,因為它保留了關於矩形的原始信息; 變化只是視覺上的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM