?? tabstub.cs
字號(hào):
Recalculate();
Invalidate();
}
// Generate event to notify where mouse is now hovering
OnPageOver(_selectedIndex);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
// Only select a button or page when using left mouse button
if (e.Button == MouseButtons.Left)
{
// Create a point representing current mouse position
Point mousePos = new Point(e.X, e.Y);
int count = _drawTabs.Count;
// Search each draw cell
for(int index=0; index<count; index++)
{
DrawTab dt = _drawTabs[index] as DrawTab;
// Is mouse pressed in this draw cell?
if (dt.DrawRect.Contains(mousePos))
{
// Prevent any hover timer expiring
_hoverTimer.Stop();
// This becomes the current hover item
_hoverItem = _selectedIndex;
// Not timing a hover change
_hoverOver = _hoverItem;
// Will this cause a change in selection?
if (_selectedIndex != dt.Index)
{
// Change selection and redraw
_selectedIndex = dt.Index;
Recalculate();
Invalidate();
}
// Generate event to notify a click occured on the selection
OnPageClicked(_selectedIndex);
break;
}
}
}
}
public static int TabStubVector(Font font)
{
int fixedVector = _imageVector + _imageGaps;
int minFontVector = font.Height + _imageGaps;
// Make sure at least bit enough for the provided font
if (fixedVector < minFontVector)
fixedVector = minFontVector;
return fixedVector + _sideGap;
}
protected void ResizeControl()
{
int textMax = 0;
// Find largest space needed for drawing page text
using(Graphics g = this.CreateGraphics())
{
foreach(Crownwood.Magic.Controls.TabPage page in _tabPages)
{
// Find width of the requested text
SizeF dimension = g.MeasureString(page.Title, this.Font);
if ((int)dimension.Width > textMax)
textMax = (int)dimension.Width;
}
}
// Calculate total width/height needed
int variableVector = _tabPages.Count * (_imageVector + _imageGaps) + textMax + _imageGap;
// Calculate the fixed direction value
int fixedVector = TabStubVector(this.Font);
// Resize the control as appropriate
switch(_edge)
{
case Edge.Left:
case Edge.Right:
this.Size = new Size(fixedVector, variableVector + _beginGap + _endGap);
break;
case Edge.Top:
case Edge.Bottom:
case Edge.None:
default:
this.Size = new Size(variableVector + _beginGap + _endGap, fixedVector);
break;
}
}
protected void Recalculate()
{
// Create a fresh colleciton for drawing objects
_drawTabs = new ArrayList();
// Need start and end position markers
int posEnd;
int cellVector = _imageVector + _imageGaps;
int posStart = _beginGap;
switch(_edge)
{
case Edge.Left:
case Edge.Right:
posEnd = this.Height - _endGap;
break;
case Edge.Top:
case Edge.Bottom:
case Edge.None:
default:
posEnd = this.Width - _endGap;
break;
}
int count = _tabPages.Count;
// Process from start of list until we find the selected one
for(int index=0; (index<count) && (index!=_selectedIndex); index++)
{
Rectangle drawRect;
// Drawing rectangle depends on direction
switch(_edge)
{
case Edge.Left:
drawRect = new Rectangle(0, posStart, this.Width - _sideGap - 1, cellVector);
break;
case Edge.Right:
drawRect = new Rectangle(_sideGap, posStart, this.Width - _sideGap, cellVector);
break;
case Edge.Bottom:
drawRect = new Rectangle(posStart, _sideGap, cellVector, this.Height - _sideGap);
break;
case Edge.Top:
case Edge.None:
default:
drawRect = new Rectangle(posStart, 0, cellVector, this.Height - _sideGap - 1);
break;
}
// Move starting position
posStart += cellVector;
// Generate new drawing object for this tab
_drawTabs.Add(new DrawTab(_tabPages[index], drawRect, index));
}
// Process from end of list until we find the selected one
for(int index=count-1; (index>=0) && (index!=_selectedIndex); index--)
{
Rectangle drawRect;
// Drawing rectangle depends on direction
switch(_edge)
{
case Edge.Left:
drawRect = new Rectangle(0, posEnd - cellVector, this.Width - _sideGap - 1, cellVector);
break;
case Edge.Right:
drawRect = new Rectangle(_sideGap, posEnd - cellVector, this.Width - _sideGap, cellVector);
break;
case Edge.Bottom:
drawRect = new Rectangle(posEnd - cellVector, _sideGap, cellVector, this.Height - _sideGap);
break;
case Edge.Top:
case Edge.None:
default:
drawRect = new Rectangle(posEnd - cellVector, 0, cellVector, this.Height - _sideGap - 1);
break;
}
// Move starting position
posEnd -= cellVector;
// Generate new drawing object for this tab
_drawTabs.Add(new DrawTab(_tabPages[index], drawRect, index));
}
if (_selectedIndex != -1)
{
Rectangle drawRect;
// Drawing rectangle depends on direction
switch(_edge)
{
case Edge.Left:
drawRect = new Rectangle(0, posStart, this.Width - _sideGap - 1, posEnd - posStart);
break;
case Edge.Right:
drawRect = new Rectangle(_sideGap, posStart, this.Width - _sideGap, posEnd - posStart);
break;
case Edge.Bottom:
drawRect = new Rectangle(posStart, _sideGap, posEnd - posStart, this.Height - _sideGap);
break;
case Edge.Top:
case Edge.None:
default:
drawRect = new Rectangle(posStart, 0, posEnd - posStart, this.Height - _sideGap - 1);
break;
}
// Generate new drawing object for this tab
_drawTabs.Add(new DrawTab(_tabPages[_selectedIndex], drawRect, _selectedIndex));
}
}
protected void AdjustRectForEdge(ref Rectangle rect)
{
// Adjust rectangle to exclude desired edge
switch(_edge)
{
case Edge.Left:
rect.X--;
rect.Width++;
break;
case Edge.Right:
rect.Width++;
break;
case Edge.Bottom:
rect.Height++;
break;
case Edge.Top:
case Edge.None:
default:
rect.Y--;
rect.Height++;
break;
}
}
protected void DrawOutline(Graphics g, bool pre)
{
Rectangle borderRect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
// Adjust for drawing area
switch(_edge)
{
case Edge.Left:
borderRect.Y += _beginGap;
borderRect.Height -= _beginGap + _endGap - 1;
borderRect.Width -= _sideGap;
break;
case Edge.Right:
borderRect.Y += _beginGap;
borderRect.Height -= _beginGap + _endGap - 1;
borderRect.X += _sideGap;
borderRect.Width -= _sideGap;
break;
case Edge.Bottom:
borderRect.Y += _sideGap;
borderRect.Height -= _sideGap;
borderRect.X += _beginGap;
borderRect.Width -= _beginGap + _endGap - 1;
break;
case Edge.Top:
case Edge.None:
default:
borderRect.Height -= _sideGap;
borderRect.X += _beginGap;
borderRect.Width -= _beginGap + _endGap - 1;
break;
}
// Remove unwated drawing edge
AdjustRectForEdge(ref borderRect);
if (pre)
{
if (_style == VisualStyle.IDE)
{
// Fill tab area in required color
using(SolidBrush fillBrush = new SolidBrush(this.BackColor))
g.FillRectangle(fillBrush, borderRect);
}
}
else
{
if (_style == VisualStyle.Plain)
{
using(Pen penL = new Pen(ControlPaint.LightLight(this.BackColor)),
penD = new Pen(ControlPaint.Dark(this.BackColor)))
{
g.DrawLine(penL, borderRect.Left, borderRect.Top, borderRect.Right, borderRect.Top);
g.DrawLine(penL, borderRect.Left, borderRect.Top, borderRect.Left, borderRect.Bottom);
g.DrawLine(penD, borderRect.Right, borderRect.Top, borderRect.Right, borderRect.Bottom);
g.DrawLine(penD, borderRect.Right, borderRect.Bottom, borderRect.Left, borderRect.Bottom);
}
}
}
}
protected void DrawOutlineForCell(Graphics g, Pen pen, Rectangle rect)
{
// Draw border around the tab cell
if (_style == VisualStyle.IDE)
g.DrawRectangle(pen, rect);
else
{
switch(_edge)
{
case Edge.Left:
case Edge.Right:
g.DrawLine(pen, rect.Left + 1, rect.Bottom, rect.Right, rect.Bottom);
break;
case Edge.Top:
case Edge.Bottom:
g.DrawLine(pen, rect.Right, rect.Top + 1, rect.Right, rect.Bottom);
break;
}
}
}
protected override void OnPaintBackground(PaintEventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
// Fill background in required color
if (_style == VisualStyle.IDE)
using(SolidBrush fillBrush = new SolidBrush(_backIDE))
e.Graphics.FillRectangle(fillBrush, this.ClientRectangle);
else
using(SolidBrush fillBrush = new SolidBrush(this.BackColor))
e.Graphics.FillRectangle(fillBrush, this.ClientRectangle);
// Style specific outline drawing
DrawOutline(e.Graphics, true);
// Draw border around area
using(Pen borderPen = new Pen(ControlPaint.LightLight(this.ForeColor)))
{
// Draw each of the draw objects
foreach(DrawTab dt in _drawTabs)
{
Rectangle drawRect = dt.DrawRect;
AdjustRectForEdge(ref drawRect);
// Style specific cell outline drawing
DrawOutlineForCell(e.Graphics, borderPen, drawRect);
// Draw the image in the left/top of the cell
Crownwood.Magic.Controls.TabPage page = dt.TabPage;
int xDraw;
int yDraw;
switch(_edge)
{
case Edge.Left:
case Edge.Right:
xDraw = drawRect.Left + (drawRect.Width - _imageVector) / 2;
yDraw = drawRect.Top + _imageGap;
break;
case Edge.Top:
case Edge.Bottom:
case Edge.None:
default:
xDraw = drawRect.Left + _imageGap;
yDraw = drawRect.Top + (drawRect.Height - _imageVector) / 2;
break;
}
if ((page.ImageIndex != -1) && (page.ImageList != null))
{
// Draw the actual image
e.Graphics.DrawImage(page.ImageList.Images[page.ImageIndex],
new Rectangle(xDraw, yDraw, _imageVector, _imageVector));
}
// Is anything currently selected
if (_selectedIndex != -1)
{
// Is this page selected?
if (page == _tabPages[_selectedIndex])
{
Rectangle textRect;
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.NoClip | StringFormatFlags.NoWrap;
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
// Create text drawing rectangle
switch(_edge)
{
case Edge.Left:
case Edge.Right:
textRect = new Rectangle(drawRect.Left, yDraw + _imageVector + _imageGap,
drawRect.Width, drawRect.Height - _imageVector - _imageGap * 2);
drawFormat.FormatFlags |= StringFormatFlags.DirectionVertical;
break;
case Edge.Top:
case Edge.Bottom:
case Edge.None:
default:
textRect = new Rectangle(xDraw + _imageVector + _imageGap, drawRect.Top,
drawRect.Width - _imageVector - _imageGap * 2, drawRect.Height);
break;
}
Color brushColor = this.ForeColor;
if (_style == VisualStyle.IDE)
brushColor = ControlPaint.Light(brushColor);
using(SolidBrush drawBrush = new SolidBrush(brushColor))
e.Graphics.DrawString(page.Title, this.Font, drawBrush, textRect, drawFormat);
}
}
}
}
// Style specific outline drawing
DrawOutline(e.Graphics, false);
base.OnPaint(e);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -