Coding4ever’s Blog

Just coding… coding… and coding… because coding should be fun :)

Menambahkan Tombol Baru Di Title Bar

| Comments

Yaph sesuai judulnya kita akan menambahkan tombol baru di Title Bar, lihat gambar :

oke langsung saja tambahkan sebuah modul kemudian copy paste kode berikut :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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 As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) 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 Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long

Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion      As Long
    dwMinorVersion      As Long
    dwBuildNumber       As Long
    dwPlatformId        As Long
    szCSDVersion        As String * 128
End Type

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 Const WM_MOVE                       As Long = &H3
Private Const WM_SETCURSOR                  As Long = &H20
Private Const WM_NCPAINT                    As Long = &H85
Private Const WM_COMMAND                    As Long = &H111
Private Const BM_SETSTATE                   As Long = &HF3
Private Const SWP_FRAMECHANGED              As Long = &H20
Private Const WS_CHILD                      As Long = &H40000000
Private Const WS_VISIBLE                    As Long = &H10000000
Private Const WS_EX_TOOLWINDOW              As Long = &H80
Private Const VER_PLATFORM_WIN32_WINDOWS    As Long = 1
Private Const VER_PLATFORM_WIN32_NT         As Long = 2

Private MyForm                              As Form
Private frm                                 As Form
Private GiàIntercettato                     As Boolean
Private sysVar00_lOSVersion                 As Long
Private WHook                               As Long
Private ButtonHwnd                          As Long
Private lButtXPos                           As Long

Public Sub InitButton(frmObj As Form, Optional XPosition As Long = 75)
    Dim os As OSVERSIONINFO
    Dim retval As Long  ' return value

    os.dwOSVersionInfoSize = Len(os)  ' set the size of the structure
    retval = GetVersionEx(os)  ' read Windows's version information
    sysVar00_lOSVersion = os.dwPlatformId
    Set MyForm = frmObj
    GiàIntercettato = False

    Call UnHookButton

    Set frm = frmObj
    lButtXPos = XPosition

    ButtonHwnd = CreateWindowEx(WS_EX_TOOLWINDOW, "Button", "?", WS_CHILD + WS_VISIBLE, 50, 50, 14, 14, frmObj.hwnd, 0, App.hInstance, 0)

    WHook = SetWindowsHookEx(4, AddressOf HookProc, 0, App.ThreadID)

    Call SetParent(ButtonHwnd, GetParent(frmObj.hwnd))
End Sub

Public Sub UnHookButton()
    If lButtXPos > 0 Then

        lButtXPos = 0
        Call UnhookWindowsHookEx(WHook)
        Call DestroyWindow(ButtonHwnd)
    End If
End Sub

Private Function HookProc(ByVal ncode As Long, ByVal wParam As Long, Inf As CWPSTRUCT) As Long
    Dim FormRect As RECT
    Static LastParam As Long

    If Inf.hwnd = GetParent(ButtonHwnd) And sysVar00_lOSVersion = VER_PLATFORM_WIN32_WINDOWS Then
        If Inf.Message = WM_COMMAND Then
            Select Case LastParam

                Case ButtonHwnd
                    Call frm.btnNew_Click
            End Select
        ElseIf Inf.Message = WM_SETCURSOR Then
            LastParam = Inf.wParam
        End If

    ElseIf Inf.hwnd = frm.hwnd Then
        If Inf.Message = WM_NCPAINT Or Inf.Message = WM_MOVE Then

            Call GetWindowRect(frm.hwnd, FormRect)

            Call SetWindowPos(ButtonHwnd, 0, FormRect.Right - lButtXPos, FormRect.Top + 6, 17, 14, SWP_FRAMECHANGED)

        End If

    ElseIf Inf.hwnd = ButtonHwnd And sysVar00_lOSVersion = VER_PLATFORM_WIN32_NT Then
        If Inf.Message = BM_SETSTATE And Inf.wParam = 0 Then
            If GiàIntercettato = False Then
               GiàIntercettato = True
               Call MyForm.btnNew_Click
             Else
               GiàIntercettato = False
            End If
        End If
    End If

    HookProc = CallNextHookEx(WHook, ncode, wParam, Inf.lParam)
End Function

contoh penggunaan di form :

1
2
3
4
5
6
7
8
9
10
11
Private Sub Form_Load()
    Call InitButton(Me)
End Sub

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

Public Sub btnNew_Click()
    'TODO : tampilkan pesan atau form disini
End Sub

referensi :

  1. http://www.justvb.net/

  2. http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=58651&lngWId=1

Selamat mencoba and happy coding !!! :)

visual basic

Tentang Penulis

Software developer yang fokus mengembangkan aplikasi di atas platform .NET (Desktop, ASP.NET MVC, Web Service) dan Android. Senang mempelajari teknologi baru terutama di bidang OOP, Design Pattern, ORM, Database dan Continuous Integration & Deployment.
Saat ini bekerja sebagai staf IT di salah satu PTS di Yogyakarta sebagai senior software developer. Di waktu luang insya Alloh akan terus berbagi pengalaman di blog ini :)

« Membuat paket instalasi vb + mysql dengan NSIS Objek TreeView sebagai alternatif membuat menu »

Comments