Opening a ContextMenu without a parent control

H

Hans Merkl

Hi,

I am writing an Excel addin with C#. I have some ActiveX objects embedded
within the spreadsheet and when the user clicks on one of them I want to
show a context menu.

Now the problem is that ContextMenu.Show() needs a Control object as
parameter but in my scenario there is no Winforms control object. I have
the HWND window handle of the ActiveX object but I can't figure out how to
pass it to the ContextMenu object.

Any ideas how to do this?

Thanks

Hans Merkl
 
J

Jeffrey Tan[MSFT]

Hi Hans ,

Thanks for your post.

I am not familiar with Office automation. Normally, ContextMenu.Show()
method internally invokes TrackPopupMenuEx win32 API to do the actual work.
So we may do something like this:

[StructLayout(LayoutKind.Sequential)]
public class TPMPARAMS
{
public int cbSize;
public int rcExclude_left;
public int rcExclude_top;
public int rcExclude_right;
public int rcExclude_bottom;
}


[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool TrackPopupMenuEx(IntPtr hmenu, int fuFlags, int
x, int y, IntPtr hwnd, TPMPARAMS tpm);

private void button1_Click(object sender, System.EventArgs e)
{
Point pt=this.PointToScreen(this.textBox1.Location);
TrackPopupMenuEx(this.contextMenu1.Handle, 0x42,
pt.X+10, pt.Y+10, this.textBox1.Handle, null);
}

You can substitute the "this.textBox1.Handle" with your activeX window
handle value. However, I am not sure if this work for Office Addin.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Max

Jeffrey,

Thanks for the suggestion. I have tried it but when I use this technique
with an ActiveX control embedded in Excel all items within submenus are
disabled. Anyway, I have solved it by creating a transparent dummy form and
using this form as parent for the menu.

HAns
 
J

Jeffrey Tan[MSFT]

Hi HAns,

I am glad your use the transparent layer to resolve the problem. If you
need further help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top