隐藏在面板下的图形 [英] Graphics Hidden Under A Panel

查看:73
本文介绍了隐藏在面板下的图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是我先前有关面板下位图图像的上一个问题的后续问题
更改为m_Graphics = Graphics.FromImage(m_Bitmap)并使用Paint事件的建议似乎可行.我在图片框上方有20个小面板,用于隐藏随机生成的图形.但是,只有在其中存在覆盖面板的情况下,图形才会绘制在picCanvas图片框上.没有面板,没有图形显示.而且,如果将一个小面板放在picCanvas上的任何位置,则图形只会在面板下方绘制.当我使用断点调试程序并检查m_Bitmap.Width和m_Bitmap.Heght时,它们等于picCanvas图片框的大小.现在,我可以根据自己的需要进行这项工作,但是我不理解它?

有什么想法吗?

这是修改后的代码:

OK, new question as a follow up problem from my previous question about Bit Map Images Under A Panel
The advice to change to m_Graphics = Graphics.FromImage(m_Bitmap) and to use the Paint event seems to work. I have 20 small panels over the picture box to hide the randomly generated graphics. But the graphics will only draw on the picCanvas picture box if the overlaying panels are there. No panels, no graphics show up. And if you put one small panel anywhere on the picCanvas, the graphics only draw under the panel. when I debug the program with a breakpoint and check m_Bitmap.Width and m_Bitmap.Heght they are equal to the size of the picCanvas picture box. Now, I can make this work for my needs but I don''t understand it???

Any thoughts???

Here is the revised code:

Public Class Form1
    Dim PenColor As New Pen(Color.Red, 2)
    Dim X As Long
    Dim Y As Long
    Dim W As Long
    Dim H As Long
    Dim NewFlag As Boolean
    ' The Bitmap and Graphics objects we will draw on.
    Private m_Bitmap As Bitmap
    Private m_Graphics As Graphics
    ' Make the initial blank image.
    Private Sub Form1_Load() Handles MyBase.Load
        NewFlag = True
        Call MakeNewBitmap()
    End Sub

    ' Make a new blank image.
    Private Sub mnuFileClear_Click() Handles mnuFileClear.Click
        Call MakeNewBitmap()
        Panel1.Visible = True
        'There are actually 20 Panels but I removed the code to make it more compact for the question
      
    End Sub

    ' Make a new Bitmap to fit the canvas.
    Private Sub MakeNewBitmap()
        ' Get the drawing surface's size.
        Dim wid As Integer = picCanvas.ClientSize.Width
        Dim hgt As Integer = picCanvas.ClientSize.Height

        ' Make a Bitmap and Graphics to fit.
        m_Bitmap = New Bitmap(wid, hgt)
        m_Graphics = Graphics.FromImage(m_Bitmap)

        ' Clear the drawing area.
        m_Graphics.Clear(Me.BackColor)

        ' Display the result.
        picCanvas.Image = m_Bitmap
    End Sub

    Private Sub MakeGraphics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MakeGraphics.Click
        NewFlag = True
         Make some random elipses on screen when button is clicked
        m_Graphics = Graphics.FromImage(m_Bitmap)


        For Temp = 1 To 25
            Randomize()
            X = 5 + (Rnd() * (m_Bitmap.Width - 30))
            Y = 5 + (Rnd() * (m_Bitmap.Height - 30))
            'Minimum length,width = 20, max = 220
            H = 2 + (Rnd() * 20)
            W = 2 + (Rnd() * 20)
            m_Graphics.DrawEllipse(PenColor, X, Y, W, H)
        Next
        
    End Sub

    Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
        m_Graphics = Graphics.FromImage(m_Bitmap)


        
    End Sub
    Private Sub HidePanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HidePanel.Click
        Dim R As Integer
        Randomize()
        R = 1 + Rnd() * 20
        Label1.Text = R

        If R = 1 Then Panel1.Visible = False
       'There are actually 20 Panels but I removed the code to make it more compact for the question
      
    End Sub

    
End Class

推荐答案

您尚未完成建议的操作.不要在内存中保留图形对象.如果您在这些面板中绘制某些内容,请创建一个派生类,以在其paint事件中进行绘制.关于您的主要问题,您正在面板中绘画,所以我看不到它怎么可能出现在其他地方,或者我错过了什么?
You''ve not done what was advised. Don''t keep a graphics object in memory. If you draw something within these panels, create a derived class that does your drawing in it''s paint event. As for your main question, you''re drawing within the panel, so I don''t see how it could appear anywhere else, or did I miss something ?


这篇关于隐藏在面板下的图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆