Перекинуть Label из окна в контейнер пользовательского контрола - VB

Узнай цену своей работы

Формулировка задачи:

Добрый день сейчас самостоятельно разрабатываю компонент Спойлер.. исходники здесь модификация здесь:
Листинг программы
  1. Option Explicit
  2. '
  3. ' © Антихакер32
  4. '
  5. Private Const def_cm1_Height = 300 'Высота кнопки
  6. Private Const HWND_TOPMOST = -1
  7. Private Const SWP_NOSIZE = &H1
  8. Private Const SWP_NOMOVE = &H2
  9. Private Const GWL_EXSTYLE = (-20)
  10. Private Const WS_EX_TOOLWINDOW = 128
  11. '
  12. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  13. Private Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
  14. Private Declare Function GetForegroundWindow Lib "user32" () As Long
  15. '
  16. Dim WithEvents cm1 As CommandButton
  17. Dim WithEvents Pic As PictureBox
  18. Dim Amb As Object
  19. Dim mSpoilerAction As Boolean
  20. '''''Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  21. '''''Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
  22. '''''Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
  23. '''''Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
  24. Public Sub WActivate()
  25. On Error GoTo errr
  26. SpoilerAction = False
  27. errr:
  28. End Sub
  29. Public Sub WindowPosChaning()
  30. On Error GoTo errr
  31. Const ram = 60
  32. Static Left&, top&
  33. Left = Parent.Left - (Parent.ScaleWidth - Parent.Width + ram) + Amb.Left
  34. top = Parent.top - (Parent.ScaleHeight - Parent.Height + ram) + Amb.top + cm1.Height
  35. Pic.Move Left, top, Amb.Width, Amb.Height - cm1.Height
  36. errr:
  37. End Sub
  38. Private Property Get SpoilerAction() As Boolean
  39. SpoilerAction = mSpoilerAction
  40. End Property
  41. Private Property Let SpoilerAction(ByVal vNewValue As Boolean)
  42. mSpoilerAction = vNewValue
  43. If vNewValue Then
  44. SetWindowPos Pic.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
  45. Pic.Visible = True
  46. Else
  47. SetWindowPos Pic.hWnd, 0, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
  48. Pic.Visible = False
  49. End If
  50. End Property
  51. Private Sub cm1_Click()
  52. SpoilerAction = Not SpoilerAction
  53. End Sub
  54. Private Sub UserControl_ExitFocus()
  55. SpoilerAction = False
  56. End Sub
  57. Private Sub UserControl_Initialize()
  58. Set cm1 = Controls.Add("vb.CommandButton", "cm1_" & hWnd)
  59. cm1.Visible = 1
  60. Set Pic = Controls.Add("vb.PictureBox", "pic_" & hWnd)
  61. End Sub
  62.  
  63. Private Sub UserControl_Resize()
  64. On Error Resume Next
  65. cm1.Move 0, 0, Width, def_cm1_Height
  66. Pic.Move 0, cm1.Height, Width, Height - cm1.Height
  67. End Sub
  68. Private Sub UserControl_Show()
  69. Dim j$(), id$, indAmb&, o As Object, hW&, py&
  70. j = Split(Ambient.DisplayName, "(")
  71. If UBound(j) Then
  72. indAmb = Val(Split(j(1), ")")(0))
  73. Set Amb = CallByName(Parent, j(0), VbGet, indAmb)
  74. Else: indAmb = -1
  75. Set Amb = CallByName(Parent, j(0), VbGet)
  76. End If
  77. On Error Resume Next
  78. If Ambient.UserMode Then
  79. For Each o In Parent
  80. id = o.Container.Name 'Если произойдет ошибка, то останеться это id
  81. id = id & "(" & o.Container.Index & ")"
  82. If id = Ambient.DisplayName Then
  83. hW = 0: hW = o.hWnd
  84. If hW = 0 Then
  85. '
  86. 'Здесь лейбл, значит надо напечатать на PictureBox эту надпись :(
  87. '
  88. Else: SetParent hW, Pic.hWnd
  89. End If
  90. o.top = o.top - def_cm1_Height
  91. End If
  92. Next
  93. SetParent Pic.hWnd, 0
  94. SetWindowLong Pic.hWnd, GWL_EXSTYLE, _
  95. GetWindowLong(Pic.hWnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW
  96. WindowPosChaning
  97. Hook Amb
  98. Else 'Режим проектирования
  99. SetParent Pic.hWnd, hWnd
  100. Pic.Visible = True: Pic.Enabled = 0
  101. Pic.ZOrder 1
  102. End If
  103. End Sub
  104. Private Sub UserControl_Terminate()
  105. UnHook
  106. End Sub
Листинг программы
  1. Option Explicit
  2. '
  3. Public Const GWL_WNDPROC = -4
  4. ' Сообщения windows
  5. Public Const WM_CLOSE = &H10
  6. Private Const WM_MOVE = &H3
  7. Const WM_ACTIVATE = &H6
  8. Const HTCAPTION = 2
  9. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  10. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  11. Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
  12. '
  13. Dim SWL As Long
  14. Dim mTypeName As String
  15. Dim mParent As Object
  16. Dim mParent_hWnd As Long
  17.  
  18. Function Hook(Child As Object) As Long
  19. On Error GoTo errr
  20. If Child.Parent.hWnd <> mParent_hWnd Then
  21. mTypeName = TypeName(Child)
  22. Set mParent = Child.Parent
  23. mParent_hWnd = mParent.hWnd
  24. SWL = SetWindowLong(mParent_hWnd, GWL_WNDPROC, AddressOf WindowProc)
  25. End If
  26. errr:
  27. End Function
  28. Sub UnHook()
  29. Call CallWindowProc(SWL, mParent_hWnd, WM_CLOSE, 0, 0)
  30. End Sub
  31. Public Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  32. Dim o As Object
  33. On Error GoTo errr
  34. Select Case Msg
  35. Case WM_ACTIVATE
  36. For Each o In mParent
  37. If TypeName(o) = mTypeName Then
  38. Call o.WActivate
  39. End If
  40. Next
  41. Exit Function
  42. Case WM_MOVE
  43. For Each o In mParent
  44. If TypeName(o) = mTypeName Then
  45. Call o.WindowPosChaning
  46. End If
  47. Next
  48. Exit Function
  49. End Select
  50. WindowProc = CallWindowProc(SWL, hWnd, Msg, wParam, lParam)
  51. errr:
  52. End Function
так вот, есть ли нормальный способ чтоб перекинуть лейбл из окна в контейнер пользовательского контрола
Там проблемное место, это 101-я строчка, в модуле UserControl'а ..Какие будут предложения коллеги ?
вот как я придумал пока сделать, но это решение меня мало радует
Листинг программы
  1. Private Sub UserControl_Show()
  2. Dim j$(), id$, indAmb&, o As Object, hW&, hP&
  3. Dim lab As Label, col As Collection, f&, v As Object
  4. j = Split(Ambient.DisplayName, "(")
  5. If UBound(j) Then
  6. indAmb = Val(Split(j(1), ")")(0))
  7. Set Amb = CallByName(Parent, j(0), VbGet, indAmb)
  8. Else: indAmb = -1
  9. Set Amb = CallByName(Parent, j(0), VbGet)
  10. End If
  11. On Error Resume Next
  12. If Ambient.UserMode Then
  13. For Each o In Parent
  14. id = o.Container.Name 'Если произойдет ошибка, то останеться это id
  15. id = id & "(" & o.Container.Index & ")"
  16. If id = Ambient.DisplayName Then
  17. hW = 0: hW = o.hWnd
  18. hP = GetParent(hW)
  19. If hW = 0 Then
  20. '
  21. 'Здесь лейбл, значит
  22. '
  23. Set lab = Controls.Add("vb." & TypeName(o), "lab_" & hWnd, Pic)
  24. With lab
  25. .Alignment = o.Appearance
  26. .Appearance = o.Appearance
  27. .AutoSize = o.AutoSize
  28. .BackColor = o.BackColor
  29. .BorderStyle = o.BorderStyle
  30. .Left = o.Left
  31. .top = o.top
  32. .Width = o.Width
  33. .Height = o.Height
  34. .Font = o.Font
  35. .Caption = o.Caption
  36. o.Visible = False
  37. .Visible = True
  38. End With
  39. ElseIf hP = hWnd Then
  40. SetParent hW, Pic.hWnd
  41. End If
  42. o.top = o.top - def_cm1_Height
  43. End If
  44. Next
  45. SetParent Pic.hWnd, 0
  46. SetWindowLong Pic.hWnd, GWL_EXSTYLE, _
  47. GetWindowLong(Pic.hWnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW
  48. WindowPosChaning
  49. Hook Amb
  50. Else 'Режим проектирования
  51. SetParent Pic.hWnd, hWnd
  52. ' Pic.Visible = True: Pic.Enabled = 0
  53. Pic.ZOrder 1
  54. End If
  55. End Sub

Решение задачи: «Перекинуть Label из окна в контейнер пользовательского контрола»

textual
Листинг программы
  1. Option Explicit
  2. '
  3. '   © Антихакер32
  4. '
  5. '
  6. Private Type RECT
  7.         Left As Long
  8.         Top As Long
  9.         Right As Long
  10.         Bottom As Long
  11. End Type
  12. '
  13. Public Enum FlagsSpoilerStyle
  14.     [По умолчанию] = 0
  15.     [Кнопка слева] = 1
  16. End Enum
  17. '
  18. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  19. Private Declare Sub SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)
  20. Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
  21. Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
  22. Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
  23. Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
  24. '
  25. 'Default Property Values:
  26. Const m_def_Style = 0
  27. Const def_Exp = 280 'Высота кнопки
  28. Const HWND_TOPMOST = -1
  29. Const SWP_NOSIZE = &H1
  30. Const SWP_NOMOVE = &H2
  31. Const GWL_EXSTYLE = (-20)
  32. Const WS_EX_TOOLWINDOW = 128
  33. 'Property Variables:
  34. Dim m_Style As FlagsSpoilerStyle
  35. Dim WithEvents cm1 As CommandButton
  36. Dim sh1 As Shape
  37. Dim mSpoilerAction As Boolean
  38. Dim oldParRect As RECT, mRect As RECT
  39. Dim ResizeAction As Boolean
  40.  
  41. Public Sub WActivate()
  42.     On Error GoTo errr
  43.     If SpoilerAction Then SpoilerAction = False
  44. errr:
  45. End Sub
  46.  
  47. Public Sub WMove()
  48.     Static Rect1 As RECT, Rect2 As RECT
  49.     On Error GoTo errr
  50.     If mSpoilerAction Then
  51.         GetWindowRect Parent.hWnd, Rect1
  52.         GetWindowRect hWnd, Rect2
  53.         With Rect2
  54.             .Left = .Left + (Rect1.Left - oldParRect.Left)
  55.             .Top = .Top + (Rect1.Top - oldParRect.Top)
  56.             SetWindowPos hWnd, -1, .Left, .Top, 0, 0, SWP_NOSIZE
  57.         End With
  58.         oldParRect = Rect1
  59.     End If
  60. errr:
  61. End Sub
  62.  
  63. Private Property Get SpoilerAction() As Boolean
  64.     SpoilerAction = mSpoilerAction
  65. End Property
  66.  
  67.  
  68. Private Property Let SpoilerAction(ByVal vNewValue As Boolean)
  69.     Static Rect1 As RECT, Rect2 As RECT, Rect3 As RECT, Border&
  70.     On Error GoTo errr
  71.     GetWindowRect Parent.hWnd, Rect1
  72.     GetWindowRect hWnd, Rect2
  73.     ResizeAction = True
  74.     If vNewValue Then
  75.         With Rect2
  76.             .Left = Rect1.Left + (.Left - Rect1.Left)
  77.             .Top = Rect1.Top + (.Top - Rect1.Top)
  78.             SetWindowPos hWnd, 0, .Left, .Top, 0, 0, SWP_NOSIZE
  79.             SetParent hWnd, 0
  80.             oldParRect = Rect1
  81.         End With
  82.         Select Case m_Style
  83.         Case [По умолчанию]: Height = sh1.Height
  84.         Case [Кнопка слева]: Width = sh1.Width
  85.         End Select
  86.     Else
  87.         GetClientRect Parent.hWnd, Rect3
  88.         With Rect2
  89.             .Left = (.Left - (Rect1.Right - Rect3.Right))
  90.             .Left = .Left + (mRect.Left - .Left)
  91.             .Top = (.Top - (Rect1.Bottom - Rect3.Bottom))
  92.             .Top = .Top + (mRect.Top - .Top)
  93.             SetParent hWnd, Parent.hWnd
  94.             SetWindowPos hWnd, 0, .Left, .Top, 0, 0, SWP_NOSIZE
  95.         End With
  96.         Select Case m_Style
  97.         Case [По умолчанию]: Height = def_Exp
  98.         Case [Кнопка слева]: Width = def_Exp
  99.         End Select
  100.     End If
  101.     mSpoilerAction = vNewValue
  102. errr:
  103.     ResizeAction = False
  104. End Property
  105.  
  106. Private Sub cm1_Click()
  107.     SpoilerAction = Not SpoilerAction
  108. End Sub
  109.  
  110. Private Sub UserControl_Initialize()
  111.     Set cm1 = Controls.Add("vb.CommandButton", "cm1_" & hWnd)
  112.     Set sh1 = Controls.Add("vb.Shape", "sh1_" & hWnd)
  113.     sh1.Visible = 1: cm1.Visible = 1
  114.     SetWindowLong hWnd, GWL_EXSTYLE, _
  115.     GetWindowLong(hWnd, GWL_EXSTYLE) Or WS_EX_TOOLWINDOW
  116. End Sub
  117.  
  118. Private Sub UserControl_Resize()
  119.     On Error Resume Next
  120.     If ResizeAction Then Exit Sub
  121.     With mRect
  122.         If Not mSpoilerAction Then
  123.             sh1.Move 0, 0, Width, Height
  124.             .Left = Extender.Left \ Screen.TwipsPerPixelX
  125.             .Top = Extender.Top \ Screen.TwipsPerPixelY
  126.         End If
  127.         Select Case m_Style
  128.         Case [По умолчанию]: cm1.Move 0, 0, sh1.Width, def_Exp
  129.         Case [Кнопка слева]: cm1.Move 0, 0, def_Exp, sh1.Height
  130.         End Select
  131.     End With
  132. End Sub
  133.  
  134. Private Sub UserControl_Show()
  135.     Dim o As Object
  136.     If Ambient.UserMode Then
  137.         SpoilerAction = 1
  138.         Hook Extender
  139.     End If
  140. End Sub
  141.  
  142. Private Sub UserControl_Terminate()
  143.      UnHook
  144. End Sub
  145.  
  146. 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
  147. 'MemberInfo=8,0,0,0
  148. Public Property Get Style() As FlagsSpoilerStyle
  149.     Style = m_Style
  150. End Property
  151.  
  152. Public Property Let Style(ByVal New_Style As FlagsSpoilerStyle)
  153.     m_Style = New_Style
  154.     PropertyChanged "Style"
  155.     UserControl_Resize
  156. End Property
  157.  
  158. 'Initialize Properties for User Control
  159. Private Sub UserControl_InitProperties()
  160.     m_Style = m_def_Style
  161. End Sub
  162.  
  163. 'Load property values from storage
  164. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  165.     m_Style = PropBag.ReadProperty("Style", m_def_Style)
  166.     UserControl_Resize
  167. End Sub
  168.  
  169. 'Write property values to storage
  170. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  171.  
  172.     Call PropBag.WriteProperty("Style", m_Style, m_def_Style)
  173. End Sub

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.375 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы