Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

vb.net - Program first draws on invisible picturebox then picturebox becomes visible DESPITE Visibility supposed to c ome first then dynamic drawing

A snippet of my code:

    Dim G As Graphics
    Dim BBG As Graphics
    Dim BB As Bitmap

    Dim R As Rectangle

..................................................................

            picMainScreen.Visible = True
            G = picMainScreen.CreateGraphics
            BB = New Bitmap(picMainScreen.Width, picMainScreen.Height)
            For x = 0 To 256 * 3 - 1 Step 24
                For y = 0 To 240 * 3 - 1 Step 24
                    R = New Rectangle(New Point(x, y), New Point(24, 24))

                    G.DrawRectangle(Pens.Black, R)
                Next
            Next

In this snippet of code, picMainScreen was a PictureBox that was originally not supposed to be visible. Then through some conditions, picMainScreen was SUPPOSED to TURN Visible. And THEN, the code draws all the rectangles onto the picture.

However, that isn't the case: the rectangles are first drawn onto the picture, and THEN the picture becomes visible.

Why does this happen? And what's the remedy?

question from:https://stackoverflow.com/questions/65878064/program-first-draws-on-invisible-picturebox-then-picturebox-becomes-visible-desp

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

1 Answer

0 votes
by (71.8m points)

Your rectangle instantiation is interesting since you are using two parameters of type Point. R = New Rectangle(New Point(x, y), New Point(24, 24))

The Microsoft Docs show these parameter types; Rectangle(Point, Size) Initializes a new instance of the Rectangle class with the specified location and size.

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.rectangle.-ctor?view=netframework-4.7.2

I'm not sure if the parameter types are the problem; however you are mixing drawing graphics with a picture control and they might not cohabitate in the same space well.

You could try calling DoEvents() after making the picture control visible to force it to be displayed first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...