Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2006

Tip: Looking for answers? Try searching our database.

Opening a ContextMenu without a parent control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hans Merkl - 22 Feb 2006 23:04 GMT
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
"Jeffrey Tan[MSFT]" - 23 Feb 2006 03:01 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Max - 01 Mar 2006 14:11 GMT
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

> Hi Hans ,
>
[quoted text clipped - 35 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 02 Mar 2006 01:45 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.