Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

picturebox - Z-Index of Drawing Rectangle C#

I have an application which uses the mouse to free-draw a rectangle on a picbox image. However the rectangle only shows up behind the picbox, rather than on top of it. Is there a property i can set which can fix this? (show rect on top of picbox image rather than behind it). Here is the code:

   System.Drawing.Graphics picboxGraphics;
    bool mDown = false;
    int mouseX;
    int mouseY;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        mDown = true;
        mouseX = e.X;
        mouseY = e.Y;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mDown == true)
        {
            this.Refresh();
            Pen drawPen = new Pen(Color.Red, 5);
            int width = e.X - mouseX, height = e.Y - mouseY;
            Rectangle rect = new Rectangle(mouseX, mouseY, width * Math.Sign(width), height * Math.Sign(height));
            picboxGraphics = this.CreateGraphics();
            picboxGraphics.DrawRectangle(drawPen, rect);
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        mDown = false;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You creating graphics from form which is behind the picbox you can create graphics from picbox's image and draw someting. But if you want layer system you can draw your thins on an transparent image and combine them. with this vay you can make an undo or delete layer system.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...