views:

35

answers:

1

I am creating a Kiosk application. I have it full screen and I am drawing directly on the form:

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

Everything draws fine, but the second I press Alt or Tab (individually or together, in any order) the rectangle I drew disappears. If I call the function again, it redraws and stays no matter what button I press. Is there some sort of .NET function that I need to call so my drawing function only needs to be called once?

Thanks.

+2  A: 

You should be doing your painting by overriding OnPaint - then you will be able to re-paint as necessary. At the moment, anything you draw is only valid under the screen is invalidated.

Marc Gravell
OK, I moved the method into the override. How should I pass the headerText variable? Global variable?
Anders
I am still getting the same behavior, I think I am going to just go with my original plan and create Panels instead of drawing Rectangles. I'll end up having more control over the final implementation this way anyway. Thanks for your help though.
Anders
Sorry, I was elsewhere - yes, it should work with a text variable. Very surprised it didn't work; it should!
Marc Gravell