PDA

View Full Version : context menu (right-cklick) help



largo_winch
04-05-2011, 11:38
I need little help for context menu creation. see my example. the context menu doesn't place where mouse is standing. any help would be nice. bye, largo


Empty GUI script created on 05-03-2011 20:19:05 by largo_winch (ThinAIR)
Uses "UI", "console"
---Define a button ID %ButtonClose = 1001 %textboxy = 1002
Type POINTAPI x As Long y As Long End Type
Type CHARRANGE cpMin As Long cpMax As Long End Type
Dim hDlg As DWord
%IDM_UNDO = %WM_USER + 220 %IDM_CUT = %WM_USER + 222 %IDM_COPY = %WM_USER + 223 %IDM_PASTE = %WM_USER + 224 %IDM_DELETE = %WM_USER + 225 %IDM_SELALL = %WM_USER + 226
Global hEdit2 As Long
Declare Function TrackPopupMenu Lib "USER32.DLL" Alias "TrackPopupMenu" (ByVal hMenu As DWord, ByVal uFlags As DWord, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hWnd As DWord, lprc As RECT) As Long Declare Function ClientToScreen Lib "USER32.DLL" Alias "ClientToScreen" (ByVal hWnd As DWord, lpPoint As POINTAPI) As Long DECLARE FUNCTION CheckMenuItem LIB "USER32.DLL" ALIAS "CheckMenuItem" (BYVAL hMenu AS DWORD, BYVAL uIDCheckItem AS DWORD, BYVAL uCheck AS DWORD) AS DWORD Declare Function GetCursorPos Lib "USER32.DLL" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
Dialog New 0, "context_menu, right_click!",-1,-1, 330, 214, _ %WS_POPUP Or _ %WS_VISIBLE Or _ %WS_CLIPCHILDREN Or _ %WS_CAPTION Or _ %WS_SYSMENU Or _ %WS_MINIMIZEBOX, _ 0 To hDlg
Control Add Button, hDlg, %ButtonClose, "Click to kill", 90, 50, 75, 50 Control Add Textbox, hDlg, %textboxy, " ",120,10,80,20
Dialog Show Modal hDlg Call cbDialog
------------------------------------------------ Callback function used to handle dialog events ------------------------------------------------ CallBack Function cbDialog() As Long Local cr As CHARRANGE, hMenuedit2 As DWord,pt As POINTAPI
Select Case CBMSG Case %WM_COMMAND If CBWPARAM = %ButtonClose Then Dialog End CBHNDL
Case %WM_RBUTTONDOWN
Control_AppendText CBHNDL, %textboxy, "%NM_RCLICK " & $CRLF, %TRUE 'SendMessage hEdit2, %EM_EXGETSEL, 0, VarPtr(cr)
MENU New Popup To hMenuEdit2 MENU Add Popup, hEdit2, "test", hMenuEdit2, 4 MENU Add String, hMenuEdit2, "Cu&t" & $TAB & "Ctrl + X", %IDM_CUT, IIf$(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED) '_ '%MF_ENABLED ', _
MENU Add String, hMenuEdit2, "&Copy" & $TAB & "Ctrl + C", %IDM_COPY, _ IIf$(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED)' MENU Add String, hMenuEdit2, "&Paste" & $TAB & "Ctrl + V", %IDM_PASTE, _ IIf$(SendMessage(hEdit2, %EM_CANPASTE, 0, 0), %MF_ENABLED, %MF_GRAYED) Call CheckMenuItem(hMenuEdit2, %IDM_COPY, %MF_CHECKED) MENU Add String, hMenuEdit2, "&Delete" & $TAB & "Del", %IDM_DELETE, _ IIf$(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED) MENU Add String, hMenuEdit2, "-", 0, 0 MENU Add String, hMenuEdit2, "&Select all" & $TAB & "Ctrl + A", %IDM_SELALL, _ IIf$(SendMessage(hEdit2, %WM_GETTEXTLENGTH, 0, 0), %MF_ENABLED, %MF_GRAYED)
pt.x = LOWRD(CBLPARAM) pt.y = HIWRD(CBLPARAM)
MsgBox 0, "x-pos " + Str$(pt.x) & $TAB & " y-pos " + Str$(pt.y) 'ClientToScreen hEdit2, pt 'convert mouse position 'TrackPopupMenu hMenuEdit2, %TPM_LEFTALIGN Or %TPM_RIGHTBUTTON, pt.x, pt.y, 0, hEdit2, ByVal %NULL Function = 0 MENU Attach hMenuEdit2, hDlg '?




Case %WM_DESTROY MsgBox 0, "Window is to be destroyed."
End Select
End Function

ErosOlmi
04-05-2011, 13:52
What editor do you use for your code?
It seems a mess. I do not think many will help if they have to adjust all the code.

Michael Clease
04-05-2011, 14:05
I found the attached code seems to work fine for me but the tagged version (the same??) is not worth looking at.

what OS and screen resolution?

largo_winch
04-05-2011, 14:10
second attempt: copied last text from open office writer document into this forum editor. my thinbasic file hasn't this mess. here a new one.


' Empty GUI script created on 05-03-2011 20:19:05 by largo_winch (ThinAIR)

Uses "UI", "console"

'---Define a button ID
%ButtonClose = 1001
%textboxy = 1002

Type POINTAPI
x As Long
y As Long
End Type

Type CHARRANGE
cpMin As Long
cpMax As Long
End Type

Dim hDlg As DWord

%IDM_UNDO = %WM_USER + 220
%IDM_CUT = %WM_USER + 222
%IDM_COPY = %WM_USER + 223
%IDM_PASTE = %WM_USER + 224
%IDM_DELETE = %WM_USER + 225
%IDM_SELALL = %WM_USER + 226

Global hEdit2 As Long

Declare Function TrackPopupMenu Lib "USER32.DLL" Alias "TrackPopupMenu" (ByVal hMenu As DWord, ByVal uFlags As DWord, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hWnd As DWord, lprc As RECT) As Long
Declare Function ClientToScreen Lib "USER32.DLL" Alias "ClientToScreen" (ByVal hWnd As DWord, lpPoint As POINTAPI) As Long
DECLARE FUNCTION CheckMenuItem LIB "USER32.DLL" ALIAS "CheckMenuItem" (BYVAL hMenu AS DWORD, BYVAL uIDCheckItem AS DWORD, BYVAL uCheck AS DWORD) AS DWORD
Declare Function GetCursorPos Lib "USER32.DLL" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long

Dialog New 0, "context_menu, right_click!",-1,-1, 330, 214, _
%WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPCHILDREN Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_MINIMIZEBOX, _
0 To hDlg

Control Add Button, hDlg, %ButtonClose, "Click to kill", 90, 50, 75, 50
Control Add Textbox, hDlg, %textboxy, " ",120,10,80,20

Dialog Show Modal hDlg Call cbDialog

'------------------------------------------------
' Callback function used to handle dialog events
'------------------------------------------------
CallBack Function cbDialog() As Long
Local cr As CHARRANGE, hMenuedit2 As DWord,pt As POINTAPI

Select Case CBMSG
Case %WM_COMMAND
If CBWPARAM = %ButtonClose Then Dialog End CBHNDL

Case %WM_RBUTTONDOWN

Control_AppendText CBHNDL, %textboxy, "%NM_RCLICK " & $CRLF, %TRUE
'SendMessage hEdit2, %EM_EXGETSEL, 0, VarPtr(cr)

MENU New Popup To hMenuEdit2
MENU Add Popup, hEdit2, "test", hMenuEdit2, 4
MENU Add String, hMenuEdit2, "Cu&t" & $TAB & "Ctrl + X", %IDM_CUT, IIf$(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED) '_ '%MF_ENABLED ', _

MENU Add String, hMenuEdit2, "&Copy" & $TAB & "Ctrl + C", %IDM_COPY, _
IIf$(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED)'
MENU Add String, hMenuEdit2, "&Paste" & $TAB & "Ctrl + V", %IDM_PASTE, _
IIf$(SendMessage(hEdit2, %EM_CANPASTE, 0, 0), %MF_ENABLED, %MF_GRAYED)
Call CheckMenuItem(hMenuEdit2, %IDM_COPY, %MF_CHECKED)
MENU Add String, hMenuEdit2, "&Delete" & $TAB & "Del", %IDM_DELETE, _
IIf$(cr.cpMin <> cr.cpMax, %MF_ENABLED, %MF_GRAYED)
MENU Add String, hMenuEdit2, "-", 0, 0
MENU Add String, hMenuEdit2, "&Select all" & $TAB & "Ctrl + A", %IDM_SELALL, _
IIf$(SendMessage(hEdit2, %WM_GETTEXTLENGTH, 0, 0), %MF_ENABLED, %MF_GRAYED)

pt.x = LOWRD(CBLPARAM)
pt.y = HIWRD(CBLPARAM)

MsgBox 0, "x-pos " + Str$(pt.x) & $TAB & " y-pos " + Str$(pt.y)
'ClientToScreen hEdit2, pt 'convert mouse position
'TrackPopupMenu hMenuEdit2, %TPM_LEFTALIGN Or %TPM_RIGHTBUTTON, pt.x, pt.y, 0, hEdit2, ByVal %NULL
Function = 0
MENU Attach hMenuEdit2, hDlg '?





Case %WM_DESTROY
MsgBox 0, "Window is to be destroyed."

End Select

End Function



the problem is that the menu appears only on left top place, but I wanted to have context menu at same place where mouse placed (free placed area) so this was my intention. there's missing one or two code lines with commands I am sure. bye, largo
ps: my thinbasic code file is correct as attachement!

ErosOlmi
04-05-2011, 14:24
Ok, here it is a working example.
Please consider:

parent window is always CBHNDL and not child controls. They can be used but you need to get their handle.
CHARRANGE (http://www.thinbasic.com/public/products/thinBasic/help/html/charrange.htm) and POINTAPI (http://www.thinbasic.com/public/products/thinBasic/help/html/pointapi.htm) are native to thinBasic so no need to declare those types



'Empty GUI script created On 05-03-2011 20:19:05 by largo_winch (ThinAIR)
Uses "UI", "console"

'---Define a Button ID
%ButtonClose = 1001
%textboxy = 1002

'Type POINTAPI
' x As Long
' y As Long
'End Type
'
'Type CHARRANGE
' cpMin As Long
' cpMax As Long
'End Type

Dim hDlg As DWord
%IDM_UNDO = %WM_USER + 220
%IDM_CUT = %WM_USER + 222
%IDM_COPY = %WM_USER + 223
%IDM_PASTE = %WM_USER + 224
%IDM_DELETE = %WM_USER + 225
%IDM_SELALL = %WM_USER + 226

Global hEdit2 As Long
Declare Function TrackPopupMenu Lib "USER32.DLL" Alias "TrackPopupMenu" (ByVal hMenu As DWord, ByVal uFlags As DWord, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hWnd As DWord, lprc As RECT) As Long



Dialog New 0, "context_menu, right_click!",-1,-1, 330, 214, _
%WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPCHILDREN Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_MINIMIZEBOX, _
0 To hDlg

Control Add Button, hDlg, %ButtonClose, "Click to kill", 90, 50, 75, 50
Control Add Textbox, hDlg, %textboxy, " ",120,10,80,20

Dialog Show Modal hDlg Call cbDialog

'------------------------------------------------
'CallBack Function used To Handle Dialog events
'------------------------------------------------
CallBack Function cbDialog() As Long
Local cr As CHARRANGE
Local hMenuedit2 As DWord
Local pt As POINTAPI
Local lResult As DWord

Select Case CBMSG
Case %WM_COMMAND
If CBWPARAM = %ButtonClose Then Dialog End CBHNDL

Case %WM_RBUTTONDOWN

'---Create menu
MENU New Popup To hMenuEdit2
MENU Add Popup, hEdit2, "test", hMenuEdit2, 4
MENU Add String, hMenuEdit2, "Cu&t" & $TAB & "Ctrl + X", %IDM_CUT, %MF_ENABLED
MENU Add String, hMenuEdit2, "&Copy" & $TAB & "Ctrl + C", %IDM_COPY, %MF_ENABLED
'
MENU Add String, hMenuEdit2, "&Paste" & $TAB & "Ctrl + V", %IDM_PASTE, %MF_ENABLED
MENU Add String, hMenuEdit2, "&Delete" & $TAB & "Del", %IDM_DELETE, %MF_ENABLED
MENU Add String, hMenuEdit2, "-", 0, 0
MENU Add String, hMenuEdit2, "&Select all" & $TAB & "Ctrl + A", %IDM_SELALL, %MF_ENABLED

'---Get position passed by event
pt.x = LOWRD(CBLPARAM)
pt.y = HIWRD(CBLPARAM)

'---Convert local window position to screen position
Win_ClientToScreen CBHNDL, pt

'---Show and Handle menu.
'---Important: use %TPM_RETURNCMD otherwise the choosed command will not be returned
lResult = TrackPopupMenu(hMenuEdit2, %TPM_LEFTALIGN Or %TPM_RIGHTBUTTON Or %TPM_RETURNCMD, pt.x, pt.y, 0, CBHNDL, ByVal %NULL)
Control_SetText CBHNDL, %textboxy, "Menu returned " & lResult

Case %WM_DESTROY
MsgBox 0, "Window is to be destroyed."
End Select
End Function

largo_winch
09-05-2011, 12:31
dear eros, thank you for right help and solution! bye, largo