📄 menucontrol.cs
字号:
protected void OnCollectionCleared()
{
// Reset state ready for a recalculation
Deselect();
RemoveItemTracking();
Recalculate();
Invalidate();
}
protected void OnCollectionInserted(int index, object value)
{
MenuCommand mc = value as MenuCommand;
// We need notification whenever the properties of this command change
mc.PropertyChanged += new MenuCommand.PropChangeHandler(OnCommandChanged);
// Reset state ready for a recalculation
Deselect();
RemoveItemTracking();
Recalculate();
Invalidate();
}
protected void OnCollectionRemoved(int index, object value)
{
// Reset state ready for a recalculation
Deselect();
RemoveItemTracking();
Recalculate();
Invalidate();
}
protected void OnCommandChanged(MenuCommand item, MenuCommand.Property prop)
{
Recalculate();
Invalidate();
}
protected void OnMdiChildActivate(object sender, EventArgs e)
{
// Unhook from event
if (_activeChild != null)
_activeChild.SizeChanged -= new EventHandler(OnMdiChildSizeChanged);
// Remember the currently active child form
_activeChild = _mdiContainer.ActiveMdiChild;
// Need to know when window becomes maximized
if (_activeChild != null)
_activeChild.SizeChanged += new EventHandler(OnMdiChildSizeChanged);
// Might be a change in pendant requirements
Recalculate();
Invalidate();
}
protected void OnMdiChildSizeChanged(object sender, EventArgs e)
{
// Has window changed to become maximized?
if (_activeChild.WindowState == FormWindowState.Maximized)
{
// Reflect change in menu
Recalculate();
Invalidate();
}
}
protected void OnMdiMin(object sender, EventArgs e)
{
if (_activeChild != null)
{
_activeChild.WindowState = FormWindowState.Minimized;
// Reflect change in menu
Recalculate();
Invalidate();
}
}
protected void OnMdiRestore(object sender, EventArgs e)
{
if (_activeChild != null)
{
_activeChild.WindowState = FormWindowState.Normal;
// Reflect change in menu
Recalculate();
Invalidate();
}
}
protected void OnMdiClose(object sender, EventArgs e)
{
if (_activeChild != null)
{
_activeChild.Close();
// Reflect change in menu
Recalculate();
Invalidate();
}
}
protected void CreatePendantButtons()
{
// Create the objects
_minButton = new InertButton(_menuImages, _minIndex);
_restoreButton = new InertButton(_menuImages, _restoreIndex);
_closeButton = new InertButton(_menuImages, _closeIndex);
// Define the constant sizes
_minButton.Size = new Size(_buttonLength, _buttonLength);
_restoreButton.Size = new Size(_buttonLength, _buttonLength);
_closeButton.Size = new Size(_buttonLength, _buttonLength);
// Default their position so they are not visible
_minButton.Location = new Point(-_buttonLength, -_buttonLength);
_restoreButton.Location = new Point(-_buttonLength, -_buttonLength);
_closeButton.Location = new Point(-_buttonLength, -_buttonLength);
// Hookup event handlers
_minButton.Click += new EventHandler(OnMdiMin);
_restoreButton.Click += new EventHandler(OnMdiRestore);
_closeButton.Click += new EventHandler(OnMdiClose);
// Add to display
this.Controls.AddRange(new Control[]{_minButton, _restoreButton, _closeButton});
}
protected void RemovePendantButtons()
{
// Unhook event handlers
_minButton.Click -= new EventHandler(OnMdiMin);
_restoreButton.Click -= new EventHandler(OnMdiRestore);
_closeButton.Click -= new EventHandler(OnMdiClose);
// Remove from display
// Use helper method to circumvent form Close bug
ControlHelper.Remove(this.Controls, _minButton);
ControlHelper.Remove(this.Controls, _restoreButton);
ControlHelper.Remove(this.Controls, _closeButton);
// Release resources
_minButton.Dispose();
_restoreButton.Dispose();
_closeButton.Dispose();
// Remove references
_minButton = null;
_restoreButton = null;
_closeButton = null;
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
// Have we become disabled?
if (!this.Enabled)
{
// Is an item selected?
if (_selected)
{
// Is a popupMenu showing?
if (_popupMenu != null)
{
// Dismiss the submenu
_popupMenu.Dismiss();
// No reference needed
_popupMenu = null;
}
// Reset state
Deselect();
_drawUpwards = false;
SimulateReturnFocus();
}
}
// Do not draw any item as being tracked
RemoveItemTracking();
// Change in state changes the way items are drawn
Invalidate();
}
internal void OnWM_MOUSEDOWN(Win32.POINT screenPos)
{
// Convert the mouse position to screen coordinates
User32.ScreenToClient(this.Handle, ref screenPos);
OnProcessMouseDown(screenPos.x, screenPos.y);
}
protected override void OnMouseDown(MouseEventArgs e)
{
OnProcessMouseDown(e.X, e.Y);
base.OnMouseDown(e);
}
protected void OnProcessMouseDown(int xPos, int yPos)
{
Point pos = new Point(xPos, yPos);
for(int i=0; i<_drawCommands.Count; i++)
{
DrawCommand dc = _drawCommands[i] as DrawCommand;
// Find the DrawCommand this is over
if (dc.SelectRect.Contains(pos) && dc.Enabled)
{
// Is an item already selected?
if (_selected)
{
// Is it this item that is already selected?
if (_trackItem == i)
{
// Is a popupMenu showing
if (_popupMenu != null)
{
// Dismiss the submenu
_popupMenu.Dismiss();
// No reference needed
_popupMenu = null;
}
}
}
else
{
// Select the tracked item
_selected = true;
_drawUpwards = false;
// Is there a change in tracking?
if (_trackItem != i)
{
// Modify the display of the two items
_trackItem = SwitchTrackingItem(_trackItem, i);
}
else
{
// Update display to show as selected
DrawCommand(_trackItem, true);
}
// Is there a submenu to show?
if (dc.Chevron || (dc.MenuCommand.MenuCommands.Count > 0))
User32.PostMessage(this.Handle, WM_OPERATEMENU, 1, 0);
}
break;
}
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
// Is an item currently being tracked?
if (_trackItem != -1)
{
// Is it also selected?
if (_selected == true)
{
// Is it also showing a submenu
if (_popupMenu == null)
{
// Deselect the item
Deselect();
_drawUpwards = false;
DrawCommand(_trackItem, true);
SimulateReturnFocus();
}
}
}
base.OnMouseUp(e);
}
internal void OnWM_MOUSEMOVE(Win32.POINT screenPos)
{
// Convert the mouse position to screen coordinates
User32.ScreenToClient(this.Handle, ref screenPos);
OnProcessMouseMove(screenPos.x, screenPos.y);
}
protected override void OnMouseMove(MouseEventArgs e)
{
// Sometimes we need to ignore this message
if (_ignoreMouseMove)
_ignoreMouseMove = false;
else
OnProcessMouseMove(e.X, e.Y);
base.OnMouseMove(e);
}
protected void OnProcessMouseMove(int xPos, int yPos)
{
// Sometimes we need to ignore this message
if (_ignoreMouseMove)
_ignoreMouseMove = false;
else
{
// Is the first time we have noticed a mouse movement over our window
if (!_mouseOver)
{
// Create the structure needed for User32 call
Win32.TRACKMOUSEEVENTS tme = new Win32.TRACKMOUSEEVENTS();
// Fill in the structure
tme.cbSize = 16;
tme.dwFlags = (uint)Win32.TrackerEventFlags.TME_LEAVE;
tme.hWnd = this.Handle;
tme.dwHoverTime = 0;
// Request that a message gets sent when mouse leaves this window
User32.TrackMouseEvent(ref tme);
// Yes, we know the mouse is over window
_mouseOver = true;
}
Form parentForm = this.FindForm();
// Only hot track if this Form is active
if ((parentForm != null) && parentForm.ContainsFocus)
{
Point pos = new Point(xPos, yPos);
int i = 0;
for(i=0; i<_drawCommands.Count; i++)
{
DrawCommand dc = _drawCommands[i] as DrawCommand;
// Find the DrawCommand this is over
if (dc.SelectRect.Contains(pos) && dc.Enabled)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -