With this recent articles, Dino Esposito exposes some methods to customize menu user interface with .NET.
The arguments are:
- Customizing Menu Rendering
- Overriding the Menu of a Form
- The MeasureItem Event
- The DrawItem Event
- Painting the Menu Item
- Context Menu and TextBoxes
- Using Graphical Menus Seamlessly
but what I want to underline and mention is the response to one of my last questions: the Context Menu of a Textbox is customizable?
The answer is this:
The Textbox is the only control in Windows Forms that has a built-in context menu. The control exposes a ContextMenu property, but it doesn't return an instance of the context menu that appears when you right-click. Why? The code for the textbox's context menu is hardcoded in the Win32 API. In particular, the context menu is generated on the fly each time the user right-clicks. The menu is then released and destroyed once the selection is made. You can replace this menu with your own by setting the ContextMenu property, but there's no chance to manipulate the original menu. Since the menu has a very short (and unmanaged) life, how can you easily subclass it and mark it as owner-draw?
The simplest trick that I've found is the following: create a TextboxContextMenu class that represents a custom context menu with all of the items of a standard textbox context menu (Undo, Cut, Copy, and the like). Implementing these methods is relatively simple and samples can be found in the MSDN documentation. Once you hold an instance of a managed class that works like the typical context menu of a TextBox, you first make it owner-draw and then you bind it to any TextBox you want:
Dim txtMenu As New TextBoxContextMenu
gMenu.AddIcon(txtMenu.MenuItemCut, "..\images\cut.bmp")
gMenu.AddIcon(txtMenu.MenuItemCopy, "..\images\copy.bmp")
gMenu.AddIcon(txtMenu.MenuItemPaste, "..\images\paste.bmp")
gMenu.AddIcon(txtMenu.MenuItemDelete, "..\images\delete.bmp")
TextBox1.ContextMenu = txtMenugMenu.Init(TextBox1.ContextMenu)
The TextBoxContextMenu class lives in the same assembly that contains the GraphicMenu class. You should be aware that owner-drawn menus don't work correctly when attached to a NotifyIcon component. For more information on this problem see Knowledge Base article 827043.
I think it's a really good tip!
This week Microsoft annnounced the beta availability of the Java Language Conversion Assistant (JLCA), a tool designed to ease migration of Java applications to the .NET environment.
I'd like to test it because it seems very interesting. If there's anyone that have tryed this tool, i'd like to know what's his opinion...