Recent

Author Topic: Always the right size of a form  (Read 1447 times)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Always the right size of a form
« on: February 20, 2018, 12:02:10 pm »
i did a lot of programming in MS Access. But since Mac OSx doesn't have MS Access i re-programmed my database in Lazarus.
I Access i had a function that could resize any form to the resolution of the screen. If the form was design in 1024x768 and the screen resolution of the new user is 1680x1050. The form would be scales 1.35 times it original size.


I used the following Visual Basic code with i found somewhere on the internet.
Is it possible to re-write it to a Lazarus function?
Code: Text  [Select][+][-]
  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "FormWindow"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. '*************************************************************
  11. ' Class module: FormWindow                                 *
  12. '*************************************************************
  13. ' Moves and resizes a window in the coordinate system        *
  14. ' of its parent window.                                      *
  15. ' N.B.: This class was developed for use on Access forms     *
  16. '       and has not been tested for use with other window    *
  17. '       types.                                               *
  18. '*************************************************************
  19. Option Compare Database
  20. Option Explicit
  21. '*************************************************************
  22. ' Type declarations
  23. '*************************************************************
  24. Private Type RECT       'RECT structure used for API calls.
  25.     Left As Long
  26.     Top As Long
  27.     Right As Long
  28.     Bottom As Long
  29. End Type
  30.  
  31.  
  32. Private Type POINTAPI   'POINTAPI structure used for API calls.
  33.     x As Long
  34.     Y As Long
  35. End Type
  36.  
  37.  
  38. '*************************************************************
  39. ' Member variables
  40. '*************************************************************
  41. Private m_hWnd As Long          'Handle of the window.
  42. Private m_rctWindow As RECT     'Rectangle describing the sides of the last polled location of the window.
  43.  
  44.  
  45. '*************************************************************
  46. ' Private error constants for use with RaiseError procedure
  47. '*************************************************************
  48. Private Const m_ERR_INVALIDHWND = 1
  49. Private Const m_ERR_NOPARENTWINDOW = 2
  50.  
  51.  
  52. '*************************************************************
  53. ' API function declarations
  54. '*************************************************************
  55. Private Declare Function apiIsWindow Lib "user32" Alias "IsWindow" (ByVal hWnd As Long) As Long
  56. Private Declare Function apiMoveWindow Lib "user32" Alias "MoveWindow" (ByVal hWnd As Long, ByVal x As Long, ByVal Y As Long, _
  57.     ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
  58.     'Moves and resizes a window in the coordinate system of its parent window.
  59. Private Declare Function apiGetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hWnd As Long, lpRect As RECT) As Long
  60.     'After calling, the lpRect parameter contains the RECT structure describing the sides of the window in screen coordinates.
  61. Private Declare Function apiScreenToClient Lib "user32" Alias "ScreenToClient" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
  62.     'Converts lpPoint from screen coordinates to the coordinate system of the specified client window.
  63. Private Declare Function apiGetParent Lib "user32" Alias "GetParent" (ByVal hWnd As Long) As Long
  64.     'Returns the handle of the parent window of the specified window.
  65.  
  66.  
  67. '*************************************************************
  68. ' Private procedures
  69. '*************************************************************
  70. Private Sub RaiseError(ByVal lngErrNumber As Long, ByVal strErrDesc As String)
  71. 'Raises a user-defined error to the calling procedure.
  72.  
  73.  
  74.     err.Raise vbObjectError + lngErrNumber, "FormWindow", strErrDesc
  75. End Sub
  76.  
  77.  
  78. Private Sub UpdateWindowRect()
  79. 'Places the current window rectangle position (in pixels, in coordinate system of parent window) in m_rctWindow.
  80. Dim ptCorner As POINTAPI
  81.    
  82.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  83.         apiGetWindowRect m_hWnd, m_rctWindow   'm_rctWindow now holds window coordinates in screen coordinates.
  84.        
  85.         If Not Me.Parent Is Nothing Then
  86.             'If there is a parent window, convert top, left of window from screen coordinates to parent window coordinates.
  87.             With ptCorner
  88.                 .x = m_rctWindow.Left
  89.                 .Y = m_rctWindow.Top
  90.             End With
  91.        
  92.             apiScreenToClient Me.Parent.hWnd, ptCorner
  93.        
  94.             With m_rctWindow
  95.                 .Left = ptCorner.x
  96.                 .Top = ptCorner.Y
  97.             End With
  98.    
  99.             'If there is a parent window, convert bottom, right of window from screen coordinates to parent window coordinates.
  100.             With ptCorner
  101.                 .x = m_rctWindow.Right
  102.                 .Y = m_rctWindow.Bottom
  103.             End With
  104.        
  105.             apiScreenToClient Me.Parent.hWnd, ptCorner
  106.        
  107.             With m_rctWindow
  108.                 .Right = ptCorner.x
  109.                 .Bottom = ptCorner.Y
  110.             End With
  111.         End If
  112.     Else
  113.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  114.     End If
  115. End Sub
  116. '*************************************************************
  117. ' Public read-write properties
  118. '*************************************************************
  119. Public Property Get hWnd() As Long
  120. 'Returns the value the user has specified for the window's handle.
  121.  
  122.  
  123.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  124.         hWnd = m_hWnd
  125.     Else
  126.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  127.     End If
  128.    
  129. End Property
  130.  
  131.  
  132. Public Property Let hWnd(ByVal lngNewValue As Long)
  133. 'Sets the window to use by specifying its handle.
  134. 'Only accepts valid window handles.
  135.     If lngNewValue = 0 Or apiIsWindow(lngNewValue) Then
  136.         m_hWnd = lngNewValue
  137.     Else
  138.         RaiseError m_ERR_INVALIDHWND, "The value passed to the hWnd property is not a valid window handle."
  139.     End If
  140. End Property
  141. '----------------------------------------------------
  142. Public Property Get Left() As Long
  143. 'Returns the current position (in pixels) of the left edge of the window in the coordinate system of its parent window.
  144.  
  145.  
  146.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  147.         UpdateWindowRect
  148.         Left = m_rctWindow.Left
  149.     Else
  150.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  151.     End If
  152. End Property
  153.  
  154.  
  155. Public Property Let Left(ByVal lngNewValue As Long)
  156. 'Moves the window such that its left edge falls at the position indicated
  157. '(measured in pixels, in the coordinate system of its parent window).
  158.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  159.         UpdateWindowRect
  160.         With m_rctWindow
  161.             apiMoveWindow m_hWnd, lngNewValue, .Top, .Right - .Left, .Bottom - .Top, True
  162.         End With
  163.     Else
  164.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  165.     End If
  166. End Property
  167. '----------------------------------------------------
  168. Public Property Get Top() As Long
  169. 'Returns the current position (in pixels) of the top edge of the window in the coordinate system of its parent window.
  170.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  171.         UpdateWindowRect
  172.         Top = m_rctWindow.Top
  173.     Else
  174.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  175.     End If
  176. End Property
  177.  
  178.  
  179. Public Property Let Top(ByVal lngNewValue As Long)
  180. 'Moves the window such that its top edge falls at the position indicated
  181. '(measured in pixels, in the coordinate system of its parent window).
  182.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  183.         UpdateWindowRect
  184.         With m_rctWindow
  185.             apiMoveWindow m_hWnd, .Left, lngNewValue, .Right - .Left, .Bottom - .Top, True
  186.         End With
  187.     Else
  188.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  189.     End If
  190. End Property
  191. '----------------------------------------------------
  192. Public Property Get Width() As Long
  193. 'Returns the current width (in pixels) of the window.
  194.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  195.         UpdateWindowRect
  196.         With m_rctWindow
  197.             Width = .Right - .Left
  198.         End With
  199.     Else
  200.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  201.     End If
  202. End Property
  203.  
  204.  
  205. Public Property Let Width(ByVal lngNewValue As Long)
  206. 'Changes the width of the window to the value provided (in pixels).
  207.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  208.         UpdateWindowRect
  209.         With m_rctWindow
  210.             apiMoveWindow m_hWnd, .Left, .Top, lngNewValue, .Bottom - .Top, True
  211.         End With
  212.     Else
  213.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  214.     End If
  215. End Property
  216. '----------------------------------------------------
  217. Public Property Get Height() As Long
  218. 'Returns the current height (in pixels) of the window.
  219.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  220.         UpdateWindowRect
  221.         With m_rctWindow
  222.             Height = .Bottom - .Top
  223.         End With
  224.     Else
  225.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  226.     End If
  227. End Property
  228.  
  229.  
  230. Public Property Let Height(ByVal lngNewValue As Long)
  231. 'Changes the height of the window to the value provided (in pixels).
  232.     If m_hWnd = 0 Or apiIsWindow(m_hWnd) Then
  233.         UpdateWindowRect
  234.         With m_rctWindow
  235.             apiMoveWindow m_hWnd, .Left, .Top, .Right - .Left, lngNewValue, True
  236.         End With
  237.     Else
  238.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  239.     End If
  240. End Property
  241. '*************************************************************
  242. ' Public read-only properties
  243. '*************************************************************
  244. Public Property Get Parent() As FormWindow
  245. 'Returns the parent window as a FormWindow object.
  246. 'For forms, this should be the Access MDI window.
  247. Dim fwParent As New FormWindow
  248. Dim lngHWnd As Long
  249.    
  250.     If m_hWnd = 0 Then
  251.         Set Parent = Nothing
  252.     ElseIf apiIsWindow(m_hWnd) Then
  253.         lngHWnd = apiGetParent(m_hWnd)
  254.         fwParent.hWnd = lngHWnd
  255.         Set Parent = fwParent
  256.     Else
  257.         RaiseError m_ERR_INVALIDHWND, "The window handle " & m_hWnd & " is no longer valid."
  258.     End If
  259.     Set fwParent = Nothing
  260. End Property
  261.  
« Last Edit: February 20, 2018, 12:04:58 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Always the right size of a form
« Reply #1 on: February 20, 2018, 02:03:58 pm »
Is it possible to re-write it to a Lazarus function?
Yes, but don't bother. Lazarus forms have ScaleBy method that you can use. If you want to scale form to 135% then you call it like this:
Code: Pascal  [Select][+][-]
  1. YourFormName.ScaleBy(135, 100);

For your specific example I would call it like this:
Code: Pascal  [Select][+][-]
  1. YourFormName.ScaleBy(1680, 1024);

or
Code: Pascal  [Select][+][-]
  1. YourFormName.ScaleBy(1050, 768);

whatever ratio is less of those two (just in case screen width/height proportions differ).

You could also take a look at this thread (besides screen resolution dpi also matters):
https://forum.lazarus.freepascal.org/index.php/topic,40139.msg277005.html#msg277005
« Last Edit: February 20, 2018, 02:09:42 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018