Programación General > Visual Basic 6.0 e inferiores

 Agregar Nuevo Boton En Barra De Titulo

(1/1)

hunter:
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:
Hola, te paso una forma que me dijeron utilizando dos funciones mas, no es lo ideal pero funciona:

..saludos



--- Código: Text ---    '*********************   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 Private Type POINTAPI    X As Long    Y As LongEnd TypePrivate Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long   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   Const WM_MOVE = &H3 Const WM_SETCURSOR = &H20 Const WM_NCPAINT = &H85 Const WM_COMMAND = &H111   Const SWP_FRAMECHANGED = &H20 Const GWL_EXSTYLE = -20     Private WHook& Private ButtonHwnd As Long   Public Sub Init(MiaForm As Form, TestoPulsante As String)   MiaFormHandle = MiaForm.hwnd   ButtonHwnd& = CreateWindowEx(0&, "Button", TestoPulsante, &H40000000, 50, 50, 14, 14, MiaFormHandle, 0&, App.hInstance, 0&)   Call ShowWindow(ButtonHwnd&, 1)   WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID) Call SetWindowLong(ButtonHwnd&, GWL_EXSTYLE, &H80) Call SetParent(ButtonHwnd&, GetParent(MiaFormHandle)) End Sub   Public Sub Terminate()   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 = ButtonHwnd& Then             If Inf.Message = 533 Then            Dim pt As POINTAPI, mwnd As Long            GetCursorPos pt            mwnd = WindowFromPoint(pt.X, pt.Y)                If mwnd = Inf.hwnd Then                    Form1.cmdInTitlebar_Click                End If        End If   Debug.Print Inf.Message         If Inf.Message = WM_COMMAND Then                                         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               Call GetWindowRect(MiaFormHandle, FormRect)               Call SetWindowPos(ButtonHwnd&, 0, FormRect.Right - 75, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)         End If     End If   End Function    

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

Recursos visual basic, ocx, codigo fuente

Navegación

[0] Índice de Mensajes

Ir a la versión completa