关于图像和winforms的问题

时间:2009-03-16 13:37:04

标签: vb.net visual-studio-2008 system.drawing

我正在创建一个Kiosk应用程序。我有全屏幕,我直接在表格上绘图:

Private Function CreateHeader(ByVal headerText As String) As Boolean
    Try
        Dim oGFX As Graphics = Graphics.FromHwnd(hwnd:=_oP.Handle)
        Dim oRect As Rectangle = New Rectangle(x:=0, y:=0, Width:=_screenSize.X, Height:=150)
        Dim oBMP As New Bitmap(GetEmbeddedResourceStream("Kiosk.logo.jpg"))

        Dim oBrush As Brush = New SolidBrush(Color.FromArgb(red:=0, green:=0, blue:=153))


        oGFX.FillRectangle(brush:=oBrush, rect:=oRect)
        oGFX.DrawImage(image:=oBMP, _
                           x:=0, _ 
                           y:=0, _
                       width:=oBMP.Width, _ 
                      height:=oBMP.Height)

        If Not String.IsNullOrEmpty(headerText) Then
            Dim oFont As New Font("Impact", 48, FontStyle.Regular)
            Dim g As Graphics = Me.CreateGraphics()

            Dim oStringSize As SizeF = g.MeasureString(headerText, oFont)

            g = Nothing

            oGFX.DrawString(s:=headerText, _
                         font:=oFont, _
                        brush:=Brushes.White, _
                            x:=(oRect.Width - Math.Round(oStringSize.Width, 0)) / 2, _
                            y:=(oRect.Height - Math.Round(oStringSize.Height, 0)) / 2)
        End If

        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

一切都很好,但第二个我按Alt或Tab(单独或一起,以任何顺序)我画的矩形消失。如果我再次调用该函数,无论我按什么按钮,它都会重绘并保持不变。是否需要调用某种.NET函数,因此我的绘图函数只需要调用一次?

感谢。

1 个答案:

答案 0 :(得分:2)

你应该通过覆盖OnPaint进行绘画 - 然后你就可以根据需要重新绘画。目前,您绘制的任何内容仅在屏幕无效时才有效。