home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / acodet1a / formdrag.ctl (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-06-25  |  12.8 KB  |  280 lines

  1. VERSION 5.00
  2. Begin VB.UserControl FormDragger 
  3.    Alignable       =   -1  'True
  4.    Appearance      =   0  'Flat
  5.    CanGetFocus     =   0   'False
  6.    ClientHeight    =   3600
  7.    ClientLeft      =   0
  8.    ClientTop       =   0
  9.    ClientWidth     =   4800
  10.    ScaleHeight     =   3600
  11.    ScaleWidth      =   4800
  12. Attribute VB_Name = "FormDragger"
  13. Attribute VB_GlobalNameSpace = False
  14. Attribute VB_Creatable = True
  15. Attribute VB_PredeclaredId = False
  16. Attribute VB_Exposed = False
  17. 'API Types
  18. Private Type RECT
  19.     Left As Long
  20.     Top As Long
  21.     Right As Long
  22.     Bottom As Long
  23. End Type
  24. Private Type POINTAPI
  25.         X As Long
  26.         Y As Long
  27. End Type
  28. 'API Constants
  29. Private Const BDR_SUNKENINNER = &H8
  30. Private Const BF_LEFT As Long = &H1
  31. Private Const BF_TOP As Long = &H2
  32. Private Const BF_RIGHT As Long = &H4
  33. Private Const BF_BOTTOM As Long = &H8
  34. Private Const BF_RECT As Long = (BF_LEFT Or BF_TOP Or BF_RIGHT Or BF_BOTTOM)
  35. Private Const BDR_RAISED = &H5
  36. Private Const GWL_EXSTYLE = (-20)
  37. Private Const WS_EX_TOOLWINDOW = &H80
  38. Private Const VK_LBUTTON = &H1
  39. Private Const PS_SOLID = 0
  40. Private Const R2_NOTXORPEN = 10
  41. Private Const BLACK_PEN = 7
  42. Private Const SM_CYCAPTION = 4
  43. 'API Declares
  44. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
  45. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  46. Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
  47. Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As Long, ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
  48. Private Declare Function SetROP2 Lib "gdi32" (ByVal hdc As Long, ByVal nDrawMode As Long) As Long
  49. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  50. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  51. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
  52. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  53. Private Declare Function GetCapture Lib "user32" () As Long
  54. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
  55. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
  56. Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
  57. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
  58. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  59. Private Declare Function DrawEdge Lib "user32" (ByVal hdc As Long, qrc As RECT, ByVal edge As Long, ByVal grfFlags As Long) As Long
  60. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
  61. Private Declare Function ReleaseCapture Lib "user32" () As Long
  62. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As Any) As Long
  63. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  64. Private Declare Function SetParent Lib "user32" (ByVal HwndChild As Long, ByVal hWndNewParent As Long) As Long
  65. Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
  66. Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
  67. Private Declare Function GetActiveWindow Lib "user32" () As Long
  68. 'Event Declarations:
  69. Event Click() 'MappingInfo=UserControl,UserControl,-1,Click
  70. Attribute Click.VB_Description = "Occurs when the user presses and then releases a mouse button over an object."
  71. Attribute Click.VB_MemberFlags = "200"
  72. Event DblClick() 'MappingInfo=UserControl,UserControl,-1,DblClick
  73. Attribute DblClick.VB_Description = "Occurs when the user presses and releases a mouse button and then presses and releases it again over an object."
  74. Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=UserControl,UserControl,-1,MouseDown
  75. Attribute MouseDown.VB_Description = "Occurs when the user presses the mouse button while an object has the focus."
  76. Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=UserControl,UserControl,-1,MouseMove
  77. Attribute MouseMove.VB_Description = "Occurs when the user moves the mouse."
  78. Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=UserControl,UserControl,-1,MouseUp
  79. Attribute MouseUp.VB_Description = "Occurs when the user releases the mouse button while an object has the focus."
  80. Event FormDropped(FormLeft As Long, FormTop As Long, FormWidth As Long, FormHeight As Long)
  81. Event FormMoved(FormLeft As Long, FormTop As Long, FormWidth As Long, FormHeight As Long)
  82. 'Default Property Values:
  83. Const m_def_RepositionForm = True
  84. Const m_def_Caption = ""
  85. 'Property Variables:
  86. Dim m_RepositionForm As Boolean
  87. Dim m_Caption As String
  88. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  89.     Dim na As Long
  90.     Dim pt As POINTAPI
  91.     Dim frmHwnd As Long
  92.     UserControl_Paint
  93.     frmHwnd = UserControl.Extender.Parent.hwnd
  94.     'start 'dragging' the form
  95.     If Button = vbLeftButton And X >= 0 And X <= UserControl.ScaleWidth And Y >= 0 And Y <= UserControl.ScaleHeight Then
  96.         ReleaseCapture
  97.         DragObject frmHwnd
  98.     End If
  99.     RaiseEvent MouseDown(Button, Shift, X, Y)
  100. End Sub
  101. Private Sub DragObject(ByVal hwnd As Long)
  102.     'Procedure which simulates windows dragging of an object.
  103.     Dim pt As POINTAPI
  104.     Dim ptPrev As POINTAPI
  105.     Dim objRect As RECT
  106.     Dim DragRect As RECT
  107.     Dim na As Long
  108.     Dim lBorderWidth As Long
  109.     Dim lObjWidth As Long
  110.     Dim lObjHeight As Long
  111.     Dim lXOffset As Long
  112.     Dim lYOffset As Long
  113.     Dim bMoved As Boolean
  114.     ReleaseCapture
  115.     GetWindowRect hwnd, objRect
  116.     lObjWidth = objRect.Right - objRect.Left
  117.     lObjHeight = objRect.Bottom - objRect.Top
  118.     GetCursorPos pt
  119.     'Store the initial cursor position
  120.     ptPrev.X = pt.X
  121.     ptPrev.Y = pt.Y
  122.     'Set the initial rectangle, and draw it to show the user that
  123.     'the object can be moved
  124.     lXOffset = pt.X - objRect.Left
  125.     lYOffset = pt.Y - objRect.Top
  126.     With DragRect
  127.         .Left = pt.X - lXOffset
  128.         .Top = pt.Y - lYOffset
  129.         .Right = .Left + lObjWidth
  130.         .Bottom = .Top + lObjHeight
  131.     End With
  132.     'use form border width highlighting
  133.     lBorderWidth = 3
  134.     DrawDragRectangle DragRect.Left, DragRect.Top, DragRect.Right, DragRect.Bottom, lBorderWidth
  135.     'Move the object
  136.     Do While GetKeyState(VK_LBUTTON) < 0
  137.         GetCursorPos pt
  138.         If pt.X <> ptPrev.X Or pt.Y <> ptPrev.Y Then
  139.             ptPrev.X = pt.X
  140.             ptPrev.Y = pt.Y
  141.             'erase the previous drag rectangle if any
  142.             DrawDragRectangle DragRect.Left, DragRect.Top, DragRect.Right, DragRect.Bottom, lBorderWidth
  143.             'Tell the user we've moved
  144.             RaiseEvent FormMoved(pt.X - lXOffset, pt.Y - lYOffset, lObjWidth, lObjHeight)
  145.             'Adjust the height/width
  146.             With DragRect
  147.                 .Left = pt.X - lXOffset
  148.                 .Top = pt.Y - lYOffset
  149.                 .Right = .Left + lObjWidth
  150.                 .Bottom = .Top + lObjHeight
  151.             End With
  152.             DrawDragRectangle DragRect.Left, DragRect.Top, DragRect.Right, DragRect.Bottom, lBorderWidth
  153.             bMoved = True
  154.         End If
  155.         DoEvents
  156.     Loop
  157.     'erase the previous drag rectangle if any
  158.     DrawDragRectangle DragRect.Left, DragRect.Top, DragRect.Right, DragRect.Bottom, lBorderWidth
  159.     'move and repaint the window
  160.     If bMoved Then
  161.         If m_RepositionForm Then
  162.             MoveWindow hwnd, DragRect.Left, DragRect.Top, DragRect.Right - DragRect.Left, DragRect.Bottom - DragRect.Top, True
  163.         End If
  164.         'tell the user we've dropped the form
  165.         RaiseEvent FormDropped(DragRect.Left, DragRect.Top, DragRect.Right - DragRect.Left, DragRect.Bottom - DragRect.Top)
  166.     End If
  167. End Sub
  168. Private Sub DrawDragRectangle(ByVal X As Long, ByVal Y As Long, ByVal x1 As Long, ByVal y1 As Long, ByVal lWidth As Long)
  169.     'Draw a rectangle using the Win32 API
  170.     Dim hdc As Long
  171.     Dim hPen As Long
  172.     hPen = CreatePen(PS_SOLID, lWidth, &HE0E0E0)
  173.     hdc = GetDC(0)
  174.     Call SelectObject(hdc, hPen)
  175.     Call SetROP2(hdc, R2_NOTXORPEN)
  176.     Call Rectangle(hdc, X, Y, x1, y1)
  177.     Call SelectObject(hdc, GetStockObject(BLACK_PEN))
  178.     Call DeleteObject(hPen)
  179.     Call SelectObject(hdc, hPen)
  180.     Call ReleaseDC(0, hdc)
  181. End Sub
  182. 'Initialize Properties for User Control
  183. Private Sub UserControl_InitProperties()
  184.     m_Caption = m_def_Caption
  185.     m_Caption = m_def_Caption
  186.     m_RepositionForm = m_def_RepositionForm
  187. End Sub
  188. Private Sub UserControl_Paint()
  189.     Dim lBackColor As Long
  190.     Dim sCaption As String
  191.     'size, position, print caption etc.
  192.     With UserControl
  193.         .Cls
  194.         .Extender.Align = vbAlignTop
  195.         .Extender.Top = 0
  196.         .Height = GetSystemMetrics(SM_CYCAPTION) * Screen.TwipsPerPixelY
  197.         'draw the caption
  198.         If GetActiveWindow = UserControl.Extender.Parent.hwnd Then
  199.             .ForeColor = vbTitleBarText
  200.             lBackColor = vbActiveTitleBar
  201.         Else
  202.             .ForeColor = vbInactiveTitleBarText
  203.             lBackColor = vbInactiveTitleBar
  204.         End If
  205.         UserControl.Line (Screen.TwipsPerPixelX, Screen.TwipsPerPixelY)-(UserControl.ScaleWidth - (2 * Screen.TwipsPerPixelX), UserControl.ScaleHeight - Screen.TwipsPerPixelY), lBackColor, BF
  206.         .CurrentX = 4 * Screen.TwipsPerPixelX
  207.         .CurrentY = 3 * Screen.TwipsPerPixelY
  208.         .Font.Name = "MS Sans Serif"
  209.         .Font.Bold = True
  210.         'Check width
  211.         sCaption = m_Caption
  212.         If UserControl.TextWidth(sCaption) > (UserControl.ScaleWidth - (4 * Screen.TwipsPerPixelX)) Then
  213.             Do While UserControl.TextWidth(sCaption & "...") > (UserControl.ScaleWidth - (4 * Screen.TwipsPerPixelX)) And Len(sCaption) > 0
  214.                 sCaption = Trim$(Left$(sCaption, Len(sCaption) - 1))
  215.             Loop
  216.             sCaption = sCaption & "..."
  217.         End If
  218.         UserControl.Print sCaption;
  219.     End With
  220. End Sub
  221. 'Load property values from storage
  222. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  223.     m_Caption = PropBag.ReadProperty("Caption", m_def_Caption)
  224.     m_RepositionForm = PropBag.ReadProperty("RepositionForm", m_def_RepositionForm)
  225.     UserControl_Paint
  226. End Sub
  227. Private Sub UserControl_Resize()
  228.     UserControl_Paint
  229. End Sub
  230. 'Write property values to storage
  231. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  232.     Call PropBag.WriteProperty("Caption", m_Caption, m_def_Caption)
  233.     Call PropBag.WriteProperty("RepositionForm", m_RepositionForm, m_def_RepositionForm)
  234. End Sub
  235. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  236. 'MemberInfo=13,0,0,0
  237. Public Property Get Caption() As String
  238. Attribute Caption.VB_Description = "Sets/Returns the caption of the control."
  239.     Caption = m_Caption
  240. End Property
  241. Public Property Let Caption(ByVal New_Caption As String)
  242.     m_Caption = New_Caption
  243.     PropertyChanged "Caption"
  244.     UserControl_Paint
  245. End Property
  246. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  247. 'MemberInfo=0,0,0,true
  248. Public Property Get RepositionForm() As Boolean
  249. Attribute RepositionForm.VB_Description = "Specifies whether the control should move the form to it's new location."
  250.     RepositionForm = m_RepositionForm
  251. End Property
  252. Public Property Let RepositionForm(ByVal New_RepositionForm As Boolean)
  253.     m_RepositionForm = New_RepositionForm
  254.     PropertyChanged "RepositionForm"
  255. End Property
  256. Private Sub UserControl_Click()
  257.     RaiseEvent Click
  258. End Sub
  259. Private Sub UserControl_DblClick()
  260.     RaiseEvent DblClick
  261. End Sub
  262. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  263. 'MappingInfo=UserControl,UserControl,-1,hWnd
  264. Public Property Get hwnd() As Long
  265. Attribute hwnd.VB_Description = "Returns a handle (from Microsoft Windows) to an object's window."
  266.     hwnd = UserControl.hwnd
  267. End Property
  268. Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  269.     RaiseEvent MouseMove(Button, Shift, X, Y)
  270. End Sub
  271. Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  272.     RaiseEvent MouseUp(Button, Shift, X, Y)
  273. End Sub
  274. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  275. 'MappingInfo=UserControl,UserControl,-1,Refresh
  276. Public Sub Refresh()
  277. Attribute Refresh.VB_Description = "Forces a complete repaint of a object."
  278.     UserControl.Refresh
  279. End Sub
  280.