How to change the Caption of a Borderless Form while Minimized using a Timer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicebasic
    New Member
    • Aug 2010
    • 91

    How to change the Caption of a Borderless Form while Minimized using a Timer?

    I have a VB project that has a Borderless form. I'm trying to update its caption while it has been minimized, but I can't.

    The code in the "Form" is is follows:
    Code:
    Public myCounter As Integer
    
    Private Sub cmdExit_Click()
        End
    End Sub
    
    Private Sub cmdMinimize_Click()
        Me.WindowState = vbMinimized
    End Sub
    
    Private Sub myTimer_Timer()
        myCounter = myCounter + 1
        Me.Caption = myCounter
    End Sub

    When I click on its "Minimized Caption Bar" or when it Loses Focus, its caption will be updated with the code in the Timer. But this is not what I'm looking for. I'd like to have it updated with the code in the Timer without gaining or losing focus.

    I tested other ways. For example, I changed the code in "Sub myTimer_Timer() " to this:

    Code:
    Private Sub myTimer_Timer()
        myCounter = myCounter + 1
        Me.Show
        Me.Caption = myCounter
    End Sub
    or even to this:

    Code:
    Private Sub myTimer_Timer()
        myCounter = myCounter + 1
        Me.SetFocus
        Me.Caption = myCounter
    End Sub
    Using these two tricks, I can have it updated according to the Interval set for the Timer. But the problem is that its "Minmized Caption Bar" blinks and is rather annoying.

    How can I have it updated "Without Blinking"?

    Thank you for your help.
    Attached Files
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    You can only change the caption if border is shown.
    So if You minimize, set the border ON.
    When You set the form back (resize), set the border back OFF.

    Code:
    Public myCounter As Integer
    
    Private Sub cmdExit_Click()
        End
    End Sub
    
    Private Sub cmdMinimize_Click()
        With Me
          .WindowState = vbMinimized
          .ZOrder
          .BorderStyle = vbFixedSingle
       End With
    End Sub
    
    Private Sub Form_Resize()
       Me.BorderStyle = vbBSNone
    End Sub
    
    Private Sub myTimer_Timer()
        myCounter = myCounter + 1
          Me.Caption = myCounter
    End Sub

    Comment

    • nicebasic
      New Member
      • Aug 2010
      • 91

      #3
      Thank you, Mr. Guido Geurs,

      You're a great expert. I applied your code to the project. Attached to this post, you'll find the "Modified Project". But, something is wrong.

      When I minimize the form, its caption will be updated as I needed. But, something really odd happens. Every time, after minimizing the form, if you double click on the minimized form to restore it, it will be restored, but, unfortunately, it shrinks and gets smaller.

      If you minimize and restore the form several times, you won't see anything when restoring the form. The form vanishes.
      Attached Files

      Comment

      • nicebasic
        New Member
        • Aug 2010
        • 91

        #4
        I made some changes to the code to fix this bug. Here's the new code:
        Code:
        Public myCounter As Integer
        Public myFormWidth As Long
        Public myFormheight As Long
        
        Private Sub cmdExit_Click()
            End
        End Sub
        
        Private Sub cmdMinimize_Click()
            With Me
                .WindowState = vbMinimized
                .ZOrder
                .BorderStyle = vbFixedSingle
            End With
        End Sub
        
        Private Sub Form_Load()
            myFormWidth = Me.Width
            myFormheight = Me.Height
        End Sub
        
        Private Sub Form_Paint()
            Me.Width = myFormWidth
            Me.Height = myFormheight
        End Sub
        
        Private Sub Form_Resize()
            Me.BorderStyle = vbBSNone
        End Sub
        
        Private Sub myTimer_Timer()
            myCounter = myCounter + 1
            Me.Caption = myCounter
        End Sub
        Thanks to your help, nearly 90% of the job has been done.

        There is only one problem. If you minimize the form, and then, double click on the "Minimized Caption Bar" to restore the form, at first, the Caption of the Form is visible and after a very very short time, it changes to nothing and becomes invisible.

        Is there any method to fix this bug? I mean, when you double click on the "Minimized Caption Bar" to restore the form, prevent the Caption of the Form to be visible even for a very very short time?

        Thank you for your great help.
        Attached Files

        Comment

        • Guido Geurs
          Recognized Expert Contributor
          • Oct 2009
          • 767

          #5
          With the code=
          Code:
          Private Sub cmdMinimize_Click() 
              With Me 
                  .WindowState = vbMinimized 
                  .ZOrder 
                  .BorderStyle = vbFixedSingle 
              End With 
          End Sub 
          
          Private Sub Form_Resize() 
              Me.BorderStyle = vbBSNone 
          End Sub
          When The code does a Minimize then it takes (remembers) the size of the "No border" and set then the border "FixedSingl e" with the size of "No Border".
          When it Resizes, the borders will be the original min borderwidth and the titelbar !

          With the code:
          Code:
          Public myCounter As Integer
          
          Private Sub cmdExit_Click()
              End
          End Sub
          
          Private Sub cmdMinimize_Click()
              With Me
                  .BorderStyle = vbFixedSingle
                  .WindowState = vbMinimized
                  .ZOrder
              End With
          End Sub
          
          Private Sub Form_Resize()
                   Me.BorderStyle = vbBSNone
                   Me.ZOrder
          End Sub
          
          Private Sub myTimer_Timer()
              myCounter = myCounter + 1
              Me.Caption = myCounter
          End Sub
          It's OK : it sets first the frame back and then minimizes.

          Comment

          • nicebasic
            New Member
            • Aug 2010
            • 91

            #6
            I changed the following parts of the code using your suggested code:
            Code:
            Private Sub cmdMinimize_Click()
                With Me
                    .BorderStyle = vbFixedSingle
                    .WindowState = vbMinimized
                    .ZOrder
                End With
            End Sub
             
            Private Sub Form_Resize()
                     Me.BorderStyle = vbBSNone
                     Me.ZOrder
            End Sub
            But, this way, the "Minimized Caption Bar" doesn't get updated. It only gets updated when losing and gaining focus.

            You're a great expert. Thank you a million times for your kind help.

            I'm so happy to see intelligent experts like you in Bytes.com (a really valuable and excellent website for programmers).

            Best wishes for you.

            Comment

            • Guido Geurs
              Recognized Expert Contributor
              • Oct 2009
              • 767

              #7
              Sorry for my inattention but I think this is finally the one:

              Code:
              Dim ORGWIDTH As Long
              Dim ORGHEIGHT As Long
              Public myCounter As Integer
              
              Private Sub cmdExit_Click()
                  End
              End Sub
              
              Private Sub cmdMinimize_Click()
                  With Me
                    .WindowState = vbMinimized
                    .ZOrder
                 End With
              End Sub
              
              Private Sub Form_Load()
                 With Me
                    ORGWIDTH = .Width
                    ORGHEIGHT = .Height
                 End With
              End Sub
              
              Private Sub Form_Resize()
                 With Me
                    If .WindowState = vbMinimized Then
                       .BorderStyle = vbFixedSingle
                    Else
                       .BorderStyle = vbBSNone
                       .Width = ORGWIDTH
                       .Height = ORGHEIGHT
                    End If
                    .ZOrder
                 End With
              End Sub
              
              Private Sub myTimer_Timer()
                 myCounter = myCounter + 1
                 Me.Caption = myCounter
              End Sub

              Comment

              • nicebasic
                New Member
                • Aug 2010
                • 91

                #8
                Thank you, sir.

                You're so kind and attentive to this matter. I appreciate your help.

                This is the code with your modifications. But, still the problem is there. When you "Double Click" on the "Minimized Caption Bar", it will be resized to the "Original Form Size", but at first, the Caption of the Form is visible. After a very short time, it disappears. This doesn't make any problems, though.

                Again, thank you for your attention.
                Attached Files

                Comment

                • Guido Geurs
                  Recognized Expert Contributor
                  • Oct 2009
                  • 767

                  #9
                  After a few trays I think I have found the solution for resetting the border immediately ofter maximize: Only the code "Me.Caption = Me.Caption" will rebuild the border.
                  Not .Refresh, ... ore anything else!

                  So the code must be=
                  Code:
                  Dim ORGWIDTH As Long
                  Dim ORGHEIGHT As Long
                  Public myCounter As Integer
                  
                  Private Sub Form_Load()
                     With Me
                        ORGWIDTH = .Width
                        ORGHEIGHT = .Height
                     End With
                  End Sub
                  
                  Private Sub Form_Resize()
                     With Me
                        If .WindowState = vbMinimized Then
                           .BorderStyle = vbFixedSingle
                        Else
                           .BorderStyle = vbBSNone
                           .Caption = .Caption
                           .Width = ORGWIDTH
                           .Height = ORGHEIGHT
                        End If
                        .ZOrder
                     End With
                  End Sub
                  
                  Private Sub cmdExit_Click()
                      End
                  End Sub
                  
                  Private Sub cmdMinimize_Click()
                      With Me
                        .WindowState = vbMinimized
                        .ZOrder
                     End With
                  End Sub
                  
                  Private Sub myTimer_Timer()
                     myCounter = myCounter + 1
                     Me.Caption = myCounter
                  End Sub

                  Comment

                  • nicebasic
                    New Member
                    • Aug 2010
                    • 91

                    #10
                    Thank you, Mr. Guido Geurs,

                    Honestly, I have to say that you're a really great and talented Solution Developer. I can't believe that it has been solved.

                    Using your incredible method to solve this problem, I added one line to your final solution.

                    Now, thanks to your great solution, when I click on the "Minimized Caption Bar" to restore the program to its Original Size, the problem has been fixed. But, when I click on the Minimize Button to Minimize the program, a problem still exists. I mean, if you press the Minimize Button, the program will be minimized, but for a very very short time, you can see the Minimized Caption Bar has "Windows Classic Style", even if you use "Windows XP Style" theme on your computer.

                    This is a modification of Sub Form_Resize():
                    Code:
                    Private Sub Form_Resize()
                       With Me
                          If .WindowState = vbMinimized Then
                             .BorderStyle = vbFixedSingle
                             .Caption = .Caption      '==> This line has been added!
                          Else
                             .BorderStyle = vbBSNone
                             .Caption = .Caption
                             .Width = ORGWIDTH
                             .Height = ORGHEIGHT
                          End If
                          .ZOrder
                       End With
                    End Sub
                    Actually, I was disappointed and I thought it wasn't possible to solve this problem. But you made it possible.

                    You did a great job. Thank you very much, my dear friend.

                    Comment

                    Working...