Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / WinForms / Managed / System / WinForms / ContextMenu.cs / 1 / ContextMenu.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Security;
using System.Security.Permissions;
///
///
/// This class is used to put context menus on your form and show them for
/// controls at runtime. It basically acts like a regular Menu control,
/// but can be set for the ContextMenu property that most controls have.
///
[
DefaultEvent("Popup"),
]
public class ContextMenu : Menu {
private EventHandler onPopup;
private EventHandler onCollapse;
internal Control sourceControl;
private RightToLeft rightToLeft = System.Windows.Forms.RightToLeft.Inherit;
///
///
/// Creates a new ContextMenu object with no items in it by default.
///
public ContextMenu()
: base(null) {
}
///
///
/// Creates a ContextMenu object with the given MenuItems.
///
public ContextMenu(MenuItem[] menuItems)
: base(menuItems) {
}
///
///
/// The last control that was acted upon that resulted in this context
/// menu being displayed.
/// VSWHIDBEY 426099 - add demand for AllWindows.
///
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
SRDescription(SR.ContextMenuSourceControlDescr)
]
public Control SourceControl {
[UIPermission(SecurityAction.Demand, Window=UIPermissionWindow.AllWindows)]
get {
return sourceControl;
}
}
///
///
/// [To be supplied.]
///
[SRDescription(SR.MenuItemOnInitDescr)]
public event EventHandler Popup {
add {
onPopup += value;
}
remove {
onPopup -= value;
}
}
///
///
/// Fires when the context menu collapses.
///
[SRDescription(SR.ContextMenuCollapseDescr)]
public event EventHandler Collapse {
add {
onCollapse += value;
}
remove {
onCollapse -= value;
}
}
///
///
/// This is used for international applications where the language
/// is written from RightToLeft. When this property is true,
/// text alignment and reading order will be from right to left.
///
// VSWhidbey 164244: Add a DefaultValue attribute so that the Reset context menu becomes
// available in the Property Grid but the default value remains No.
[
Localizable(true),
DefaultValue(RightToLeft.No),
SRDescription(SR.MenuRightToLeftDescr)
]
public virtual RightToLeft RightToLeft {
get {
if (System.Windows.Forms.RightToLeft.Inherit == rightToLeft) {
if (sourceControl != null) {
return ((Control)sourceControl).RightToLeft;
}
else {
return RightToLeft.No;
}
}
else {
return rightToLeft;
}
}
set {
//valid values are 0x0 to 0x2.
if (!ClientUtils.IsEnumValid(value, (int)value, (int)RightToLeft.No, (int)RightToLeft.Inherit)){
throw new InvalidEnumArgumentException("RightToLeft", (int)value, typeof(RightToLeft));
}
if (RightToLeft != value) {
rightToLeft = value;
UpdateRtl((value == System.Windows.Forms.RightToLeft.Yes));
}
}
}
internal override bool RenderIsRightToLeft {
get {
return (rightToLeft == System.Windows.Forms.RightToLeft.Yes);
}
}
///
///
/// Fires the popup event
///
protected internal virtual void OnPopup(EventArgs e) {
if (onPopup != null) {
onPopup(this, e);
}
}
///
///
/// Fires the collapse event
///
protected internal virtual void OnCollapse(EventArgs e) {
if (onCollapse != null) {
onCollapse(this, e);
}
}
///
///
///
///
[
System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode),
System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
]
protected internal virtual bool ProcessCmdKey(ref Message msg, Keys keyData, Control control) {
sourceControl = control;
return ProcessCmdKey(ref msg, keyData);
}
private void ResetRightToLeft() {
RightToLeft = RightToLeft.No;
}
///
///
/// Returns true if the RightToLeft should be persisted in code gen.
///
internal virtual bool ShouldSerializeRightToLeft() {
if (System.Windows.Forms.RightToLeft.Inherit == rightToLeft) {
return false;
}
return true;
}
///
///
/// Displays the context menu at the specified position. This method
/// doesn't return until the menu is dismissed.
///
public void Show(Control control, Point pos) {
Show(control, pos, NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTBUTTON);
}
///
///
/// Displays the context menu at the specified position. This method
/// doesn't return until the menu is dismissed.
///
public void Show(Control control, Point pos, LeftRightAlignment alignment) {
// This code below looks wrong but it's correct.
// WinForms Left alignment means we want the menu to show up left of the point it is invoked from.
// We specify TPM_RIGHTALIGN which tells win32 to align the right side of this
// menu with the point (which aligns it Left visually)
if (alignment == LeftRightAlignment.Left) {
Show(control, pos, NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTBUTTON | NativeMethods.TPM_RIGHTALIGN);
}
else {
Show(control, pos, NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTBUTTON | NativeMethods.TPM_LEFTALIGN);
}
}
private void Show(Control control, Point pos, int flags) {
if (control == null)
throw new ArgumentNullException("control");
if (!control.IsHandleCreated || !control.Visible)
throw new ArgumentException(SR.GetString(SR.ContextMenuInvalidParent), "control");
sourceControl = control;
OnPopup(EventArgs.Empty);
pos = control.PointToScreen(pos);
SafeNativeMethods.TrackPopupMenuEx(new HandleRef(this, Handle),
flags,
pos.X,
pos.Y,
new HandleRef(control, control.Handle),
null);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Security;
using System.Security.Permissions;
///
///
/// This class is used to put context menus on your form and show them for
/// controls at runtime. It basically acts like a regular Menu control,
/// but can be set for the ContextMenu property that most controls have.
///
[
DefaultEvent("Popup"),
]
public class ContextMenu : Menu {
private EventHandler onPopup;
private EventHandler onCollapse;
internal Control sourceControl;
private RightToLeft rightToLeft = System.Windows.Forms.RightToLeft.Inherit;
///
///
/// Creates a new ContextMenu object with no items in it by default.
///
public ContextMenu()
: base(null) {
}
///
///
/// Creates a ContextMenu object with the given MenuItems.
///
public ContextMenu(MenuItem[] menuItems)
: base(menuItems) {
}
///
///
/// The last control that was acted upon that resulted in this context
/// menu being displayed.
/// VSWHIDBEY 426099 - add demand for AllWindows.
///
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
SRDescription(SR.ContextMenuSourceControlDescr)
]
public Control SourceControl {
[UIPermission(SecurityAction.Demand, Window=UIPermissionWindow.AllWindows)]
get {
return sourceControl;
}
}
///
///
/// [To be supplied.]
///
[SRDescription(SR.MenuItemOnInitDescr)]
public event EventHandler Popup {
add {
onPopup += value;
}
remove {
onPopup -= value;
}
}
///
///
/// Fires when the context menu collapses.
///
[SRDescription(SR.ContextMenuCollapseDescr)]
public event EventHandler Collapse {
add {
onCollapse += value;
}
remove {
onCollapse -= value;
}
}
///
///
/// This is used for international applications where the language
/// is written from RightToLeft. When this property is true,
/// text alignment and reading order will be from right to left.
///
// VSWhidbey 164244: Add a DefaultValue attribute so that the Reset context menu becomes
// available in the Property Grid but the default value remains No.
[
Localizable(true),
DefaultValue(RightToLeft.No),
SRDescription(SR.MenuRightToLeftDescr)
]
public virtual RightToLeft RightToLeft {
get {
if (System.Windows.Forms.RightToLeft.Inherit == rightToLeft) {
if (sourceControl != null) {
return ((Control)sourceControl).RightToLeft;
}
else {
return RightToLeft.No;
}
}
else {
return rightToLeft;
}
}
set {
//valid values are 0x0 to 0x2.
if (!ClientUtils.IsEnumValid(value, (int)value, (int)RightToLeft.No, (int)RightToLeft.Inherit)){
throw new InvalidEnumArgumentException("RightToLeft", (int)value, typeof(RightToLeft));
}
if (RightToLeft != value) {
rightToLeft = value;
UpdateRtl((value == System.Windows.Forms.RightToLeft.Yes));
}
}
}
internal override bool RenderIsRightToLeft {
get {
return (rightToLeft == System.Windows.Forms.RightToLeft.Yes);
}
}
///
///
/// Fires the popup event
///
protected internal virtual void OnPopup(EventArgs e) {
if (onPopup != null) {
onPopup(this, e);
}
}
///
///
/// Fires the collapse event
///
protected internal virtual void OnCollapse(EventArgs e) {
if (onCollapse != null) {
onCollapse(this, e);
}
}
///
///
///
///
[
System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode),
System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.InheritanceDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
]
protected internal virtual bool ProcessCmdKey(ref Message msg, Keys keyData, Control control) {
sourceControl = control;
return ProcessCmdKey(ref msg, keyData);
}
private void ResetRightToLeft() {
RightToLeft = RightToLeft.No;
}
///
///
/// Returns true if the RightToLeft should be persisted in code gen.
///
internal virtual bool ShouldSerializeRightToLeft() {
if (System.Windows.Forms.RightToLeft.Inherit == rightToLeft) {
return false;
}
return true;
}
///
///
/// Displays the context menu at the specified position. This method
/// doesn't return until the menu is dismissed.
///
public void Show(Control control, Point pos) {
Show(control, pos, NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTBUTTON);
}
///
///
/// Displays the context menu at the specified position. This method
/// doesn't return until the menu is dismissed.
///
public void Show(Control control, Point pos, LeftRightAlignment alignment) {
// This code below looks wrong but it's correct.
// WinForms Left alignment means we want the menu to show up left of the point it is invoked from.
// We specify TPM_RIGHTALIGN which tells win32 to align the right side of this
// menu with the point (which aligns it Left visually)
if (alignment == LeftRightAlignment.Left) {
Show(control, pos, NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTBUTTON | NativeMethods.TPM_RIGHTALIGN);
}
else {
Show(control, pos, NativeMethods.TPM_VERTICAL | NativeMethods.TPM_RIGHTBUTTON | NativeMethods.TPM_LEFTALIGN);
}
}
private void Show(Control control, Point pos, int flags) {
if (control == null)
throw new ArgumentNullException("control");
if (!control.IsHandleCreated || !control.Visible)
throw new ArgumentException(SR.GetString(SR.ContextMenuInvalidParent), "control");
sourceControl = control;
OnPopup(EventArgs.Empty);
pos = control.PointToScreen(pos);
SafeNativeMethods.TrackPopupMenuEx(new HandleRef(this, Handle),
flags,
pos.X,
pos.Y,
new HandleRef(control, control.Handle),
null);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ImageSource.cs
- MasterPageBuildProvider.cs
- LOSFormatter.cs
- CssClassPropertyAttribute.cs
- PageFunction.cs
- EmbeddedObject.cs
- SafeSecurityHelper.cs
- IPCCacheManager.cs
- SchemaObjectWriter.cs
- EncoderExceptionFallback.cs
- HMACMD5.cs
- ImageInfo.cs
- TextServicesProperty.cs
- PassportAuthenticationEventArgs.cs
- StreamingContext.cs
- Parsers.cs
- UserUseLicenseDictionaryLoader.cs
- UserInitiatedNavigationPermission.cs
- DoubleKeyFrameCollection.cs
- WarningException.cs
- ServiceDescription.cs
- QilLoop.cs
- PrimitiveXmlSerializers.cs
- XmlAttributeAttribute.cs
- QualifiedCellIdBoolean.cs
- SqlClientPermission.cs
- CodeCompileUnit.cs
- OracleMonthSpan.cs
- ExecutionEngineException.cs
- GridViewColumnHeader.cs
- JsonDeserializer.cs
- ComplexType.cs
- DataGridItemCollection.cs
- NotCondition.cs
- DataControlFieldCell.cs
- XMLSyntaxException.cs
- RoutedUICommand.cs
- Parallel.cs
- HierarchicalDataBoundControlAdapter.cs
- AnnotationAuthorChangedEventArgs.cs
- PrintingPermissionAttribute.cs
- QueryCacheKey.cs
- SerializationAttributes.cs
- HTTPAPI_VERSION.cs
- AssociatedControlConverter.cs
- DataGridRowHeader.cs
- FileLevelControlBuilderAttribute.cs
- LockedAssemblyCache.cs
- Win32Native.cs
- HttpAsyncResult.cs
- ImpersonateTokenRef.cs
- PersonalizationStateQuery.cs
- Vertex.cs
- ReadOnlyHierarchicalDataSourceView.cs
- CurrencyWrapper.cs
- DispatcherOperation.cs
- XhtmlStyleClass.cs
- RuntimeVariableList.cs
- BitmapEffectGroup.cs
- ParameterRetriever.cs
- SqlConnectionPoolProviderInfo.cs
- _AutoWebProxyScriptEngine.cs
- CompileLiteralTextParser.cs
- SecurityPermission.cs
- KeyPullup.cs
- Rect3D.cs
- ConfigurationElement.cs
- CapabilitiesUse.cs
- UpdateEventArgs.cs
- SQLCharsStorage.cs
- RelOps.cs
- CqlBlock.cs
- LinkTarget.cs
- MediaPlayerState.cs
- ReadContentAsBinaryHelper.cs
- RedirectionProxy.cs
- JournalEntry.cs
- PersonalizationStateQuery.cs
- TypedTableBaseExtensions.cs
- RectAnimationUsingKeyFrames.cs
- OracleNumber.cs
- DataGridViewCellPaintingEventArgs.cs
- SessionStateItemCollection.cs
- RegularExpressionValidator.cs
- XmlExpressionDumper.cs
- NullableDoubleMinMaxAggregationOperator.cs
- SubpageParaClient.cs
- Int16AnimationBase.cs
- RangeValidator.cs
- storepermission.cs
- BmpBitmapEncoder.cs
- base64Transforms.cs
- CollectionChangeEventArgs.cs
- DataGridViewDataErrorEventArgs.cs
- AssociationSet.cs
- ContainerCodeDomSerializer.cs
- CoreSwitches.cs
- StackOverflowException.cs
- SortDescriptionCollection.cs
- DesignerActionKeyboardBehavior.cs