Microsoft KB Archive/297032

= PRB: Context Menu Is Not Displayed Correctly on Windows NT4.0 =

Article ID: 297032

Article Last Modified on 6/14/2006

-

APPLIES TO

 Microsoft Visual J++ 6.0 Standard Edition, when used with:  Microsoft Windows NT 4.0

 Microsoft Windows NT 4.0 Service Pack 6a 

-



This article was previously published under Q297032



SYMPTOMS
On a Windows NT 4.0-based computer, if a user right-clicks a node in a Visual J++ project that includes a basic TreeView control and then right-clicks another node, the original node is still selected, and the context menu does not change (or is not hidden).

Normally, when you right-click a node, a context menu appears. When you right-click another node, the new node should be selected, and the new context menu should appear. However, this does not occur on Windows NT 4.0.



CAUSE
This problem occurs because of a change in the default behavior of the Win32 TrackPopupMenuEx function to include button clicks outside the context menu, but not on the TreeView control.



RESOLUTION
To resolve this problem, subclass the ContextMenu control, and override the show method to call TrackPopupMenuEx with both the TPM_VERTICAL and TPM_RIGHTBUTTON flags set.

To preserve the ability to render the form in the Windows Foundation Classes for Java (WFC) Designer, you must also provide the nested ClassInfo class.

This is a complete listing of the subclassed ContextMenu. import com.ms.wfc.app.*; import com.ms.wfc.core.*; import com.ms.wfc.ui.*; import com.ms.win32.*; import com.ms.wfc.html.*;

public class ContextMenuNT4 extends ContextMenu {   public ContextMenuNT4 {       super; }   public void show(Control control, Point pos) {          if (control == null) throw new WFCInvalidArgumentException(Sys.getString(&quot;InvalidArgument&quot;, &quot;control&quot;, &quot;null&quot;));

if (!control.getHandleCreated || !control.getVisible) throw new WFCException(Sys.getString(&quot;ContextMenuInvalidParent&quot;));

onPopup(Event.EMPTY); pos = control.pointToScreen(pos); Win32.TrackPopupMenuEx(getHandle,                                 win.TPM_VERTICAL | win.TPM_RIGHTBUTTON,                                  pos.x,                                  pos.y,                                  control.getHandle,                                  null); }   //Include this section so that control works well with WFC Designer. public static class ClassInfo extends Menu.ClassInfo {       public static final EventInfo popup = new EventInfo(            ContextMenuNT4.class, &quot;popup&quot;, EventHandler.class,            new DescriptionAttribute(Sys.getString(&quot;MenuItemOnInitDescr&quot;)));

public void getEvents(IEvents events)<BR/> {           super.getEvents(events); events.add(popup); }   } }

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Create a new Windows application in Visual J++ 6.0.</li> Add a TreeView control to the form.</li> Add a few nodes to the TreeView as follows: <ol style="list-style-type: lower-alpha;"> Right-click the TreeView control, and then click Properties.</li> Click Node, and then click the ellipses button.</li> Add a few nodes.</li></ol> </li> Add a ContextMenu control to the form.</li> Right-click the TreeView control, and then click Properties. Click the contextMenu property, and then click contextMenu1 from the drop-down list box to associate the ContextMenu to the TreeView control's contextMenu property.</li>  Add the following code to create a new MenuItem: MenuItem menuItem1 = new MenuItem; </li>  Add the following code in the initform method to set the menuItem and its text: menuItem1.setText(&quot;selected&quot;); contextMenu1.setMenuItems(new MenuItem[]{menuItem1}); </li> On Windows NT 4.0, right-click a TreeView node, and then try to right-click another node. Notice that the node is not selected, and the context menu is not displayed.</li></ol>

Workaround
<ol> Add the new class, ContextMenuNT4, to the above project as listed in the &quot;Resolution&quot; section.</li>  Edit Form1.java of this project to create a new instance of ContextMenuNT4 as follows: ContextMenuNT4 contextMenuNT41 = new ContextMenuNT4; </li> Replace any instance of the original ContextMenu class with this new ContextMenuNT4 class.</li> Build and run the application. The node is selected, and the context menu is displayed as expected.</li></ol>

<div class="references_section">