PDA

Click to See Complete Forum and Search --> : API Guru needed!


SirLee
Aug 10th, 2000, 06:11 AM
I am displaying a popupwindow in an msflexgrid on rightclick: The user clicks, the cell gets selected and the popup appears. I am using a code demonstration from http://www.vbapi.com for the popup. My problem is I don't know how to close and reopen it somewhere else with another rightclick. In Excel the user right-clicks, the cell gets selected, the menu appears. If the user clicks on another cell the popup closes, the new cell is selected and the popup reopens. How can I do that in my code?

Thank you!

Here is the code:

''''''''''''''Form'''''''''''''''''''''''''

Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Select Case Button
Case 2
Call LeftClick

End Select

End Sub




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

Select Case Button
Case 2
Call PopupWindow
End Select



End Sub







'''''''''''''''PopupWindow Module'''''''''''''''''''''''''''
Private Const ID_ABOUT = 101
Private Const ID_SEPARATOR = 102
Private Const ID_EXIT = 103


Public Declare Function CreatePopupMenu Lib "user32.dll" () As Long
Public Declare Function DestroyMenu Lib "user32.dll" (ByVal hMenu As Long) As Long
Public Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type
Public Const MIIM_STATE = &H1
Public Const MIIM_ID = &H2
Public Const MIIM_TYPE = &H10
Public Const MFT_SEPARATOR = &H800
Public Const MFT_STRING = &H0
Public Const MFS_DEFAULT = &H1000
Public Const MFS_ENABLED = &H0
Public Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" (ByVal _
hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As _
MENUITEMINFO) As Long
Public Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Public Type TPMPARAMS
cbSize As Long
rcExclude As RECT
End Type
Public Declare Function TrackPopupMenuEx Lib "user32.dll" (ByVal hMenu As Long, ByVal _
fuFlags As Long, ByVal x As Long, ByVal y As Long, ByVal hWnd As Long, lptpm As _
TPMPARAMS) As Long
Public Const TPM_LEFTALIGN = &H0
Public Const TPM_TOPALIGN = &H0
Public Const TPM_NONOTIFY = &H80
Public Const TPM_RETURNCMD = &H100
Public Const TPM_LEFTBUTTON = &H0
Public Type POINT_TYPE
x As Long
y As Long
End Type
Public Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINT_TYPE) As Long
Public Declare Function SetRectEmpty Lib "user32.dll" (lpRect As RECT) As Long

'''''''''''''''''''''''''''''''''''''''''''''''''''''





Function PopupWindow()


Dim hPopupMenu As Long ' handle to the popup menu to display
Dim mii As MENUITEMINFO ' describes menu items to add
Dim tpm As TPMPARAMS ' identifies the exclusion rectangle
Dim curpos As POINT_TYPE ' holds the current mouse coordinates
Dim menusel As Long ' ID of what the user selected in the popup menu
Dim retval As Long ' generic return value

' Create the popup menu that will be displayed.
hPopupMenu = CreatePopupMenu()
' Add the menu's first item: "About This Problem..."
With mii
' The size of this structure.
.cbSize = Len(mii)
' Which elements of the structure to use.
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
' The type of item: a string.
.fType = MFT_STRING
' This item is currently enabled and is the default item.
.fState = MFS_ENABLED Or MFS_DEFAULT
' Assign this item an item identifier.
.wID = ID_ABOUT
' Display the following text for the item.
.dwTypeData = "&About This Example..."
.cch = Len(.dwTypeData)
End With
retval = InsertMenuItem(hPopupMenu, 0, 1, mii)
' Add the second item: a separator bar.
With mii
.fType = MFT_SEPARATOR
.fState = MFS_ENABLED
.wID = ID_SEPARATOR
End With
retval = InsertMenuItem(hPopupMenu, 1, 1, mii)
' Add the final item: "Exit".
With mii
.fType = MFT_STRING
.wID = ID_EXIT
.dwTypeData = "E&xit"
.cch = Len(.dwTypeData)
End With
retval = InsertMenuItem(hPopupMenu, 2, 1, mii)

' Determine where the mouse cursor currently is, in order to have
' the popup menu appear at that point.
retval = GetCursorPos(curpos)

' Make the exclusion rectangle empty because there's no need for it here.
With tpm
' Size of the structure.
.cbSize = Len(tpm)
' Make the exclusion rectangle empty.
retval = SetRectEmpty(.rcExclude)
End With



' Display the popup menu at the mouse cursor. Instead of sending messages
' to window Form1, have the function merely return the ID of the user's selection.
menusel = TrackPopupMenuEx(hPopupMenu, TPM_TOPALIGN Or TPM_LEFTALIGN Or TPM_NONOTIFY _
Or TPM_LEFTBUTTON Or TPM_RETURNCMD Or TPM_LEFTBUTTON, curpos.x, curpos.y, FrmMain.hWnd, tpm)

' Before acting upon the user's selection, destroy the popup menu now.

retval = DestroyMenu(hPopupMenu)


Select Case menusel
Case ID_ABOUT
' Use the Visual Basic MsgBox function to display a short message in a dialog
' box. Using the MessageBox API function isn't necessary.
retval = MsgBox("This example demonstrates how to use the API to display " & _
"a pop-up menu.", vbOKOnly Or vbInformation, "Windows API Guide")
Case ID_EXIT
' End this program by closing and unloading Form1.
Unload FrmMain
End Select



End Function



'''''''''''''''''''''


Using Visual Basic 5.0

gwdash
Aug 10th, 2000, 05:41 PM
why arn't you just using the PopupMenu Method?

SirLee
Aug 11th, 2000, 02:23 AM
Oops, I didn't know you could do that!

Thank you.


But my problem stays the same....

even if I use PopupMenu the popup is not closed and opened at the new position if I rightclick somewhere else.

Do you know how to do that?

Every other Windows application is capable of that

gwdash
Aug 11th, 2000, 10:35 AM
Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbRightButton Then
PopupMenu mnuTest, vbPopupMenuRightButton
MSFlexGrid1.Row = MSFlexGrid1.MouseRow
MSFlexGrid1.Col = MSFlexGrid1.MouseCol
Debug.Print "Row"; MSFlexGrid1.Row; "Col"; MSFlexGrid1.Col
Debug.Print "Row"; MSFlexGrid1.MouseRow; "Col"; MSFlexGrid1.MouseCol
End If
End Sub

This should do the trick :)

Iluvaras
Aug 14th, 2000, 07:07 AM
Hurray!!

This might solve my problem as well! :-)))

Regards Kishpa

SirLee
Aug 14th, 2000, 10:19 AM
Thank you very much for your help!

This is working perfectly!

:) :) :) :)