Context menu & Mouseposition

  • Thread starter Thread starter Christian Westerlund
  • Start date Start date
C

Christian Westerlund

Hi!

I want to get the mouseposition where I tapped on the screen and the
contextmenu appears.
My contextmenu is attached to a datagrid.
I've read about others that had the same problem as I but I didn't find
any soloution that worked.

I've tried the following:

Control.MousePosition : always returns 0 if I click on a cell
otherwise it is correct

[DllImport("coredll.dll")]
public static extern int GetCursorPos(
ref POINT lpPoint ); : The same with this one

override the OnPopup or Show from the Contextmenu : returns 0

override the OnMouseDown or OnGotFocus : returns 0

/Christian
 
Christian,

Use GetMouseMovePoints API function to achieve this. I have copied and
pasted the following code from one of my projects for you to adapt. Place it
in the popup event of the contect menu.

Dim buff(1) As Point
Dim retrieved As Integer
Dim retVal As Boolean
Dim size As Integer = buff.Length
Dim p As Point
retVal = GetMouseMovePoints(buff, size, retrieved)

If retVal = True Then
p.X = CInt(buff(0).X / 4)
p.Y = CInt(buff(0).Y / 4)
End If

HTH,

Geoff
 
Hi!

Thanks! It worked perfect!
Why is it that you must divide the position with 4 though?

/Christian

Christian,

Use GetMouseMovePoints API function to achieve this. I have copied and
pasted the following code from one of my projects for you to adapt. Place it
in the popup event of the contect menu.

Dim buff(1) As Point
Dim retrieved As Integer
Dim retVal As Boolean
Dim size As Integer = buff.Length
Dim p As Point
retVal = GetMouseMovePoints(buff, size, retrieved)

If retVal = True Then
p.X = CInt(buff(0).X / 4)
p.Y = CInt(buff(0).Y / 4)
End If

HTH,

Geoff
Hi!

I want to get the mouseposition where I tapped on the screen and the
contextmenu appears.
My contextmenu is attached to a datagrid.
I've read about others that had the same problem as I but I didn't find
any soloution that worked.

I've tried the following:

Control.MousePosition : always returns 0 if I click on a cell
otherwise it is correct

[DllImport("coredll.dll")]
public static extern int GetCursorPos(
ref POINT lpPoint ); : The same with this one

override the OnPopup or Show from the Contextmenu : returns 0

override the OnMouseDown or OnGotFocus : returns 0

/Christian
 
Hi!

You have to use Platform Invoke to be able to call this method.
Add this to your class:

[DllImport("coredll.dll")]
public static extern bool GetMouseMovePoints( Point[] pptBuf, uint
nBufPoints, ref uint pnPointsRetrieved );

/Christian
 
Any chance you could translate this to VB.NET??

--
Todd Acheson

Christian Westerlund said:
Hi!

You have to use Platform Invoke to be able to call this method.
Add this to your class:

[DllImport("coredll.dll")]
public static extern bool GetMouseMovePoints( Point[] pptBuf, uint
nBufPoints, ref uint pnPointsRetrieved );

/Christian
Hi,

I'm confused I don't seem to be able to call getmousemovepoints. Is it
part of the .netcf?
 
Following is the code to use GetMouseMovePoints function in VB.NET for Pocket PC.

Make sure you specify this:
Imports System.Runtime.InteropServices


Declare the function:
<DllImport("coredll.dll")> Private Shared Function GetMouseMovePoints(ByVal pBuff() As MousePositionXY, ByRef iSize As Integer, ByRef iRetrieved As Integer) As Boolean
End Function

Declare the structure:
Public Structure MousePositionXY
Dim x As Integer
Dim y As Integer
End Structure

Create a function:
Private Function GetMousePosition() As MousePositionXY

Dim MousePositionInfo(3) As MousePositionXY
Dim HitCount As Integer
Dim MousePositionInfoLength As Integer = MousePositionInfo.Length

If GetMouseMovePoints(MousePositionInfo, MousePositionInfoLength, HitCount) Then
GetMousePosition.x = CInt(MousePositionInfo(0).x / 4)
GetMousePosition.y = CInt(MousePositionInfo(0).y / 4)
Else
GetMousePosition.x = 0
GetMousePosition.y = 0
End If

End Function


Call the function:
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

Dim points As MousePositionXY = GetMousePosition()

TextBox1.Text = points.x & " - " & points.y

End Sub


That is all to it.

I have a list view and I wanted to make it editable. I had an idea to place a textbox over the cell I want to edit. I created the above function to track the position where user clicked but don’t know how to get the top and left corner related to the touched x / y coordinates.

Can someone help please.
 
Back
Top