WindowsMobile如何自定义一个ToolBar?[币多理财助手部分源码]
2012-02-10 13:59
253 查看
如图ToolBar,效果还是很不错的,这样的控件如何实现呢?
其实也很简单!整个控件由3个部分组成:
1、ImageLinkBar ,图形按钮控件。上图的ToolBar就是由6个这样的图形按钮组成。
2、UCToolBar, UserControl组件,上面放6个ImageLinkBar。
3、UCTip, UserControl组件,用于显示提示消息。如上图,点种第三个图标不放,将显示一个消息框,提示是删除按钮。
现在整理代码如下:
using System; using System.Text; using System.Drawing; using System.Windows.Forms; namespace PTool { public class ImageLinkBar : Control { private IImage imageIcon; //IImage类 网上可以找到 private IImage imageBackActive; private IImage imageBack; private bool isSelected; private bool isPressed; private Bitmap m_bmpMemoire; private Graphics gxMemoire; public IImage ImageIcon { get { return imageIcon; } set { imageIcon = value; this.Invalidate(); } } public IImage ImageBackActive { get { return imageBackActive; } set { imageBackActive = value; this.Invalidate(); } } public IImage ImageBack { get { return imageBack; } set { imageBack = value; this.Invalidate(); } } public ImageLinkBar() : base() { } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { if (m_bmpMemoire == null) { m_bmpMemoire = new Bitmap(ClientSize.Width, ClientSize.Height); } gxMemoire = Graphics.FromImage(m_bmpMemoire); if (SystemConfig.IsWM65) //WM6.5手机,主题风格与众不同哦 { GradientStyle.Fill( gxMemoire, this.ClientRectangle, System.Drawing.SystemColors.Control, System.Drawing.SystemColors.WindowFrame, GradientStyle.FillDirection.TopToBottom); } else { GradientStyle.Fill( gxMemoire, this.ClientRectangle, System.Drawing.SystemColors.Window, System.Drawing.SystemColors.ActiveCaption, GradientStyle.FillDirection.TopToBottom); } IntPtr hdcDest = gxMemoire.GetHdc(); Rectangle dstRect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height); if (isPressed) { if (imageBackActive != null) imageBackActive.Draw(hdcDest, ref dstRect, IntPtr.Zero); } else if (isSelected) { if (imageBack != null) imageBack.Draw(hdcDest, ref dstRect, IntPtr.Zero); } if (imageIcon != null) { if (SystemConfig.IsHVGA) //如何判断是不是HVGA手机,就不用说了吧 dstRect = new Rectangle((ClientSize.Width - 32 * 2) / 2, (ClientSize.Height - 32 * 2) / 2, (ClientSize.Width + 32 * 2) / 2, (ClientSize.Height + 32 * 2) / 2); else dstRect = new Rectangle((ClientSize.Width - 24 * 2) / 2, (ClientSize.Height - 24 * 2) / 2, (ClientSize.Width + 24 * 2) / 2, (ClientSize.Height + 24 * 2) / 2); imageIcon.Draw(hdcDest, ref dstRect, IntPtr.Zero); } gxMemoire.ReleaseHdc(hdcDest); e.Graphics.DrawImage(m_bmpMemoire, 0, 0); gxMemoire.Dispose(); base.OnPaint(e); } protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) { base.OnMouseDown(e); this.Focus(); isSelected = true; isPressed = true; this.Invalidate(); } protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) { base.OnMouseUp(e); isPressed = false; this.Invalidate(); } protected override void OnGotFocus(System.EventArgs e) { base.OnGotFocus(e); isSelected = true; this.Invalidate(); } protected override void OnLostFocus(System.EventArgs e) { base.OnLostFocus(e); isSelected = false; this.Invalidate(); } protected override void OnClick(System.EventArgs e) { base.OnClick(e); this.Invalidate(); } protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) { base.OnKeyPress(e); if (!e.Handled) { switch (Convert.ToInt32(e.KeyChar)) { case (int)Keys.Enter: case (int)Keys.Space: { this.OnClick(null); break; } } } } protected override void OnResize(System.EventArgs e) { base.OnResize(e); if (m_bmpMemoire != null) { m_bmpMemoire.Dispose(); m_bmpMemoire = null; } this.Invalidate(); } protected override void OnEnabledChanged(System.EventArgs e) { base.OnEnabledChanged(e); this.Invalidate(); } } }
UCTip.designer.cs
namespace PTool { partial class UCTip { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.labelText = new System.Windows.Forms.Label(); this.SuspendLayout(); // // labelText // this.labelText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.labelText.Location = new System.Drawing.Point(0, 2); this.labelText.Name = "labelText"; this.labelText.Size = new System.Drawing.Size(147, 21); this.labelText.TextAlign = System.Drawing.ContentAlignment.TopCenter; // // UCTip // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.BackColor = System.Drawing.SystemColors.Window; this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Controls.Add(this.labelText); this.Name = "UCTip"; this.Size = new System.Drawing.Size(148, 24); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Label labelText; } }
UCTip.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace PTool { public partial class UCTip : UserControl { public UCTip() { InitializeComponent(); } public string Text { get { return labelText.Text; } set { labelText.Text = value; this.Invalidate(); } } public event EventHandler Close; protected void OnClose(EventArgs e) { if (this.Close != null) { this.Close(this, e); } } } }
UCToolBar.designer.cs
namespace PTool { partial class UCToolBar { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.imageLink6 = new PTool.ImageLinkBar(); this.imageLink5 = new PTool.ImageLinkBar(); this.imageLink4 = new PTool.ImageLinkBar(); this.imageLink3 = new PTool.ImageLinkBar(); this.imageLink2 = new PTool.ImageLinkBar(); this.imageLink1 = new PTool.ImageLinkBar(); this.SuspendLayout(); // // imageLink6 // this.imageLink6.Location = new System.Drawing.Point(200, 0); this.imageLink6.Name = "imageLink6"; this.imageLink6.Size = new System.Drawing.Size(40, 40); this.imageLink6.TabIndex = 5; this.imageLink6.Text = "imageLink6"; this.imageLink6.Click += new System.EventHandler(this.imageLink6_Click); this.imageLink6.MouseDown += new System.Windows.Forms.MouseEventHandler(this.common_MouseDown); this.imageLink6.MouseUp += new System.Windows.Forms.MouseEventHandler(this.common_MouseUp); // // imageLink5 // this.imageLink5.Location = new System.Drawing.Point(160, 0); this.imageLink5.Name = "imageLink5"; this.imageLink5.Size = new System.Drawing.Size(40, 40); this.imageLink5.TabIndex = 4; this.imageLink5.Text = "imageLink5"; this.imageLink5.Click += new System.EventHandler(this.imageLink5_Click); this.imageLink5.MouseDown += new System.Windows.Forms.MouseEventHandler(this.common_MouseDown); this.imageLink5.MouseUp += new System.Windows.Forms.MouseEventHandler(this.common_MouseUp); // // imageLink4 // this.imageLink4.Location = new System.Drawing.Point(120, 0); this.imageLink4.Name = "imageLink4"; this.imageLink4.Size = new System.Drawing.Size(40, 40); this.imageLink4.TabIndex = 3; this.imageLink4.Text = "imageLink4"; this.imageLink4.Click += new System.EventHandler(this.imageLink4_Click); this.imageLink4.MouseDown += new System.Windows.Forms.MouseEventHandler(this.common_MouseDown); this.imageLink4.MouseUp += new System.Windows.Forms.MouseEventHandler(this.common_MouseUp); // // imageLink3 // this.imageLink3.Location = new System.Drawing.Point(80, 0); this.imageLink3.Name = "imageLink3"; this.imageLink3.Size = new System.Drawing.Size(40, 40); this.imageLink3.TabIndex = 2; this.imageLink3.Text = "imageLink3"; this.imageLink3.Click += new System.EventHandler(this.imageLink3_Click); this.imageLink3.MouseDown += new System.Windows.Forms.MouseEventHandler(this.common_MouseDown); this.imageLink3.MouseUp += new System.Windows.Forms.MouseEventHandler(this.common_MouseUp); // // imageLink2 // this.imageLink2.Location = new System.Drawing.Point(40, 0); this.imageLink2.Name = "imageLink2"; this.imageLink2.Size = new System.Drawing.Size(40, 40); this.imageLink2.TabIndex = 1; this.imageLink2.Text = "imageLink2"; this.imageLink2.Click += new System.EventHandler(this.imageLink2_Click); this.imageLink2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.common_MouseDown); this.imageLink2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.common_MouseUp); // // imageLink1 // this.imageLink1.Location = new System.Drawing.Point(0, 0); this.imageLink1.Name = "imageLink1"; this.imageLink1.Size = new System.Drawing.Size(40, 40); this.imageLink1.TabIndex = 0; this.imageLink1.Text = "imageLink1"; this.imageLink1.Click += new System.EventHandler(this.imageLink1_Click); this.imageLink1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.common_MouseDown); this.imageLink1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.common_MouseUp); // // UCToolBar // this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.Controls.Add(this.imageLink6); this.Controls.Add(this.imageLink5); this.Controls.Add(this.imageLink4); this.Controls.Add(this.imageLink3); this.Controls.Add(this.imageLink2); this.Controls.Add(this.imageLink1); this.Name = "UCToolBar"; this.Size = new System.Drawing.Size(240, 40); this.Paint += new System.Windows.Forms.PaintEventHandler(this.UCToolBar_Paint); this.ResumeLayout(false); } #endregion private ImageLinkBar imageLink1; private ImageLinkBar imageLink2; private ImageLinkBar imageLink3; private ImageLinkBar imageLink4; private ImageLinkBar imageLink5; private ImageLinkBar imageLink6; } }
UCToolBar.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace PTool { public partial class UCToolBar : UserControl { private IImage imageIcon1; private IImage imageIcon2; private IImage imageIcon3; private IImage imageIcon4; private IImage imageIcon5; private IImage imageIcon6; private IImage imageBackActive; private IImage imageBack; private bool buttonVisiable1 = true; private bool buttonVisiable2 = true; private bool buttonVisiable3 = true; private bool buttonVisiable4 = true; private bool buttonVisiable5 = true; private bool buttonVisiable6 = true; private Point[] pointsPage = new Point[6]; private UCTip ucTip = new UCTip(); private Timer timer1 = new Timer(); private bool track = false; private DateTime downTime; private ImageLinkBar activeLink = new ImageLinkBar(); public UCToolBar() { InitializeComponent(); ucTip.Hide(); pointsPage[0] = this.imageLink1.Location; pointsPage[1] = this.imageLink2.Location; pointsPage[2] = this.imageLink3.Location; pointsPage[3] = this.imageLink4.Location; pointsPage[4] = this.imageLink5.Location; pointsPage[5] = this.imageLink6.Location; timer1.Interval = 500; timer1.Tick += new System.EventHandler(timer1_Tick); } public IImage ImageIcon1 { get { return imageIcon1; } set { imageIcon1 = value; this.Invalidate(); } } public IImage ImageIcon2 { get { return imageIcon2; } set { imageIcon2 = value; this.Invalidate(); } } public IImage ImageIcon3 { get { return imageIcon3; } set { imageIcon3 = value; this.Invalidate(); } } public IImage ImageIcon4 { get { return imageIcon4; } set { imageIcon4 = value; this.Invalidate(); } } public IImage ImageIcon5 { get { return imageIcon5; } set { imageIcon5 = value; this.Invalidate(); } } public IImage ImageIcon6 { get { return imageIcon6; } set { imageIcon6 = value; this.Invalidate(); } } public IImage ImageBackActive { get { return imageBackActive; } set { imageBackActive = value; this.Invalidate(); } } public IImage ImageBack { get { return imageBack; } set { imageBack = value; this.Invalidate(); } } public bool ButtonEnable1 { get { return imageLink1.Enabled; } set { imageLink1.Enabled = value; this.Invalidate(); } } public bool ButtonEnable2 { get { return imageLink2.Enabled; } set { imageLink2.Enabled = value; this.Invalidate(); } } public bool ButtonEnable3 { get { return imageLink3.Enabled; } set { imageLink3.Enabled = value; this.Invalidate(); } } public bool ButtonEnable4 { get { return imageLink4.Enabled; } set { imageLink4.Enabled = value; this.Invalidate(); } } public bool ButtonEnable5 { get { return imageLink5.Enabled; } set { imageLink5.Enabled = value; this.Invalidate(); } } public bool ButtonEnable6 { get { return imageLink6.Enabled; } set { imageLink6.Enabled = value; this.Invalidate(); } } public bool ButtonVisiable1 { get { return buttonVisiable1; } set { buttonVisiable1 = value; this.Invalidate(); } } public bool ButtonVisiable2 { get { return buttonVisiable2; } set { buttonVisiable2 = value; this.Invalidate(); } } public bool ButtonVisiable3 { get { return buttonVisiable3; } set { buttonVisiable3 = value; this.Invalidate(); } } public bool ButtonVisiable4 { get { return buttonVisiable4; } set { buttonVisiable4 = value; this.Invalidate(); } } public bool ButtonVisiable5 { get { return buttonVisiable5; } set { buttonVisiable5 = value; this.Invalidate(); } } public bool ButtonVisiable6 { get { return buttonVisiable6; } set { buttonVisiable6 = value; this.Invalidate(); } } public string ButtonText1 { get { return imageLink1.Text; } set { imageLink1.Text = value; this.Invalidate(); } } public string ButtonText2 { get { return imageLink2.Text; } set { imageLink2.Text = value; this.Invalidate(); } } public string ButtonText3 { get { return imageLink3.Text; } set { imageLink3.Text = value; this.Invalidate(); } } public string ButtonText4 { get { return imageLink4.Text; } set { imageLink4.Text = value; this.Invalidate(); } } public string ButtonText5 { get { return imageLink5.Text; } set { imageLink5.Text = value; this.Invalidate(); } } public string ButtonText6 { get { return imageLink6.Text; } set { imageLink6.Text = value; this.Invalidate(); } } public event EventHandler ButtonClick1; public event EventHandler ButtonClick2; public event EventHandler ButtonClick3; public event EventHandler ButtonClick4; public event EventHandler ButtonClick5; public event EventHandler ButtonClick6; private void UCToolBar_Paint(object sender, PaintEventArgs e) { Graphics buffer = e.Graphics; if (SystemConfig.IsWM65) //WM6.5 手机处理方式 { GradientStyle.Fill( buffer, this.ClientRectangle, System.Drawing.SystemColors.Control, System.Drawing.SystemColors.WindowFrame, GradientStyle.FillDirection.TopToBottom); } else { GradientStyle.Fill( buffer, this.ClientRectangle, System.Drawing.SystemColors.Window, System.Drawing.SystemColors.ActiveCaption, GradientStyle.FillDirection.TopToBottom); } buffer.Dispose(); } public void UpdateControl() { if (buttonVisiable1) { imageLink1.ImageIcon = imageIcon1; imageLink1.ImageBackActive = imageBackActive; imageLink1.ImageBack = imageBack; imageLink1.Show(); } else { imageLink1.Hide(); } if (buttonVisiable2) { imageLink2.ImageIcon = imageIcon2; imageLink2.ImageBackActive = imageBackActive; imageLink2.ImageBack = imageBack; imageLink2.Show(); } else { imageLink2.Hide(); } if (buttonVisiable3) { imageLink3.ImageIcon = imageIcon3; imageLink3.ImageBackActive = imageBackActive; imageLink3.ImageBack = imageBack; imageLink3.Show(); } else { imageLink3.Hide(); } if (buttonVisiable4) { imageLink4.ImageIcon = imageIcon4; imageLink4.ImageBackActive = imageBackActive; imageLink4.ImageBack = imageBack; imageLink4.Show(); } else { imageLink4.Hide(); } if (buttonVisiable5) { imageLink5.ImageIcon = imageIcon5; imageLink5.ImageBackActive = imageBackActive; imageLink5.ImageBack = imageBack; imageLink5.Show(); } else { imageLink5.Hide(); } if (buttonVisiable6) { imageLink6.ImageIcon = imageIcon6; imageLink6.ImageBackActive = imageBackActive; imageLink6.ImageBack = imageBack; imageLink6.Show(); } else { imageLink6.Hide(); } SetPosition(); } private void SetPosition() { int i = 0; if (imageLink1.Visible) { imageLink1.Location = pointsPage[i]; i++; } if (imageLink2.Visible) { imageLink2.Location = pointsPage[i]; i++; } if (imageLink3.Visible) { imageLink3.Location = pointsPage[i]; i++; } if (imageLink4.Visible) { imageLink4.Location = pointsPage[i]; i++; } if (imageLink5.Visible) { imageLink5.Location = pointsPage[i]; i++; } if (imageLink6.Visible) { imageLink6.Location = pointsPage[i]; i++; } } private void imageLink1_Click(object sender, EventArgs e) { if (this.ButtonClick1 != null) { this.ButtonClick1(this, e); } } private void imageLink2_Click(object sender, EventArgs e) { if (this.ButtonClick2 != null) { this.ButtonClick2(this, e); } } private void imageLink3_Click(object sender, EventArgs e) { if (this.ButtonClick3 != null) { this.ButtonClick3(this, e); } } private void imageLink4_Click(object sender, EventArgs e) { if (this.ButtonClick4 != null) { this.ButtonClick4(this, e); } } private void imageLink5_Click(object sender, EventArgs e) { if (this.ButtonClick5 != null) { this.ButtonClick5(this, e); } } private void imageLink6_Click(object sender, EventArgs e) { if (this.ButtonClick6 != null) { this.ButtonClick6(this, e); } } private void common_MouseDown(object sender, MouseEventArgs e) { track = true; activeLink = (ImageLinkBar)sender; downTime = DateTime.Now; timer1.Enabled = true; } private void common_MouseUp(object sender, MouseEventArgs e) { timer1.Enabled = false; track = false; ucTip.Hide(); } private void timer1_Tick(object sender, EventArgs e) { if (track && DateTime.Now.Subtract(downTime).TotalSeconds > 0.3 && activeLink != null) { timer1.Enabled = false; if (ucTip.Parent == null) this.TopLevelControl.Controls.Add(ucTip); ucTip.BringToFront(); ucTip.Text = activeLink.Text; ucTip.Location = new Point((this.Width - ucTip.Width) / 2, this.Top - ucTip.Height - 2 * 2); ucTip.Show(); } } } }
代码如有问题,欢迎指正! 呵呵,希望我还记得代码的细节,今天纯粹复制粘贴而已!
如果代码构建有问题,欢迎留言,有时间我一定回复!
相关文章推荐
- Caffe源码理解(2)——超级完整版教程:如何自定义一个新的层结构并重新编译Caffe
- Larbin 搜索引擎源码赏析——(六)一个自定义的数组模板
- VB6 如何添加自定义函数 模块 把代码放到一个模块中
- (译)如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔。(第二部分)
- 如何把一个包含自定义对象的数组序列化到磁盘
- [源码+注释]如何让一个女孩子穿超短裤?
- Windows8如何自定义更新部分补丁选择想要更新的重要补丁
- 如何使用kobold2d来做一个简单的iphone游戏教程(第二部分)
- 如何使用Css样式自定义一个DataGrid的风格
- 如何将图片中的一个任意四边形区域的图像转化为矩形【附源码】
- 如何将spring源码作为导入eclipse中,变成一个普通的项目(git、github)
- 关于如何使用VS高亮显示无扩展名源码文件的一个小技巧
- (译)如何制作一个类似tiny wings的游戏:第二部分(完) - 子龙山人 ...
- 贡献一个sharpmap,自定义专题地图源码
- 如何使用cocos2d-x3.0制作一个滑动图片游戏:第二部分
- 如何构建一个类似Instagram的照片共享应用程序的HTML5:第2部分
- <一>Ioc控制反转——手把手教学:如何配置并使用单元测试一个spring框架(附源码)
- 如何判断一个Http Message的结束——python源码解读
- extjs实现选择多表自定义查询功能 前台部分(ext源码)
- 如何自定义FragmentTabHost中某一个Tab的点击效果