Return Objects width and height after anchoring has taken effect

thechazm

VBA, VB.net, C#, Java
Local time
Yesterday, 19:59
Joined
Mar 7, 2011
Messages
515
How would I return and objects width and height after anchoring has taken affect?

Everytime I return the width and height from the properties of any object it always returns the value of what it was before the anchoring took affect. This is even after the object has been resized.

Thanks,

TheChazm
 
The only way I know of is to store the differences between the control's Width and Height and the form's InsideWidth and InsideHeight on load. Then at any moment the control's actual Width and Height are equal to the form's InsideWidth and InsideHeight minus those differences.

For example, with a textbox Text1 anchored to stretch down and across:

Code:
Option Compare Database
Option Explicit

Private Text1Hdiff As Long
Private Text1Wdiff As Long

Private Sub Form_Load()
    Text1Hdiff = Me.InsideHeight - Me.Text1.Height
    Text1Wdiff = Me.InsideWidth - Me.Text1.Width
End Sub

Public Function Text1Height() As Long
    Text1Height = Me.InsideHeight - Text1Hdiff
End Function

Public Function Text1Width() As Long
    Text1Width = Me.InsideWidth - Text1Wdiff
End Function
 
Last edited:
I appreciate the help but the issue is even after the control has been resized using anchoring I get the same width and height values that it had before the load. I did some tests and even after the form has been fully loaded I run some code under a button to return that objects width and height and it is the same.

Its really confusing its almost like those values are being stored in a special region or maybe my form is just corrupt or the control itself. I will try to do some more checks and just try it from a blank fresh database but as of now it looks like the anchoring width and height are stored in a different property.

Thanks,

TheChazm
 
I verified the form is not corrupt and it definatly has something to do with anchoring. I don't know what to do from here. Anyone have any ideas?
 
"I appreciate the help but the issue is even after the control has been resized using anchoring I get the same width and height values that it had before the load."

That's what the code is for. That is normal behaviour. You need code like what I posted to get the width and height at runtime (if the control is anchored and the form resizes).

"it looks like the anchoring width and height are stored in a different property"

They are not stored at all. You have do it yourself with something like the code I gave.

Did you try that code? It's not complex.
 
Yes it does not work either sorry to say. VilaRestal let me try to clear this up:

Create a blank form
Create a rectangle and set anchoring to stretch down and across
On the dblClick of the rectangle dump the size of itself
In the on open even of the form dump the size of the rectangle

Now if you actually look at both values they will be different. The weird part is that on the on open event of the form you get a pretty big size but its not really the full size of the anchor then the on dbl click event shows its a much smaller size even after achoring.

Still yet the value that the form returns during the on open event is still not the exact size of the anchor that was applied to the rectangle.

Now if you setup controls to use the rectangle as a boundry and you anchor one control on the left and one of the right you will notice that there is about a 6" area that you still cannot utilize even after the anchor has taken place.

I hope that makes more sense but I dunno. This one is weird.
 
Yeah it's because Access opens the form with the size of the design window.

And that doesn't count as a resize.

You need to save it in design with the design window the same size as the area of the form. And not tabbed documents. That will resize the form on load without a resize event.
 
With tabbed forms (always maximized) I don't think it's going to be possible without some quite complex API calls.
 
Yea I started looking at API's for that just a little while ago. Its to bad it cant be easier. Dare I say that maybe with Access version 15 it will actually return the correct values on tabbed documents lol.

Thanks for the help.
 
Hi,

I came across your discussion and found out I am not alone :)
I've just tackled the problem and would like to share the solution.

Code in a module
'==============================================================================
'Describes the width, height, and location of a rectangle.
Public Type RECT
x1 As Long
Y1 As Long
x2 As Long
Y2 As Long
End Type

'Number of pixels per logical inch along the screen width. In a system with multiple display monitors, this value is the same for all monitors.
Public Const WU_LOGPIXELSX = 88
'Number of pixels per logical inch along the screen height. In a system with multiple display monitors, this value is the same for all monitors.
Public Const WU_LOGPIXELSY = 90

' Retrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.
Public Declare Function GetFocus& Lib "user32" ()

'Retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
Public Declare Function GetClientRect& Lib "user32" (ByVal hWnd As Long, lpRect As RECT)

' The GetDeviceCaps function retrieves device-specific information for the specified device.
Public Declare Function GetDeviceCaps& Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long)

' The GetDC function retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC. The device context is an opaque data structure, whose values are used internally by GDI.
Public Declare Function GetDC& Lib "user32" (ByVal hWnd As Long)

' The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of theReleaseDC function depends on the type of DC. It frees only common and window DCs. It has no effect on class or private DCs
Public Declare Function ReleaseDC& Lib "user32" (ByVal hWnd As Long, ByVal hdc As Long)
'==============================================================================

code in a form's module:
'=============================
Private Function controlWidth(ctrl As control) As Long
Dim r As RECT
ctrl.SetFocus
GetClientRect& GetFocus&, r
controlWidth = Round((ctrl.Width * (r.x2 - r.x1)) / ConvertTwipsToPixels(ctrl.Width, 0), 0) + _
ctrl.LeftPadding + ctrl.RightPadding + ctrlGridlineSideWidth(ctrl)
End Function

Private Function ctrlGridlineSideWidth(ctrl As control) As Long
Dim lngWidth As Long
Const lngPointsInTwips As Long = 20 'Twips in a point
If ctrl.GridlineStyleLeft <> 0 Then
lngWidth = lngWidth + ctrl.GridlineWidthLeft * lngPointsInTwips
End If
If ctrl.GridlineStyleRight <> 0 Then
lngWidth = lngWidth + ctrl.GridlineWidthRight * lngPointsInTwips
End If
ctrlGridlineSideWidth = lngWidth
End Function

Function ConvertTwipsToPixels(lngTwips As Long, _
lngDirection As Long) As Long

'Twips to pixels

'Handle to device
Dim lngDC As Long
Dim lngPixelsPerInch As Long
Const nTwipsPerInch = 1440
lngDC = GetDC(0)

If (lngDirection = 0) Then 'Horizontal
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSX)
Else 'Vertical
lngPixelsPerInch = GetDeviceCaps(lngDC, WU_LOGPIXELSY)
End If
lngDC = ReleaseDC(0, lngDC)
ConvertTwipsToPixels = (lngTwips / nTwipsPerInch) * lngPixelsPerInch

End Function
'=============================

To get the full width of a control call the controlWidth function.
My controls were arranged in a layout, so I had to take into account this:
ctrl.LeftPadding + ctrl.RightPadding + ctrlGridlineSideWidth(ctrl)
If you don't need this part then drop it.

Hope it helped.

Sergiy Vakshul
 

Users who are viewing this thread

Back
Top Bottom