PRB: Context Menu Is Not Displayed Correctly on Windows NT4.0 (297032)



The information in this article applies to:

  • Microsoft Visual J++ 6.0, when used with:
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows NT 4.0 SP6a

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("InvalidArgument",
                                          "control",
                                          "null"));

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

           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, "popup", EventHandler.class,
            new DescriptionAttribute(Sys.getString("MenuItemOnInitDescr")));

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

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new Windows application in Visual J++ 6.0.
  2. Add a TreeView control to the form.
  3. Add a few nodes to the TreeView as follows:
    1. Right-click the TreeView control, and then click Properties.
    2. Click Node, and then click the ellipses button.
    3. Add a few nodes.
  4. Add a ContextMenu control to the form.
  5. 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.
  6. Add the following code to create a new MenuItem:
    MenuItem menuItem1 = new MenuItem();
    					
  7. Add the following code in the initform method to set the menuItem and its text:
    menuItem1.setText("selected");
    contextMenu1.setMenuItems(new MenuItem[]{menuItem1});
    					
  8. 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.

Workaround

  1. Add the new class, ContextMenuNT4, to the above project as listed in the "Resolution" section.
  2. Edit Form1.java of this project to create a new instance of ContextMenuNT4 as follows:
    ContextMenuNT4 contextMenuNT41 = new ContextMenuNT4();
    					
  3. Replace any instance of the original ContextMenu class with this new ContextMenuNT4 class.
  4. Build and run the application. The node is selected, and the context menu is displayed as expected.

REFERENCES

For support information about Visual J++ and the SDK for Java, visit the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/14/2006
Keywords:kbCmnCtrls kbprb kbWFC KB297032