Excelde Sağ Klik Açılan Menü

Öncelikle aşağıda ki “hwnd” fonksiyonlarını kodlarına eklemeniz icap eder.


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 GetModuleHandle Lib "kernel32" Alias _
"GetModuleHandleA" (ByVal lpModuleName As String) 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 SendDlgItemMessage Lib "user32" Alias "SendDlgItemMessageA" _
(ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long

'~~> Constants to be used in our API functions
Private Const EM_SETPASSWORDCHAR = &HCC
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
Private Const HC_ACTION = 0

Private hHook As Long

Public Function NewProc(ByVal lngCode As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim RetVal
Dim strClassName As String, lngBuffer As Long

If lngCode < HC_ACTION Then
NewProc = CallNextHookEx(hHook, lngCode, wParam, lParam)
Exit Function
End If

strClassName = String$(256, " ")
lngBuffer = 255

If lngCode = HCBT_ACTIVATE Then
RetVal = GetClassName(wParam, strClassName, lngBuffer)
'~~> Class name of the Inputbox
If Left$(strClassName, RetVal) = "#32770" Then
'~~> This changes the edit control so that it display the password character *.
'~~> You can change the Asc("*") as you please.
SendDlgItemMessage wParam, &H1324, EM_SETPASSWORDCHAR, Asc("*"), &H0
End If
End If

'~~> This line will ensure that any other hooks that may be in place are
'~~> called correctly.
CallNextHookEx hHook, lngCode, wParam, lParam

End Function

Public Function InputBoxDK(Prompt, Optional Title, Optional Default, Optional XPos, _
Optional YPos, Optional HelpFile, Optional Context) As String
Dim lngModHwnd As Long, lngThreadID As Long
lngThreadID = GetCurrentThreadId
lngModHwnd = GetModuleHandle(vbNullString)
hHook = SetWindowsHookEx(WH_CBT, AddressOf NewProc, lngModHwnd, lngThreadID)
InputBoxDK = InputBox(Prompt, Title, Default, XPos, YPos, HelpFile, Context)
UnhookWindowsHookEx hHook
End Function

 

Sonrasında “UserForm_Initialize” olayına şu kodu yazmanız gerekir.


Private Sub UserForm_Initialize()
hwnd = FindWindow(vbNullString, Me.Caption)
End Sub

 

Sonrasında UserFormunuzun kodları içerisine aşağıda ki kodları ilave etmeniz de gerekiyor.


Private Type POINTAPI
x As Long
y As Long
End Type
'
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function TrackPopupMenuEx Lib "user32" _
(ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, _
ByVal hwnd As Long, ByVal lptpm As Any) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
(ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, _
ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'
Const MF_CHECKED = &H8&
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Const TPM_RETURNCMD = &H100&
Const TPM_RIGHTBUTTON = &H2&
'
Dim hMenu As Long
Dim hwnd As Long

 

Sonrasında sağ klik yapmak istediğiniz userform kontrol nesnenizin “MouseUp” olayına aşağıda ki kodları yazın. (Benim uygulamamda “lsthm” adlı listbox’a sağ klik yaptım.)


Private Sub lsthm_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)

For i = 1 To lsthm.ListCount
If lsthm.Selected(i) = True Then

Dim Pt As POINTAPI
Dim ret As Long
If Button = 2 Then
hMenu = CreatePopupMenu()
AppendMenu hMenu, MF_STRING, 1, "Kontrol Edildi Olarak Ata"
' AppendMenu hMenu, MF_STRING, 2, "Menu - 2"
' AppendMenu hMenu, MF_SEPARATOR, 3, ByVal 0&
' AppendMenu hMenu, MF_STRING, 4, "About"
GetCursorPos Pt
ret = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or TPM_RIGHTBUTTON, Pt.x, Pt.y, hwnd, ByVal 0&)
DestroyMenu hMenu


Select Case ret
Case 1
Call MenuProc1
Case 2
Call MenuProc2
Case 4
Call MenuProc3
End Select
End If

End If
Next

End Sub

 

MenuProc kodları ise şunlar olmalı;


Private Sub MenuProc1()

Dim y As Integer
Dim x As String

y = Sheets("dbalim").Range("n2").End(xlDown).Row

x = InputBoxDK("Bu işlem için yönetici şifrenizi girmeniz gerekmektedir!", "Yönetici Şifre Giriş Ekranı")

If x = "" Then
Exit Sub
End If

If x = "buraya belirlediğiniz bir şifre yazın" Then
Sheets("dbalim").Range("n" & y + 1).Value = lsthm.List(lsthm.ListIndex, 0)
Sheets("dbalim").Range("o" & y + 1).Value = 1
Sheets("dbalim").Range("n" & y + 1).Font.Color = -16776961
Sheets("dbalim").Range("o" & y + 1).Font.Color = -16776961

lsthm.Clear

With Me.lsthm
Me.lsthm.ColumnCount = 6
Me.lsthm.ColumnWidths = "50;90;60;30;50;30"
.AddItem
.List(0, 0) = "ID"
.List(0, 1) = "Ürün"
.List(0, 2) = "Firma"
.List(0, 3) = "Miktar"
.List(0, 4) = "İrsaliye No"
.List(0, 5) = "Birim"

End With

Dim lsthmd As Integer

lsthmd = Sheets("dbalim").Range("a2").End(xlDown).Row

For kntszhm = 2 To lsthmd

If Sheets("dbalim").Range("K" & kntszhm).Value = "" And Sheets("dbalim").Range("L" & kntszhm).Value <> "NONE" Then

lsthm.AddItem Sheets("dbalim").Range("A" & kntszhm).Value
lsthm.List(lsthm.ListCount - 1, 1) = Sheets("dbalim").Range("C" & kntszhm).Value
lsthm.List(lsthm.ListCount - 1, 2) = Sheets("dbalim").Range("G" & kntszhm).Value
lsthm.List(lsthm.ListCount - 1, 3) = Sheets("dbalim").Range("D" & kntszhm).Value
lsthm.List(lsthm.ListCount - 1, 4) = Sheets("dbalim").Range("I" & kntszhm).Value
lsthm.List(lsthm.ListCount - 1, 5) = Sheets("dbalim").Range("J" & kntszhm).Value

End If

Next
MsgBox "İşlem Tamamlanmıştır!"
End If
If x <> "" And x <> "belirlediğiniz şifre" Then
MsgBox "Hatalı şifre girişi!"
Exit Sub
End If

End Sub
'
Private Sub MenuProc2()
MsgBox "PopUp menu-2 is activated !"
End Sub
'
Private Sub MenuProc3()
MsgBox "PopUp menu-3 is activated !"
End Sub

Paylaşmayı unutmayın!
0 0 votes
Article Rating
Subscribe
Bildir
guest
0 Yorum
Inline Feedbacks
View all comments