[Windows API] Popup menu and selected text

Orbeaman

New Member
Joined
Dec 15, 2018
Messages
1
Hello,

I've already tried to use the Office.CommandBars class to implement popup menu with text boxes. It seems to perfectly work on Excel, but I don't actually develop the userform in Excel, but in CATIA (CAD software which comes along VBA7 64 bits). Importing the Office library doesn't seem to work.

I've already read that text boxes don't provide any window handle (windowless). Is there another way to capture the selected text. If I use the GetWindowFromPoint function it returns the handle of desktop.. I'm not really optimistic using the handle of the userform as it is not an Edit class.

Code:
'
Public Sub popup()
    
    Dim Pt As API.POINT: API.GetCursorPos lpPoint:=Pt
    
    Dim hwnd As LongPtr:  hwnd = API.WindowFromPoint(xPoint:=Pt.X, yPoint:=Pt.Y)
    
    Dim hMenu As LongPtr: hMenu = API.CreatePopupMenu()
    
    API.AppendMenu hMenu:=hMenu, wFlags:=API.MF_STRING, wIDNewItem:=1, lpNewItem:="&Copy"
    API.AppendMenu hMenu:=hMenu, wFlags:=API.MF_STRING, wIDNewItem:=2, lpNewItem:="C&ut"
    API.AppendMenu hMenu:=hMenu, wFlags:=API.MF_STRING, wIDNewItem:=3, lpNewItem:="&Paste"
    
    Dim lpRC As API.RECT
    
    Select Case API.TrackPopupMenu(hMenu:=hMenu, uFlags:=API.TPM_LEFTALIGN Or API.TPM_RETURNCMD Or API.TPM_RIGHTBUTTON, X:=Pt.X, Y:=Pt.Y, nReserved:=0&, hwnd:=hwnd, lpRC:=lpRC)
    Case 1: 'misc.setClipboard
    
        Dim startpos As Long, endpos As Long
        
        Dim lRet As Long: lRet = API.SendMessage(hwnd:=hwnd, wMsg:=API.EM_GETSEL, wParam:=startpos, lParam:=endpos)
        
        Dim lpClassName As String: lpClassName = VBA.Space$(255)
        
        lRet = API.GetClassName(hwnd:=hwnd, lpClassName:=lpClassName, nMaxCount:=Len(lpClassName))
        
        Debug.Print hwnd, VBA.left$(lpClassName, lRet), hMenu, lRet, startpos, endpos ' => returns "wndclass_desked_gsk" for the window class name
        
'        Dim wParam As Long
'
'        Dim lRet As Long: lRet = API.SendMessage(hWnd:=hWnd, wMsg:=API.EM_GETSEL, wParam:=wParam, lParam:=0&)
'
'        Debug.Print hWnd, hMenu, lRet, misc.LOWORD(wParam), misc.HIWORD(wParam)
        
        If Err.LastDllError Then Debug.Print misc.GetSystemErrorMessageText(ErrorNumber:=Err.LastDllErr) ' no error
    
    Case 2:
    Case 3: 'misc.getClipboard
    End Select
    
    API.DestroyMenu hMenu:=hMenu
    
End Sub

Ps : I've writtent all API declarations in the API module and the misc module contains some other functions / subs which are not important..

Does anyone has a solution about the issue?

Thanks :p
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
.
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">[COLOR=#101094][FONT=inherit]Private[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Sub[/FONT][/COLOR][COLOR=#303336][FONT=inherit] TextBox1_MouseDown[/FONT][/COLOR][COLOR=#303336][FONT=inherit]([/FONT][/COLOR][COLOR=#101094][FONT=inherit]ByVal[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Button [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Integer[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#101094][FONT=inherit]ByVal[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Shift [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Integer[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#303336][FONT=inherit] _
[/FONT][/COLOR][COLOR=#101094][FONT=inherit]ByVal[/FONT][/COLOR][COLOR=#303336][FONT=inherit] X [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Single[/FONT][/COLOR][COLOR=#303336][FONT=inherit],[/FONT][/COLOR][COLOR=#101094][FONT=inherit]ByVal[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Y [/FONT][/COLOR][COLOR=#101094][FONT=inherit]As[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Single[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]With[/FONT][/COLOR][COLOR=#303336][FONT=inherit] TextBox1
        [/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]SelStart [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#7D2727][FONT=inherit]0[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
        [/FONT][/COLOR][COLOR=#303336][FONT=inherit].[/FONT][/COLOR][COLOR=#303336][FONT=inherit]SelLength [/FONT][/COLOR][COLOR=#303336][FONT=inherit]=[/FONT][/COLOR][COLOR=#303336][FONT=inherit] Len[/FONT][/COLOR][COLOR=#303336][FONT=inherit](.[/FONT][/COLOR][COLOR=#303336][FONT=inherit]Text[/FONT][/COLOR][COLOR=#303336][FONT=inherit])[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
    [/FONT][/COLOR][COLOR=#101094][FONT=inherit]End[/FONT][/COLOR][COLOR=#101094][FONT=inherit]With[/FONT][/COLOR][COLOR=#303336][FONT=inherit]
[/FONT][/COLOR][COLOR=#101094][FONT=inherit]End[/FONT][/COLOR][COLOR=#101094][FONT=inherit]Sub[/FONT][/COLOR]</code>


https://stackoverflow.com/questions...he-contents-of-a-textbox-once-it-is-activated



And if you want to copy the text :

Code:
Option Explicit


Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)


    With TextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
        .Copy
    End With
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,175
Members
448,870
Latest member
max_pedreira

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top