2016-12-07 51 views
-1

我有一個小問題。 我怎麼能畫這種形象在任何controls的頂部像textbox在多個控件的頂部執行繪畫

這是我的代碼:

Private Sub GroupBox6_Paint(sender As Object, e As PaintEventArgs) Handles GroupBox6.Paint 
    If txtStatus.Text = "Cancelled" Then 
     Try 
      Dim newImage As Image = Image.FromFile(FOLDER_PATH & "\completed.png") 
      Dim x As Single = ((GroupBox6.Width/2) - (463/4)) 
      Dim y As Single = 10 
      Dim width As Single = 463/2 
      Dim height As Single = 242/2 
      e.Graphics.DrawImage(newImage, x, y, width, height) 
     Catch ex As Exception 
     End Try 
    End If 
End Sub 

這是我的輸出:

enter image description here

所以我的目標是在我的groupbox裏面畫Completedtextbox, label之上的任何想法?

+0

你要顯示的圖像的疊加,當用戶完成一個過程,隱藏背後的控制?我認爲當狀態爲「已取消」時,使用顯示的PictureBox控件而不是Groupbox會更容易,而不是在groupbox上繪製圖像。 – Markus

+0

@Markus如果我使用圖片框,它不透明,這就是爲什麼我在控件上繪製圖像,以保持它。但在我的情況下,txtbox和標籤覆蓋圖像。我只是想繪製它,也許把它發送到羣組框中的gropbox和其他控件的前面 – Muj

+1

有兩種解決方案,我可以想到。 1.創建一個分層窗口並將其顯示在控件的頂部2.以圖片將出現的部分的截圖並將其繪製在picbox上,然後繪製png圖像 –

回答

0

你需要兩個bitmaps和一個picturebox這個工作。第一個是在png圖像和所述第二個中的picturebox圖像:

Private pngImage, picBoxImage As Image 

在窗體加載事件初始化兩個圖像:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    pngImage = Image.FromFile(FOLDER_PATH & "\completed.png") //load it once 
    picBoxImage = CType(pngImage.Clone, Image) 

    PictureBox1.Size = New Size(CInt(463/2), CInt(242/2)) 
    PictureBox1.Parent = GroupBox6 
    PictureBox1.Image = picBoxImage 
    PictureBox1.Visible = False //you dont want it at the beggining 
End Sub 

來顯示該圖片框:

Private Sub ShowCompletedMessage() 
    Dim screenLocation As Point 
    Dim gr As Graphics 
    //you can pass these values as parameters in the sub if you want to make the code more generic 
    Dim x As Integer = CInt(((GroupBox6.Width/2) - (463/4))) 
    Dim y As Integer = 10 
    Dim width As Integer = CInt(463/2) 
    Dim height As Integer = CInt(242/2) 

    //Ensure that picturebox is not visible. If it is you don't need to take a screenshot 
    If PictureBox1.Visible = True Then 
     Return 
    End If 

    gr = Graphics.FromImage(picBoxImage) 

    //you need to transform the coordinates to screen ones 
    screenLocation = GroupBox6.PointToScreen(New Point(x, y)) 

    //draw the portion of the screen to your bitmap 
    gr.CopyFromScreen(screenLocation.X, screenLocation.Y, 0, 0, New Size(width, height), CopyPixelOperation.SourceCopy) 

    //draw the png image on top 
    gr.DrawImage(pngImage, 0, 0, width, height) 

    PictureBox1.Location = New Point(x, y) 
    PictureBox1.BringToFront() 
    PictureBox1.Visible = True 

    gr.Dispose() 
    gr = Nothing 


    Return 

End Sub 

每次你想顯示消息調用上面的子。你決定從何時何地。你需要隱藏picturebox,如果你不需要它了

PictureBox1.Visible = False 
+0

任何原因都可用於投票。 –

+0

我應該在哪裏把這種代碼放在我的表單中?在groupbox繪畫事件裏面?所以我可以嘗試一下,看看結果 – Muj

+0

@Muj做*寬*,*高*和*位置*的PNG圖像變化?當然位置將相對於* GroupBox6 *。 –