How to add Context Menu Item with event handler?

Started by Feng.Lin, May 06, 2008, 03:15:57 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Feng.Lin

Hi,

I've got the chance to read the blog post named "Customized Visio HTML Export", the function mentioned in this demo is amazing! Especially the custom context menu when right click the shape, the pop-up WinForm window to give end-user the option, and the Note, I've got a very similiar scenario that need this function and I've did some research these days, as I'm a newbie in Visio Add-in coding, I wonder if it is possble that you share the sample code to me?
Any help would be appreciated!
Thanks in advanced!


Best regards,
Feng.Lin

Visio Guy

Hi Feng.Lin!

The article that you mentioned, Customized Visio HTML Export, has a download at the very end of the text.

You can download that and see how the VBA works with Visio, and how I generated the HTML from the Visio drawing.

The html code isn't very good, but someone who knows html, javascript, or perhaps css could make a much better html-export. This was just an experiment I did years ago, because Visio developers always want more from the Save As Web feature.

I hope the download helps you to get started!
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Feng.Lin

#2
Hi Visio Guy,

Thank you so much for your quick response.
Yes, I found the VBA code in your template, it's helpful for me to get started learning the coding of Visio.
But what I'm eager to know is about your stencil shape named "Hot spots with notes". When I drag and drop the shape to the drawing area, and there are some custom context menu when I right click the shape, I was able to add the context menu by using the following line of codes:

shape1.AddNamedRow((short)Visio.VisSectionIndices.visSectionAction, "ShowProperties", 0);
Visio.Cell actionCellShow = shape1.get_Cells("Actions.ShowProperties");
actionCellShow.Formula = "\"" + "ShowProperties"+ "\"";


But I still don't have any idea how to make it possible that when the user clicks this context menu, a custom WinForm pop-up and allow user to set some configuration as your stencil did, or when a user click this context menu, it would do some operation.
It seems like that there might be some kind of event handler to handle these, would you please share some idea on this?
Appreciated!


Thanks,
Feng.Lin

Lars-Erik

Do you mean the "Hide this note" option in the menu?

This is done using the shape sheet. Check Visioguys Visio file, edit the note shape in the stencil ( double clicking it )
Open the shapes shape sheet and take a look at the "Actions" section.

I'm not getting what you exactly want to do, but you can use the actions part of the forum to for example run a VBA macro.

- Lars-Erik

Feng.Lin

Hi Lars-Erik,

Thank you for your reply!

My question is
How to pop-up a Winform dialog when user click a custom context menu in his right-click menu? Yes I find some information in the "Action" column of "Actions" Section as follows, but I've no idea how to define the action (such as DOCMD(1312), it would pop-up a dialog, where to define this dialog?):

DOCMD(1312)
SETF(GetRef(User.NotesVisible),NOT(User.NotesVisible))
SETF(GetRef(ThePage!User.HideAllNotes),NOT(ThePate!User.HideAllNotes))


Sorry for causing confusing because of my expression.


Thanks,
Feng.Lin

Lars-Erik

I think you'll have to run a macro from the action section, because its not possible to run VB code from the shape sheet.
=RUNMACRO("YourMacro") would run the macro called YourMacro.
There you can run any kind of VB code you want.

Do you want the user to input some data? what do you mean with Winform

A small example i used:
    'Get the users input on what page needs to be (un)hidden
    StrPage = InputBox("Desired Page Size (PFD, P&ID, A4H, A4V, A3, A2, A1 or A0)", "What Page?")

In this case, StrPage will get the value of the user input. Visio will tell you what and how the InputBox command works while your typing it.

Feng.Lin

Hi Lars-Erik,

Thank you so much, yes, I got your point, one question:
If we want to run the Macro, we need to write the Macro code which is already there, that means, before we add the context menu programmtically, we have to pre-define the Macro so that the context menu can "Know" what to do, is there anyway that we add the Macro together with Context Menu programtically?


Thanks,
Feng.Lin

Lars-Erik

I'm not sure what you want to do. Are you adding the right click menu to the shape using code?
if so, why? Why not just create a shape that has the menu included in the shape sheet?


What I think you might mean with the
Quoteis there anyway that we add the Macro together with Context Menu programtically

Do you want the right click menu to be made using a VBA and then somehow insert the action the menu has to preform into the menu? (instead of it calling a macro)
If so the answer is no. The menu can not hold or contain the action. The menu can only call the code that preforms the action.

- Lars-Erik

Visio Guy

#8
There are two areas involved with this discussion:


  • Menus and options on the shapes
  • Menus for the exported web page

The Customized Visio HTML Export article has shapes with special Shape Data fields (Custom Properties) that cause the custom export code to create pop-up menus.

We all need to be sure to specify exactly what we're talking about: customizations to the shapes inside of Visio, or customizations to the exported HTML.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Feng.Lin

Hi Visio Guy & Lars-Erik,

Sorry for the confusion caused because of my poor English.
Hi Visio Guy,
What we're talking about is: Customizations to the shapes inside of Visio.

And, Lars-Erik, yes, what I mean is:
is there anyway that we add the Macro together with Context Menu programtically

What I want is: the right click menu to be made using a VBA and then somehow insert the action the menu has to preform into the menu? (instead of it calling a macro, but with macro function inserted to the page programtically)

if the answer is no, is that mean we have pre-define and write the VBA code of the macro in the page?

Thank you for both of you for your patient, I appreciated.


Thanks,
Feng.Lin

Lars-Erik

#10
The menu itsself cannot contain the VBA code. All it can do is tell Visio to run a piece of code thats in the document or a VBA module inside the document.

Still would like to know why your adding the menu to the shape using code? Why not just use the shapesheet? Seems like alot of unneeded code.

Anway. This is how i would go about it. Open your shape's shapesheet. Go to the "Actions" section.
And add something like this to your shape:



Then we need to make the code itself ( what the menu will run when we click it )
Put the code in the "ThisDocument" section of your VBA code. (Alt + F11 will bring up the VBA editor)

Private Sub HelloWorld()
MsgBox ("Hi!")
End Sub


Here's an example of what the shapes menu would look like:(The top one is the item as shown above, the rest is not shown and is part of a file I'm working on)





Is this what you are looking for?

Feng.Lin

Dear Lars-Erik,

Thank you so much, your sample is really helpful to me, I am now clear about how to handle the Visio context menu, and I've finished my sample yesterday.

Please see attachment the screen capture of my final result.

Thank you again for your patience, everyone in this forum is nice and willing to share, thank you so much!


Thanks,
Feng.Lin