Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get position and size of Form on screen ? 4

Status
Not open for further replies.

MrPeds

Programmer
Jan 7, 2003
219
0
0
GB
Hi,

Is it possible in Access 2002/VBA to determine the coordinates of where a form is positioned on a screen and the the forms size?

I have an application that has a number of forms open at the same time and I want to be able to let the user position/size the forms on the screen where they want them and then save their positions. Therefore, when they come back to this part of the application in future the forms will re-open at the users saved positions.

I've seen there are some properties such as GridX, GridY , Width, WIndowHeight ad WIndowWidth but am not sure if this is what I need.

Thanks in advance,

MrPeds

 
Hi,

This example stores a popup form's opening position and size using the GetWindowRect API.

Paste this into a Module's Declaration section:
Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Place this in the Declarations section of the form:
Dim lngTop As Long, lngLeft As Long, lngRight As Long, lngBottom As Long

Place this in the form's On Open event:
On Error Resume Next
Dim r As RECT, retval As Long
retval = GetWindowRect(Me.hwnd, r)
lngTop = r.Top: lngLeft = r.Left: lngRight = r.Right: lngBottom = r.Bottom

Place this in the On Click event of a button on the form to test it:
DoCmd.MoveSize lngTop * 15, lngLeft * 15, lngRight * 15, lngBottom * 15

Reposition and resize the form then click the button, the form should return to its original startup postion and size. Use the On Unload event of the form to store lngTop * 15, lngLeft * 15 etc and the On Open event with the MoveSize action. Say if lngTop * 15 = 30 (2 * 15) at On Unload, then On Open would be MoveSize 30 etc.

Hope this makes sense.

Bill
 
You can use the GetWindowPlacement and SetWindowPlacement API calls to get and/or set the form position. Please Note that these two function work in pixels.

In a module drop in the following declarations:
Code:
Public Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Public Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

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

Public Type WINDOWPLACEMENT
        Length As Long
        flags As Long
        showCmd As Long
        ptMinPosition As POINTAPI
        ptMaxPosition As POINTAPI
        rcNormalPosition As RECT
End Type
The in the form, in this case, in a button call cmdInit, you can use the following code to get the current form’s position, and this further place the form in the upper left hand corner.
Code:
Private Sub cmdInit_Click()
   
   Dim MyWindowPos   As RECT
   Dim MyWindowParams As WINDOWPLACEMENT
   Dim RetVal As Long
   
   MyWindowParams.Length = Len(MyWindowParams)
   RetVal = GetWindowPlacement(Me.hwnd, MyWindowParams)
   MyWindowPos = MyWindowParams.rcNormalPosition
   MsgBox "Left = " & MyWindowPos.Left
   MsgBox "Top = " & MyWindowPos.Top
   
   MyWindowPos.Left = 0
   MyWindowPos.Top = 0
   MyWindowParams.rcNormalPosition = MyWindowPos
   SetWindowPlacement Me.hwnd, MyWindowParams

End Sub


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
It seems billpower that we were posting at the same time.

MrPeds, it seems that you now have two options.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
thanks for this guys, I will post my solution in due course.

I think that this kind of customisation is very useful for todays applications.

MrPeds
 
when i tried to compile my project using the second method I got a compile error on the POINTAPI variable.

DO I need to import this into my project?

Thanks,

MrPeds
 
The POINTAPI Type struction should be defined as follows:
Code:
Public Type POINTAPI
    tLng_Xloc  As Long
    tLng_YLoc  As Long
End Type
Add it right next to the other two Public Type definitions.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Cajun,

I've been trying to get your code to work in my DB (Access 2003) but I'm getting the error"

Compile Error:

Forward reference to user defined Type

What am I doing wrong?

Cheers

Dean

"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
Cajun,

Found my error. But I also found an issue with your code as well.

I wanted to reposition my form anywhere on the screen and when I closed it and reopened it, It would be where i left it. Basicaly, I had to do the following to get the end product.

In a module:

Code:
Public Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long
Public Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Public Type POINTAPI
    tLng_Xloc  As Long
    tLng_YLoc  As Long
End Type

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

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

In the form's on_Unload event:
Code:
MyWindowParams.Length = Len(MyWindowParams)
   RetVal = GetWindowPlacement(Me.hwnd, MyWindowParams)
   MyWindowPos = MyWindowParams.rcNormalPosition
   'msgBox "Left = " & MyWindowPos.Left
   'msgBox "Top = " & MyWindowPos.Top
   
  
   DoCmd.RunCommand acCmdSave
Notice the ACCmdSave? This was needed to save the new position properties

In the Form's on_Open Event:

Code:
MyWindowParams.rcNormalPosition = MyWindowPos
SetWindowPlacement Me.hwnd, MyWindowParams

The bueaty of this is now If I want to position my form anywhere on the screen. That's where it will open everytime until I decide to change it.

Cheers

Dean



"The most overlooked advantage of owning a computer is that if they foul up there's no law against whacking them around a bit."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top