• Domingo 22 de Diciembre de 2024, 14:18

Autor Tema:  Agregar Nuevo Boton En Barra De Titulo  (Leído 1005 veces)

hunter

  • Nuevo Miembro
  • *
  • Mensajes: 15
    • Ver Perfil
Agregar Nuevo Boton En Barra De Titulo
« en: Miércoles 15 de Noviembre de 2006, 16:16 »
0
Holas Gentita kiciera ke me ayuden a colocar un boton en la barra de titulo, bueno aca tengo el Script pero lo malo es ke cuando hago click en el nuevo boton no me desencadena ningun evento :S kiciera ke me ayuden a verlo...
Aca les dejo el code ke lo copie de una Web...

' Add in a module this code:

Option Explicit

'*********************
'* API Declarations  *
'*********************
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As Rect) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function 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) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook&) As Long
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

'*********************
'* Type Declarations *
'*********************
Private Type Rect
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type CWPSTRUCT
    lParam As Long
    wParam As Long
    Message As Long
    hwnd As Long
End Type

Private MiaFormHandle As Long

'*********************
'* Consts            *
'*********************
Const WM_MOVE = &H3
Const WM_SETCURSOR = &H20
Const WM_NCPAINT = &H85
Const WM_COMMAND = &H111

Const SWP_FRAMECHANGED = &H20
Const GWL_EXSTYLE = -20

'*********************
'* Vars              *
'*********************
Private WHook&
Private ButtonHwnd As Long

Public Sub Init(MiaForm As Form, TestoPulsante As String)
    ' Handle della form
    MiaFormHandle = MiaForm.hwnd
    'Create the button that is going to be placed in the Titlebar
    ButtonHwnd& = CreateWindowEx(0&, "Button", TestoPulsante, &H40000000, 50, 50, 14, 14, MiaFormHandle, 0&, App.hInstance, 0&)
    'Show the button cause it´s invisible
    Call ShowWindow(ButtonHwnd&, 1)
    'Initialize the window hooking for the button
    WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)
    Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80)
    Call SetParent(ButtonHwnd&, GetParent(MiaFormHandle))
End Sub

Public Sub Terminate()
    'Terminate the window hooking
    Call UnhookWindowsHookEx(WHook)
    Call SetParent(ButtonHwnd&, MiaFormHandle)
End Sub

Public Function HookProc&(ByVal nCode&, ByVal wParam&, Inf As CWPSTRUCT)
    Dim FormRect As Rect
    Static LastParam&
    If Inf.hwnd = GetParent(ButtonHwnd&) Then
        If Inf.Message = WM_COMMAND Then
            Select Case LastParam
                'If the LastParam is cmdInTitlebar call the Click-Procedure
                'of the button
                Case ButtonHwnd&: Call Screen.ActiveForm.cmdInTitlebar_Click
            End Select
        ElseIf Inf.Message = WM_SETCURSOR Then
            LastParam = Inf.wParam
        End If
        ElseIf Inf.hwnd = MiaFormHandle Then
        If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then
            'Get the size of the Form
            Call GetWindowRect(MiaFormHandle, FormRect)
            'Place the button int the Titlebar
            Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)
        End If
    End If
End Function

' Add in your form this code:

Private Sub Form_Load()
  Call Init(Me, "?")
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Call Terminate
End Sub

Public Sub cmdInTitlebar_Click()
  MsgBox "Ciao!", vbInformation
End Sub

Gracias xD!

luciano2

  • Miembro activo
  • **
  • Mensajes: 71
    • Ver Perfil
    • http://www.recursosvisualbasic.com.ar/
Re: Agregar Nuevo Boton En Barra De Titulo
« Respuesta #1 en: Lunes 20 de Noviembre de 2006, 00:28 »
0
Hola, te paso una forma que me dijeron utilizando dos funciones mas, no es lo ideal pero funciona:

..saludos


Código: Text
  1.  
  2.  
  3.  
  4.  
  5. '*********************
  6.  
  7.  
  8.  
  9. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  10.  
  11. Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As Rect) As Long
  12.  
  13. Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
  14.  
  15. Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
  16.  
  17. Private Declare Function 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) As Long
  18.  
  19. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&) As Long
  20.  
  21. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook&) As Long
  22.  
  23. Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
  24.  
  25. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
  26.  
  27. Private Type POINTAPI
  28.     X As Long
  29.     Y As Long
  30. End Type
  31. Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
  32. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  33.  
  34.  
  35.  
  36. Private Type Rect
  37.  
  38. Left As Long
  39.  
  40. Top As Long
  41.  
  42. Right As Long
  43.  
  44. Bottom As Long
  45.  
  46. End Type
  47.  
  48.  
  49.  
  50. Private Type CWPSTRUCT
  51.  
  52. lParam As Long
  53.  
  54. wParam As Long
  55.  
  56. Message As Long
  57.  
  58. hwnd As Long
  59.  
  60. End Type
  61.  
  62.  
  63.  
  64. Private MiaFormHandle As Long
  65.  
  66.  
  67.  
  68. Const WM_MOVE = &H3
  69.  
  70. Const WM_SETCURSOR = &H20
  71.  
  72. Const WM_NCPAINT = &H85
  73.  
  74. Const WM_COMMAND = &H111
  75.  
  76.  
  77.  
  78. Const SWP_FRAMECHANGED = &H20
  79.  
  80. Const GWL_EXSTYLE = -20
  81.  
  82.  
  83.  
  84.  
  85.  
  86. Private WHook&
  87.  
  88. Private ButtonHwnd As Long
  89.  
  90.  
  91.  
  92. Public Sub Init(MiaForm As Form, TestoPulsante As String)
  93.  
  94.  
  95.  
  96. MiaFormHandle = MiaForm.hwnd
  97.  
  98.  
  99.  
  100. ButtonHwnd& = CreateWindowEx(0&, "Button", TestoPulsante, &H40000000, 50, 50, 14, 14, MiaFormHandle, 0&, App.hInstance, 0&)
  101.  
  102.  
  103.  
  104. Call ShowWindow(ButtonHwnd&, 1)
  105.  
  106.  
  107.  
  108. WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)
  109.  
  110. Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80)
  111.  
  112. Call SetParent(ButtonHwnd&, GetParent(MiaFormHandle))
  113.  
  114. End Sub
  115.  
  116.  
  117.  
  118. Public Sub Terminate()
  119.  
  120.  
  121.  
  122. Call UnhookWindowsHookEx(WHook)
  123.  
  124. Call SetParent(ButtonHwnd&, MiaFormHandle)
  125.  
  126. End Sub
  127.  
  128.  
  129.  
  130. Public Function HookProc&(ByVal nCode&, ByVal wParam&, Inf As CWPSTRUCT)
  131.  
  132.  
  133.  
  134. Dim FormRect As Rect
  135.  
  136.     Static LastParam&
  137.    
  138.  
  139.     If Inf.hwnd = ButtonHwnd& Then
  140.    
  141.  
  142.         If Inf.Message = 533 Then
  143.             Dim pt As POINTAPI, mwnd As Long
  144.             GetCursorPos pt
  145.             mwnd = WindowFromPoint(pt.X, pt.Y)
  146.                 If mwnd = Inf.hwnd Then
  147.                     Form1.cmdInTitlebar_Click
  148.                 End If
  149.         End If
  150.    Debug.Print Inf.Message
  151.  
  152.         If Inf.Message = WM_COMMAND Then
  153.  
  154.            
  155.  
  156.          
  157.  
  158.        
  159.  
  160.         ElseIf Inf.Message = WM_SETCURSOR Then
  161.  
  162.             LastParam = Inf.wParam
  163.  
  164.         End If
  165.  
  166.     ElseIf Inf.hwnd = MiaFormHandle Then
  167.  
  168.         If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then
  169.  
  170.  
  171.  
  172.             Call GetWindowRect(MiaFormHandle, FormRect)
  173.  
  174.  
  175.  
  176.             Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)
  177.  
  178.         End If
  179.  
  180.     End If
  181.  
  182.  
  183.  
  184. End Function
  185.  
  186.  
  187.  
  188.  


--------------------------------------

Recursos visual basic, ocx, codigo fuente