关于ToolBar自定义控件的问题!为什么文字显示不出来!!!

usxue 2004-06-10 10:27:32
在网上无意的找到了一个LumiSoft.UI的控件,我试了一下它的ToolBar的控件,但我使用的时候遇到了问题:
(1)我不能显示文字,只能显示图形;
(2)而且它的DropDownButton的下拉菜单的式样也没有变的好看,还是原来的那个样子。

是不是真的还要自己定义一个ContextMenu?才能使原来的菜单样式改变??
下面是它所定义的源代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace MyTest
{
/// <summary>
/// Summary description for WToolBar.
/// </summary>
public class WToolBar : System.Windows.Forms.ToolBar
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

/// <summary>
///
/// </summary>
public WToolBar()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitForm call

SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint,true);

this.Appearance = ToolBarAppearance.Flat;
}

#region function Dispose

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#endregion

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion


#region function OnPaint

/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);

Point mPt = this.PointToClient(Control.MousePosition);
foreach(ToolBarButton btn in this.Buttons)
{
bool hot = btn.Rectangle.Contains(mPt);

if(btn.Style == ToolBarButtonStyle.Separator)
{
DrawSeparator(e.Graphics,btn);
}
else
{
DrawButton(e.Graphics,hot,btn.Rectangle,btn);
}
}
}

#endregion


#region function DrawButton

private void DrawButton(Graphics g,bool hot,Rectangle buttonRect,ToolBarButton btn)
{
buttonRect = new Rectangle(buttonRect.X,buttonRect.Y,buttonRect.Width-1,buttonRect.Height-1);

Point mPt = this.PointToClient(Control.MousePosition);
bool mouseInDropDown = false;
bool pressed = (Control.MouseButtons == MouseButtons.Left && btn.Rectangle.Contains(mPt));

// Get if mouse is in DropDownButton.
if(btn.Style == ToolBarButtonStyle.DropDownButton)
{
Rectangle dropDownRect = new Rectangle(buttonRect.X+buttonRect.Width-12,buttonRect.Y,12,buttonRect.Height);
mouseInDropDown = dropDownRect.Contains(mPt);
}

if(hot && btn.Enabled)
{
// Fill button backround with hot color
g.FillRectangle(new SolidBrush(Color.FromArgb(182,193,214)),buttonRect);

// If button is pressed, replace hot color with pressed color in button rect.
// For DropDownButton, don't draw pressed color for right dropDown section.
if(pressed && !mouseInDropDown)
{
g.FillRectangle(new SolidBrush(Color.FromArgb(210,218,232)),buttonRect.X,buttonRect.Y,this.ButtonSize.Width,this.ButtonSize.Height);
}

g.DrawRectangle(new Pen(Color.Black),buttonRect);

// Draw vertical(|) separator for DropDownButton
if(btn.Style == ToolBarButtonStyle.DropDownButton)
{
if(!(mouseInDropDown && pressed))
{
g.DrawLine(new Pen(Color.Black),buttonRect.Right-13,buttonRect.Top,buttonRect.Right- 13,buttonRect.Bottom);
}
}
}
else
{
g.FillRectangle(new SolidBrush(Color.FromArgb(219, 216, 209)),buttonRect);
}

// Draw image for button
DrawImage(g,btn.ImageIndex,pressed,!btn.Enabled,buttonRect);

// If dropDown button drow arrow
if(btn.Style == ToolBarButtonStyle.DropDownButton)
{
DrawArrow(g,btn);
}
}

#endregion

#region function DrawImage

private void DrawImage(Graphics g,int index,bool pressed,bool grayed,Rectangle buttonRect)
{
if(index > -1 && this.ImageList != null && this.ImageList.Images.Count > index)
{
Image img = this.ImageList.Images[index];

int iWidth = this.ImageSize.Width;
int bWidth = buttonRect.Height;
int iHeight = this.ImageSize.Height;
buttonRect = new Rectangle(buttonRect.X+2+(bWidth-iWidth)/2,buttonRect.Y+(bWidth-iHeight)/2,buttonRect.Height,buttonRect.Height);

if(pressed)
{
buttonRect = new Rectangle(buttonRect.X+1,buttonRect.Y+1,buttonRect.Height,buttonRect.Height);
}

// Darw grayed image
if(grayed)
{
ControlPaint.DrawImageDisabled(g,img,buttonRect.X,buttonRect.Y,Color.Transparent);
}
else
{
g.DrawImageUnscaled(img,buttonRect);
}
}
}

#endregion

#region function DrawArrow

private void DrawArrow(Graphics g,ToolBarButton btn)
{
int tX = btn.Rectangle.X + btn.Rectangle.Width - 9;
int tY = btn.Rectangle.Y + btn.Rectangle.Height - 12;

// Draw triangle
if(btn.Enabled)
{
g.FillPolygon(new SolidBrush(Color.Black),new Point[]{new Point(tX,tY),new Point(tX+5,tY),new Point(tX+2,tY+3)});
}
else
{
g.FillPolygon(new SolidBrush(Color.FromArgb(166,166,166)),new Point[]{new Point(tX,tY),new Point(tX+5,tY),new Point(tX+2,tY+3)});
}
}

#endregion

#region function DrawSeparator

private void DrawSeparator(Graphics g,ToolBarButton btn)
{
g.DrawLine(new Pen(Color.FromArgb(166, 166, 166)),btn.Rectangle.X+1,btn.Rectangle.Y+2,btn.Rectangle.X+1,btn.Rectangle.Bottom-4);
}

#endregion


#region override OnButtonClick

/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnButtonClick(ToolBarButtonClickEventArgs e)
{
base.OnButtonClick(e);
this.Invalidate();
}

#endregion

#region override OnMouseUp

/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.Invalidate();
}

#endregion

}
}
...全文
158 点赞 收藏 6
写回复
6 条回复
caiyajun512 2004年06月14日
晕,呵。。。。
回复 点赞
usxue 2004年06月14日
呵呵……
我已经解决了!
呵呵……
回复 点赞
yemao20 2004年06月14日
up
回复 点赞
chenyu001 2004年06月14日
可能是你设置的文字排列方式不对
回复 点赞
usxue 2004年06月13日
求人还不如求己!
呵呵……
回复 点赞
usxue 2004年06月10日
自己顶啊!
回复 点赞
发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告