How does a form remember its position 
Author Message
 How does a form remember its position

I like a form to show up at the same position after user move it. I cannot
find a way to record its positon because there is no form.Move event. How
can this be done ?


Sun, 11 Mar 2012 23:39:21 GMT  
 How does a form remember its position
Manually - There is no automatic way to do this in VB6

So save the position when the program ends and retrieve it on load. The
registry is as good a place as any for this.
Be careful not to save the positions if the app is minimized or maximized
(you can use GetWindowPlacement to find the form position when it is normal
regardless of its current state)

Dave O.


Quote:
>I like a form to show up at the same position after user move it. I cannot
>find a way to record its positon because there is no form.Move event. How
>can this be done ?



Sun, 11 Mar 2012 23:55:48 GMT  
 How does a form remember its position

Quote:
>I like a form to show up at the same position after user move it. I cannot
>find a way to record its positon because there is no form.Move event. How
>can this be done ?

Use a little known property called Moveable to False. You can still move the
form through code.


Mon, 12 Mar 2012 00:04:49 GMT  
 How does a form remember its position
I don't know about this. I will try that. But can you tell me a little more


Quote:


>>I like a form to show up at the same position after user move it. I cannot
>>find a way to record its positon because there is no form.Move event. How
>>can this be done ?

> Use a little known property called Moveable to False. You can still move
> the form through code.



Mon, 12 Mar 2012 00:14:12 GMT  
 How does a form remember its position
User cannot move the form, just like you said, which I missed. Not much
useful.
Thanks anyway.


Quote:


>>I like a form to show up at the same position after user move it. I cannot
>>find a way to record its positon because there is no form.Move event. How
>>can this be done ?

> Use a little known property called Moveable to False. You can still move
> the form through code.



Mon, 12 Mar 2012 00:20:17 GMT  
 How does a form remember its position

Quote:
> User cannot move the form, just like you said, which I missed. Not much
> useful.

I misunderstood your question. Use SaveSetting function in Form_Unload to
save the last position, then use it on Form_Load.


Mon, 12 Mar 2012 00:30:26 GMT  
 How does a form remember its position
Thanks. This is likely the consensus.


Quote:


>> User cannot move the form, just like you said, which I missed. Not much
>> useful.

> I misunderstood your question. Use SaveSetting function in Form_Unload to
> save the last position, then use it on Form_Load.



Mon, 12 Mar 2012 00:59:30 GMT  
 How does a form remember its position
I was not clear in my OP. I only want to the form to show in the same
session, not across all session. So I just save the postion in Unload,
re-position during Load. Thanks


Quote:
>I like a form to show up at the same position after user move it. I cannot
>find a way to record its positon because there is no form.Move event. How
>can this be done ?



Mon, 12 Mar 2012 07:55:07 GMT  
 How does a form remember its position
Phil Hunt escribi:

Quote:
> I was not clear in my OP. I only want to the form to show in the same
> session, not across all session. So I just save the postion in Unload,
> re-position during Load. Thanks

To know if it's the first time the forms loads in the session, you can
use a flag in the form initialize event:

Option Explicit

Private Type POINTAPI
         x As Long
         y As Long
End Type

Private Type RECT
         Left As Long
         Top As Long
         Right As Long
         Bottom As Long
End Type

Private Type WINDOWPLACEMENT
         Length As Long
         flags As Long
         showCmd As Long
         ptMinPosition As POINTAPI
         ptMaxPosition As POINTAPI
         rcNormalPosition As RECT
End Type

Private Declare Function GetWindowPlacement Lib "user32" _
     (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Private mFirstTime As Boolean

Private Sub Form_Initialize()
     mFirstTime = True
End Sub

Private Sub Form_Load()
     If Not mFirstTime Then
         Me.Move GetSetting(App.Title, "FormPos", Me.Name & _
             ".L", Me.Left), GetSetting(App.Title, "FormPos", _
             Me.Name & ".T", Me.Top), GetSetting(App.Title, _
             "FormPos", Me.Name & ".W", Me.Width), GetSetting _
             (App.Title, "FormPos", Me.Name & ".H", Me.Height)
         If GetSetting(App.Title, "FormPos", Me.Name & ".WS", _
             Me.WindowState) = vbMaximized Then
             Me.WindowState = vbMaximized
         End If
     Else
         mFirstTime = False
     End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
     Dim iWP As WINDOWPLACEMENT

     If GetWindowPlacement(Me.hwnd, iWP) Then
         SaveSetting App.Title, "FormPos", Me.Name & ".L", _
             iWP.rcNormalPosition.Left * Screen.TwipsPerPixelX
         SaveSetting App.Title, "FormPos", Me.Name & ".T", _
             iWP.rcNormalPosition.Top * Screen.TwipsPerPixelX
         SaveSetting App.Title, "FormPos", Me.Name & ".W", _
             (iWP.rcNormalPosition.Right - iWP.rcNormalPosition.Left) _
             * Screen.TwipsPerPixelX
         SaveSetting App.Title, "FormPos", Me.Name & ".H", _
             (iWP.rcNormalPosition.Bottom - iWP.rcNormalPosition.Top) _
             * Screen.TwipsPerPixelX
         SaveSetting App.Title, "FormPos", Me.Name & ".WS", _
             Me.WindowState
     End If
End Sub



Mon, 12 Mar 2012 14:13:40 GMT  
 How does a form remember its position
Eduardo escribi:

Quote:
> Phil Hunt escribi:

>> I was not clear in my OP. I only want to the form to show in the same
>> session, not across all session. So I just save the postion in Unload,
>> re-position during Load. Thanks

> To know if it's the first time the forms loads in the session, you can
> use a flag in the form initialize event:

You don't have to set the form to nothing with this.


Mon, 12 Mar 2012 14:15:53 GMT  
 How does a form remember its position
Eduardo escribi:

Change TwipsPerPixelX by TwipsPerPixel*Y*

     If GetWindowPlacement(Me.hwnd, iWP) Then
         SaveSetting App.Title, "FormPos", Me.Name & ".L", _
             iWP.rcNormalPosition.Left * Screen.TwipsPerPixelX
         SaveSetting App.Title, "FormPos", Me.Name & ".T", _
             iWP.rcNormalPosition.Top * Screen.TwipsPerPixel*Y*
         SaveSetting App.Title, "FormPos", Me.Name & ".W", _
             (iWP.rcNormalPosition.Right - iWP.rcNormalPosition.Left) _
             * Screen.TwipsPerPixelX
         SaveSetting App.Title, "FormPos", Me.Name & ".H", _
             (iWP.rcNormalPosition.Bottom - iWP.rcNormalPosition.Top) _
             * Screen.TwipsPerPixel*Y*
         SaveSetting App.Title, "FormPos", Me.Name & ".WS", _
             Me.WindowState
     End If
End Sub



Mon, 12 Mar 2012 14:27:05 GMT  
 How does a form remember its position
Thanks. You must miss my clarification msg. I dont need to save between
session. So I just save the top and left in the form variable. Problem
solved.
Quote:

> Phil Hunt escribi:

>> I was not clear in my OP. I only want to the form to show in the same
>> session, not across all session. So I just save the postion in Unload,
>> re-position during Load. Thanks

> To know if it's the first time the forms loads in the session, you can use
> a flag in the form initialize event:

> Option Explicit

> Private Type POINTAPI
>         x As Long
>         y As Long
> End Type

> Private Type RECT
>         Left As Long
>         Top As Long
>         Right As Long
>         Bottom As Long
> End Type

> Private Type WINDOWPLACEMENT
>         Length As Long
>         flags As Long
>         showCmd As Long
>         ptMinPosition As POINTAPI
>         ptMaxPosition As POINTAPI
>         rcNormalPosition As RECT
> End Type

> Private Declare Function GetWindowPlacement Lib "user32" _
>     (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

> Private mFirstTime As Boolean

> Private Sub Form_Initialize()
>     mFirstTime = True
> End Sub

> Private Sub Form_Load()
>     If Not mFirstTime Then
>         Me.Move GetSetting(App.Title, "FormPos", Me.Name & _
>             ".L", Me.Left), GetSetting(App.Title, "FormPos", _
>             Me.Name & ".T", Me.Top), GetSetting(App.Title, _
>             "FormPos", Me.Name & ".W", Me.Width), GetSetting _
>             (App.Title, "FormPos", Me.Name & ".H", Me.Height)
>         If GetSetting(App.Title, "FormPos", Me.Name & ".WS", _
>             Me.WindowState) = vbMaximized Then
>             Me.WindowState = vbMaximized
>         End If
>     Else
>         mFirstTime = False
>     End If
> End Sub

> Private Sub Form_Unload(Cancel As Integer)
>     Dim iWP As WINDOWPLACEMENT

>     If GetWindowPlacement(Me.hwnd, iWP) Then
>         SaveSetting App.Title, "FormPos", Me.Name & ".L", _
>             iWP.rcNormalPosition.Left * Screen.TwipsPerPixelX
>         SaveSetting App.Title, "FormPos", Me.Name & ".T", _
>             iWP.rcNormalPosition.Top * Screen.TwipsPerPixelX
>         SaveSetting App.Title, "FormPos", Me.Name & ".W", _
>             (iWP.rcNormalPosition.Right - iWP.rcNormalPosition.Left) _
>             * Screen.TwipsPerPixelX
>         SaveSetting App.Title, "FormPos", Me.Name & ".H", _
>             (iWP.rcNormalPosition.Bottom - iWP.rcNormalPosition.Top) _
>             * Screen.TwipsPerPixelX
>         SaveSetting App.Title, "FormPos", Me.Name & ".WS", _
>             Me.WindowState
>     End If
> End Sub



Mon, 12 Mar 2012 21:02:51 GMT  
 How does a form remember its position


Quote:
>I was not clear in my OP. I only want to the form to show in the same
>session, not across all session. So I just save the postion in Unload,
>re-position during Load. Thanks

Hell, why unload the form at all then? Just hide it and re-show it.


Mon, 12 Mar 2012 21:21:44 GMT  
 How does a form remember its position
Maybe I should have done that. But I am just changing an existing program,
and changing unload to hiding could get me in more trouble. I said 'could'


Quote:


>>I was not clear in my OP. I only want to the form to show in the same
>>session, not across all session. So I just save the postion in Unload,
>>re-position during Load. Thanks

> Hell, why unload the form at all then? Just hide it and re-show it.



Mon, 12 Mar 2012 21:39:54 GMT  
 How does a form remember its position
Jeff Johnson escribi:

Quote:
>> I was not clear in my OP. I only want to the form to show in the same
>> session, not across all session. So I just save the postion in Unload,
>> re-position during Load. Thanks

> Hell, why unload the form at all then? Just hide it and re-show it.

Yep.

In the forms but the main one:

Private Sub Form_QueryUnload(Cancel As _
     Integer, UnloadMode As Integer)

     If UnloadMode = 0 Then
         Me.Hide
         Cancel = 1
     End If
End Sub

In the main form:

Private Sub Form_Unload(Cancel As Integer)
     Dim frm As Form

     For Each frm In Forms
         Unload frm
         Set frm = Nothing
     Next frm
End Sub



Mon, 12 Mar 2012 21:44:19 GMT  
 
 [ 21 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Remember last Form Position

2. Refresh on a database / data grid and remember position (VB4)

3. How to make program remember position on screen.

4. Refresh on a database / data grid and remember position (VB4)

5. "Remembering" formatting positions of text

6. Remembering cursor position in textbox

7. rich text box, remembering view / position of scroll bars

8. Form , remembering prior settings .

9. Inherited form and remembering controls

10. VB6 IDE doesn't seem to remember last open form states

11. Remembering last Form size...

12. Form size not properly remembered on program startup?!?

 

 
Powered by phpBB® Forum Software