Label painting issue when form moved offscreen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robertybob
    New Member
    • Feb 2013
    • 116

    Label painting issue when form moved offscreen

    Hi

    I have a label which has text that changes dependent on user input using the paint command. Say it says 'Hello World'.

    When I drag the form around such that any part of the label goes offscreen, when the label returns to the screen it wants to constantly repaint itself as it returns meaning that, dependent on how fast you drag it back, the label can read 'He He He He Hello World' or 'HHHHHHello World' etc

    Is this a known issue and is there a way to prevent it?

    The label is simply painted using

    Code:
    Private Sub MyLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyLabel.Paint
    In order to get it to change appearance it is invalidated when the user clicks a button to tell it to change to some new value. I'm assuming though that there's some core VB process within the painteventargs that triggers when the item gets back to the visible screen that is the root cause of this as all other labels return to the screen just fine.

    Thanks!
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It would help to see the code.

    Comment

    • robertybob
      New Member
      • Feb 2013
      • 116

      #3
      Here is some code. It's pretty basic.

      Code:
      Option Explicit On
      Imports System.ComponentModel
      Imports System.Runtime.InteropServices
      Imports System.Text
      Imports System.Windows.Forms
      Imports System
      Imports System.IO
      Imports System.Collections
      Imports System.Threading
      
      Public Class Form1
      
      Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
              MyLabel.Invalidate()
      End Sub
      
      Private Sub MyLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyLabel.Paint
              Dim upperfont As Font = New Font("Arial", 10, FontStyle.Bold)
              Dim lowerfont As Font = New Font("Arial", 8, FontStyle.Regular)
              Dim upperbrush As SolidBrush = New SolidBrush(Color.Black)
              Dim lowerbrush As SolidBrush = New SolidBrush(Color.FromArgb(17, 17, 17))
              Dim upperstr As String = myName.Text
              Dim lowerstr As String = myOccupation.Text
              Dim uppersize As SizeF = e.Graphics.MeasureString(upperstr, upperfont, e.ClipRectangle.Size)
              Dim lowersize As SizeF = e.Graphics.MeasureString(lowerstr, lowerfont, e.ClipRectangle.Size)
              e.Graphics.DrawString(upperstr, upperfont, upperbrush, e.ClipRectangle.X + 3 + ((e.ClipRectangle.Width - uppersize.Width) / 2), e.ClipRectangle.Location.Y + 19)
              e.Graphics.DrawString(lowerstr, lowerfont, lowerbrush, e.ClipRectangle.X + ((e.ClipRectangle.Width - lowersize.Width) / 2), e.ClipRectangle.Location.Y + 34)
      End Sub
      
      End Class

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Did you try clearing it before redrawing?

        Comment

        • robertybob
          New Member
          • Feb 2013
          • 116

          #5
          Hi Rabbit, I'm learning VB on the job so not sure what you mean by clearing before redrawing. I thought the Invalidate() triggered some kind of clear/redraw? It seems to be redrawing every time the form is moved and fits the label painting to whatever part of the label is showing at the time.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            But you're only invalidating once. If it's redrawing, you should try invalidating each time. Probably in the dragdrop event.

            Comment

            • robertybob
              New Member
              • Feb 2013
              • 116

              #7
              Ah ok. Let me investigate the dragdrop - been away for a few days. Thanks Rabbit.

              Comment

              • robertybob
                New Member
                • Feb 2013
                • 116

                #8
                Not sure Dragdrop is the solution - am looking at trying to determine if the form is being moved but apparently there's no way to do that.

                The issue is that this is a static label - it cant be moved within the form. It displays perfectly when you move the form all over the screen. It fails when part of the label goes off the screen edge as I assume that when the form realises that a part that was not visible is now visible again it 'paints' the label. It's that event that I need to kill.

                Still checking...

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  You can either subclass the form and look for the WM_MOVE message or use a timer to see if the form has changed location.

                  Comment

                  • robertybob
                    New Member
                    • Feb 2013
                    • 116

                    #10
                    Thanks Rabbit. Might take a look at this later.

                    At the moment I've added a check named offScreen. When the form Move event is being triggered it checks to see if any part of the form is likely to be outside the screen and, if so, offScreen blocks the painting. It repaints when the form is fully on the screen again.

                    It's ugly and when label is moved off left side of screen the label is missing text til completely back but this is such a minor part of the application it will do. Was hoping for a simple 'don't repaint when label moving back onscreen' function but not available :)

                    This isn't going to help many people as it's quite bespoke to my needs but this is the gist of the code I'm running with at the moment.

                    Code:
                    Private offScreen = 0
                    Private screenWidth As Integer = 99999
                    Private screenHeight As Integer = 99999
                    
                    Private Sub Me_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                    Dim myScreens() As Screen = Screen.AllScreens
                    For s = 0 To myScreens.Count - 1
                        screenWidth += myScreens(s).Bounds.Width
                        screenHeight += myScreens(s).Bounds.Height
                    Next
                    End Sub
                    
                    Private Sub MyLabel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyLabel.Paint
                    If offScreen = 0 then
                    ' do the painting
                    End If
                    End Sub
                    
                    Private Sub Me_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
                    offScreen = 0
                    If sender.left + Me.Width > screenWidth OrElse sender.top + Me.Height > screenHeight OrElse sender.left < 0 OrElse sender.top < 0 Then offScreen = 1
                    If offScreen = 0 Then
                       MyLabel.Refresh()
                    End If
                    End Sub

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      What if in your Me_Move event you just invalidated the label?

                      Comment

                      • robertybob
                        New Member
                        • Feb 2013
                        • 116

                        #12
                        Ha! Genius! Was working this harder than needed. Seems to work a charm. Many thanks, Rabbit.

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          Glad you got it resolved. Good luck with your project.

                          Comment

                          Working...