Results 1 to 16 of 16

Thread: [RESOLVED] Magnifying form

Hybrid View

  1. #1
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: [RESOLVED] Magnifying form

    Here is a much better solution.
    create a memory device context to hold the screen picture
    update that DC only when you start to move the form
    while the form is being moved, copy a region of that DC onto the form
    It works more smoothly if the borders of the form are removed or set to fixed single.
    VB Code:
    1. Option Explicit
    2.  
    3. Private hdcScreen As Long
    4. Private hdcCompatible As Long
    5. Private hbmScreen As Long
    6.  
    7. Private ScreenWidth As Long
    8. Private screenHeight As Long
    9. Private XX As Single
    10. Private YY As Single
    11.  
    12. Private ScaleFactor As Long
    13.  
    14.  
    15. Private Declare Function StretchBlt Lib "gdi32" ( _
    16.     ByVal hdc As Long, _
    17.     ByVal X As Long, _
    18.     ByVal Y As Long, _
    19.     ByVal nWidth As Long, _
    20.     ByVal nHeight As Long, _
    21.     ByVal hSrcDC As Long, _
    22.     ByVal xSrc As Long, _
    23.     ByVal ySrc As Long, _
    24.     ByVal nSrcWidth As Long, _
    25.     ByVal nSrcHeight As Long, _
    26.     ByVal dwRop As Long _
    27. ) As Long
    28.  
    29. Private Declare Function GetWindowRect Lib "user32" ( _
    30.     ByVal hwnd As Long, _
    31.     lpRect As RECT _
    32. ) As Long
    33.  
    34. Private Declare Function GetClientRect Lib "user32" ( _
    35.     ByVal hwnd As Long, _
    36.     lpRect As RECT _
    37. ) As Long
    38.  
    39. Private Type RECT
    40.         Left As Long
    41.         Top As Long
    42.         Right As Long
    43.         Bottom As Long
    44. End Type
    45.  
    46.  
    47. Private Declare Function CreateCompatibleDC Lib "gdi32" ( _
    48.     ByVal hdc As Long _
    49. ) As Long
    50.  
    51. Private Declare Function BitBlt Lib "gdi32" ( _
    52.     ByVal hDestDC As Long, _
    53.     ByVal X As Long, _
    54.     ByVal Y As Long, _
    55.     ByVal nWidth As Long, _
    56.     ByVal nHeight As Long, _
    57.     ByVal hSrcDC As Long, _
    58.     ByVal xSrc As Long, _
    59.     ByVal ySrc As Long, _
    60.     ByVal dwRop As Long _
    61. ) As Long
    62.  
    63. Private Declare Function DeleteDC Lib "gdi32" ( _
    64.     ByVal hdc As Long _
    65. ) As Long
    66.  
    67. Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" ( _
    68.     ByVal lpDriverName As String, _
    69.     ByVal lpDeviceName As String, _
    70.     ByVal lpOutput As String, _
    71.     ByVal lpInitData As Long _
    72. ) As Long
    73.  
    74. Private Declare Function CreateCompatibleBitmap Lib "gdi32" ( _
    75.     ByVal hdc As Long, _
    76.     ByVal nWidth As Long, _
    77.     ByVal nHeight As Long _
    78. ) As Long
    79.  
    80. Private Declare Function GetDeviceCaps Lib "gdi32" ( _
    81.     ByVal hdc As Long, _
    82.     ByVal nIndex As Long _
    83. ) As Long
    84.  
    85. Private Declare Function SelectObject Lib "gdi32" ( _
    86.     ByVal hdc As Long, _
    87.     ByVal hObject As Long _
    88. ) As Long
    89.  
    90. Private Declare Function DeleteObject Lib "gdi32" ( _
    91.     ByVal hObject As Long _
    92. ) As Long
    93.  
    94. Private Const HORZRES = 8            '  Horizontal width in pixels
    95. Private Const VERTRES = 10           '  Vertical width in pixels
    96.  
    97. Sub UpDateImage()
    98.     Me.Visible = False
    99.     DoEvents
    100.          'Copy color data for the entire display into a
    101.          'bitmap that is selected into a compatible DC.
    102.  
    103.         If BitBlt(hdcCompatible, _
    104.                0, 0, _
    105.                ScreenWidth, screenHeight, _
    106.                hdcScreen, _
    107.                0, 0, _
    108.                vbSrcCopy) = 0 Then
    109.  
    110.                 MsgBox "Screen to Compat Blt Failed"
    111.             Exit Sub
    112.         End If
    113.     Me.Visible = True
    114.     DoEvents
    115. End Sub
    116.  
    117. Sub ZoomIn(ZoomFactor As Long)
    118.  Dim xLeft As Long, ytop As Long
    119.  Dim nSrcWidth As Long, nSrcHeight As Long
    120.  Dim ScaleFactor As Single
    121.  Dim xBorder As Long
    122.  Dim yBorder As Long
    123.  Dim wndRect As RECT
    124.  Dim clientRect As RECT
    125.  
    126.  GetWindowRect Me.hwnd, wndRect
    127.  GetClientRect Me.hwnd, clientRect
    128.  
    129. 'get size and location of form
    130.  xBorder = Round((wndRect.Right - wndRect.Left - clientRect.Right) / 2, 0)
    131.  yBorder = wndRect.Bottom - wndRect.Top - clientRect.Bottom - xBorder
    132.  
    133.  nSrcWidth = Round(clientRect.Right / ZoomFactor, 0)
    134.  nSrcHeight = Round(clientRect.Bottom / ZoomFactor, 0)
    135.  
    136.  'align the center of windows
    137.  ScaleFactor = (ZoomFactor - 1) / (2 * ZoomFactor)
    138.  xLeft = wndRect.Left + xBorder + Round(clientRect.Right * ScaleFactor, 0)
    139.  ytop = wndRect.Top + yBorder + Round(clientRect.Bottom * ScaleFactor, 0)
    140.  
    141. 'make the magnified picture
    142.  
    143.  StretchBlt Me.hdc, 0, 0, clientRect.Right, clientRect.Bottom, hdcCompatible, xLeft, ytop, nSrcWidth, nSrcHeight, vbSrcCopy
    144.  Me.Refresh
    145.  
    146. End Sub
    147.  
    148. Private Sub Form_Load()
    149.  
    150.     Me.AutoRedraw = True
    151.     ScaleFactor = 2
    152.    
    153. ' Create a normal DC and a memory DC for the entire screen. The
    154. ' normal DC provides a "snapshot" of the screen contents. The
    155. ' memory DC keeps a copy of this "snapshot" in the associated
    156. ' bitmap.
    157.  
    158. hdcScreen = CreateDC("DISPLAY", vbNullString, vbNullString, 0&)
    159. If hdcScreen = 0 Then
    160.     MsgBox "CreateDC failed"
    161.     Exit Sub
    162. End If
    163.  
    164.  
    165. hdcCompatible = CreateCompatibleDC(hdcScreen)
    166. If hdcCompatible = 0 Then
    167.     MsgBox "hdcCompatible failed"
    168.     Exit Sub
    169. End If
    170.  
    171. ScreenWidth = GetDeviceCaps(hdcScreen, HORZRES)
    172. screenHeight = GetDeviceCaps(hdcScreen, VERTRES)
    173.  
    174. ' Create a compatible bitmap for hdcScreen.
    175. hbmScreen = CreateCompatibleBitmap(hdcScreen, _
    176.                      ScreenWidth, _
    177.                      screenHeight)
    178.  
    179. If (hbmScreen = 0) Then
    180.     MsgBox "hbmScreen failed"
    181.     Exit Sub
    182. End If
    183.  
    184. ' Select the bitmaps into the compatible DC.
    185. If SelectObject(hdcCompatible, hbmScreen) = 0 Then
    186.     MsgBox "Compatible Bitmap Selection Failed!"
    187.     Exit Sub
    188. End If
    189.  
    190.     UpDateImage
    191.     ZoomIn ScaleFactor
    192.    
    193.  
    194. End Sub
    195.  
    196. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    197.     Select Case Button
    198.     Case vbLeftButton
    199.         XX = X 'Me.ScaleX(X, vbTwips, vbPixels)
    200.         YY = Y 'Me.ScaleY(Y, vbTwips, vbPixels)
    201.         UpDateImage
    202.     Case vbRightButton
    203.         PopupMenu mnPopup
    204.     End Select
    205.    
    206. End Sub
    207.  
    208.  
    209. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    210. Dim NewLeft As Integer
    211. Dim NewTop As Integer
    212.     If Button = vbLeftButton Then
    213.         NewLeft = Me.Left - XX + X
    214.         NewTop = Me.Top - YY + Y
    215.         If NewLeft + Me.Width <= Screen.Width Then
    216.             If NewLeft >= 0 Then
    217.                 Me.Left = NewLeft
    218.             Else
    219.                 Me.Left = 0
    220.             End If
    221.         Else
    222.             Me.Left = Screen.Width - Me.Width
    223.         End If
    224.  
    225.         If NewTop + Me.Height <= Screen.Height Then
    226.             If NewTop >= 0 Then
    227.                 Me.Top = NewTop
    228.             Else
    229.                 Me.Top = 0
    230.             End If
    231.         Else
    232.             Me.Top = Screen.Height - Me.Height
    233.         End If
    234.  
    235.     ZoomIn ScaleFactor
    236.   End If
    237.  
    238.  
    239. End Sub
    240.  
    241. Private Sub Form_Unload(Cancel As Integer)
    242.     DeleteObject hbmScreen
    243.     DeleteDC hdcScreen
    244.     DeleteDC hdcCompatible
    245. End Sub
    246.  
    247. Private Sub mnExit_Click()
    248.     Unload Me
    249. End Sub
    250.  
    251. Private Sub mnFactor_Click(Index As Integer)
    252.     ScaleFactor = Index
    253.     ZoomIn ScaleFactor
    254. End Sub
    Last edited by moeur; Oct 29th, 2005 at 02:35 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width