暂时还没有全部完成,绘制的工作已经全部OK,事件的处理还在进行中,先把效果发出来下.
一.设计时效果:
二.运行时效果:
三.简单谈谈设计思路:
由于这段时间工作比较忙,谢谢c#代码的时间很好,所以直到昨天才把本来很少的代码完成.现在谈谈主要的实现思路.
这个控件同样是一个复合控件,由一个Captiontop(继承自panel的控件),与一个jcstoolbox组成.captiontop主要实现菜单,停靠,关闭等按钮的操作与事件响应.jcstoolbox来实现各个category组的具体绘制工作.toolleft,要在左侧来绘制图片与竖体布局的文字.在它的onmousehover事件中对鼠标作出处理.使用一个timer来判断是否鼠标离开了复合控件,并且停靠按钮没有被设置为"别针".部分代码如下,如果对全部源代码感兴趣,可以mail给我:
四.部分源代码:
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace JcsExpLibary.JcsToolBox
{
public partial class JcsToolBoxControl : UserControl
{
private bool _isexpend = false ;
private int _maxWidth = 196 ;
private Timer timer;
private ContextMenuStrip cmenu;
private bool _isstop = false ;
private const int _top = 15 ; // 工具栏的绘制初始位置
private const int _toolboxwidth = 28 ;
private Color _captionForeColor = Color.Black;
private ImageList _imagelist;
private string _caption = " 工具栏 " ;
private int _imageindex = 0 ;
private Image _image;
private bool _isMouseMove = false ;
private Rectangle _buttonRect;
private Rectangle _textRect;
public delegate void ToolBoxLabelMouseHover();
public event ToolBoxLabelMouseHover ToolBoxLabelMouseHoverEvent;
public event ToolBoxLabelMouseHover ToolBoxLabelLeaveEvent;
public JcsToolBoxControl()
{
InitializeComponent();
this .MinimumSize = new Size(_toolboxwidth, _toolboxwidth);
_buttonRect = new Rectangle( 0 , _top, 22 , _top + 72 );
_textRect = new Rectangle( 0 , _top + 25 , 22 , _top + 72 );
timer = new Timer();
timer.Interval = 1000 ;
timer.Tick += new EventHandler(timer_Tick);
// cmenu = new ContextMenuStrip();
// AddContextMenuItem();
this .toolCaption1.MouseHover += new EventHandler(toolCaption1_MouseHover);
this .toolTop1.BtnCloseEvent += new ToolTop.ButtonclickEvent(toolTop1_BtnCloseEvent);
this .toolTop1.BtnStopEvent += new ToolTop.ButtonclickEvent(toolTop1_BtnStopEvent);
this .toolTop1.BtnMenuEvent += new ToolTop.ButtonclickEvent(toolTop1_BtnMenuEvent);
this .toolTop1.MouseLeave += new EventHandler(toolTop1_MouseLeave);
this .jcsToolBox1.MouseLeave += new EventHandler(jcsToolBox1_MouseLeave);
this .Width = _toolboxwidth;
}
void toolCaption1_MouseHover( object sender, EventArgs e)
{
Point p = this .PointToClient(MousePosition);
if ( this ._buttonRect.Contains(p))
{
toolLeft1_ToolBoxLabelMouseHoverEvent();
if ( this .ToolBoxLabelMouseHoverEvent != null )
this .ToolBoxLabelMouseHoverEvent();
}
else
{
toolLeft1_ToolBoxLabelLeaveEvent();
if ( this .ToolBoxLabelLeaveEvent != null )
this .ToolBoxLabelLeaveEvent();
}
}
void toolTop1_BtnMenuEvent()
{
if (cmenu != null )
{
Point p = new Point( this .toolTop1.MenuButton.Bounds.X + 3 , this .toolTop1.MenuButton.Bounds.Height + 2 );
cmenu.Show(toolTop1.PointToScreen(p));
}
}
void jcsToolBox1_MouseLeave( object sender, EventArgs e)
{
if ( ! this ._isstop)
this .timer.Start();
}
void toolTop1_MouseLeave( object sender, EventArgs e)
{
if ( ! this ._isstop)
this .timer.Start();
}
void timer_Tick( object sender, EventArgs e)
{
if ( this .Bounds.Contains(MousePosition))
return ;
if ( this ._isstop)
return ;
this .Width = _toolboxwidth;
this .timer.Stop();
}
void toolLeft1_ToolBoxLabelLeaveEvent()
{
if ( this .Width == this ._maxWidth)
{
this .Width = _toolboxwidth;
}
}
public int ToolBoxMaxWidth
{
get { return this ._maxWidth; }
set { this ._maxWidth = value; }
}
public bool IsExpend
{
get { return this ._isexpend; }
set
{
if (_isexpend == value)
return ;
this ._isexpend = value;
OnIsExpendChanged();
}
}
public ContextMenuStrip BtnContextMenu
{
get { return cmenu; }
set { this .cmenu = value; }
}
private void OnIsExpendChanged()
{
if (_isexpend)
{
this .Width = this ._maxWidth;
}
}
void toolLeft1_ToolBoxLabelMouseHoverEvent()
{
if ( this .Width == _toolboxwidth)
{
this .Width = _toolboxwidth + this .jcsToolBox1.Width ;
}
}
void toolTop1_BtnStopEvent()
{
this ._isstop = ! _isstop;
this .toolTop1.IsChange = _isstop;
if (_isstop)
{
this .timer.Stop();
}
else
{
this .timer.Start();
}
}
void toolTop1_BtnCloseEvent()
{
this .Visible = false ;
}
#region "属性"
public Color CaptionForeColor
{
get { return _captionForeColor; }
set
{
_captionForeColor = value;
this .Invalidate();
}
}
public string Caption
{
get { return _caption; }
set
{
this ._caption = value;
Invalidate();
}
}
public Image Image
{
get { return this ._image; }
set
{
this ._image = value;
this .Invalidate();
}
}
public ImageList ImageList
{
get { return this ._imagelist; }
set { this ._imagelist = value; }
}
public int ImageIndex
{
get { return this ._imageindex; }
set
{
if (_imagelist == null )
return ;
if ( this ._imagelist.Images.Count > 0 && this ._imagelist.Images.Count > value)
{
this ._imageindex = value;
}
else
{
throw new Exception( " 索引超出ImageList的索引范围! " );
}
}
}
public bool IsMouseMove
{
get { return this ._isMouseMove; }
}
#endregion
#region "重写"
protected override void OnPaint(PaintEventArgs e)
{
base .OnPaint(e);
Graphics g = e.Graphics;
// 绘制最外部边框
ControlPaint.DrawBorder3D(g, new Rectangle( 0 , 0 ,_toolboxwidth , this .ClientRectangle.Height ), Border3DStyle.Raised);
}
protected override void OnResize(EventArgs e)
{
base .OnResize(e);
if ( this .Width > _toolboxwidth)
{
this .jcsToolBox1.Width = this .Width - _toolboxwidth;
this .toolTop1.Width = this .Width - _toolboxwidth;
}
this .Invalidate();
}
#endregion
}
}