離氵茖 dé 煙婲

你说             
叶子的离开     
是因为风的追逐   
还是树的不留恋 

  博客园 :: :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

picButton.cs文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Streamsea.Properties;

namespace Streamsea.Forms
{
    
/// <summary>
    
/// 图片按钮
    
/// </summary>

    public partial class ucPictureButton : UserControl
    
{
        
private bool bPressed;
        
private Image pressImg;
        
private Image backImg;
        
private Image disableImg;

        
private Image m_img;
        
private PictureBoxSizeMode m_sizemode = PictureBoxSizeMode.StretchImage;

        
/// <summary>
        
/// 构造函数
        
/// </summary>

        public ucPictureButton()
        
{
            
base.Font = new Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
        }


        
private Size _GetPreferredSize()
        
{
            
if (this.m_img == null)
            
{
                
return base.Size;
            }

            
return this.m_img.Size;
        }


        
private Rectangle _ImageRectangle
        
{
            
get
            
{
                Rectangle rectangle1 
= new Rectangle(0000);
                
if (this.m_img != null)
                
{
                    
switch (this.m_sizemode)
                    
{
                        
case PictureBoxSizeMode.Normal:
                            rectangle1.Size 
= this.m_img.Size;
                            
return rectangle1;

                        
case PictureBoxSizeMode.StretchImage:
                            rectangle1.Size 
= base.ClientSize;
                            
return rectangle1;

                        
case ((PictureBoxSizeMode)2):
                            
return rectangle1;

                        
case PictureBoxSizeMode.CenterImage:
                            
{
                                rectangle1.Size 
= this.m_img.Size;
                                Size size1 
= base.ClientSize;
                                rectangle1.X 
= (size1.Width - rectangle1.Width) / 2;
                                rectangle1.Y 
= (size1.Height - rectangle1.Height) / 2;
                                
return rectangle1;
                            }

                    }

                }

                
return rectangle1;
            }

        }


        
/// <summary>
        
/// 指示如何显示图像。
        
/// </summary>

        [DefaultValue(PictureBoxSizeMode.StretchImage)]
        
public PictureBoxSizeMode SizeMode
        
{
            
get
            
{
                
return this.m_sizemode;
            }

            
set
            
{
                
if (!Enum.IsDefined(typeof(PictureBoxSizeMode), value))
                
{
                    
throw new ArgumentException();
                }

                
if (this.m_sizemode != value)
                
{
                    
this.m_sizemode = value;
                    
base.Invalidate();
                }

            }

        }


        
/// <summary>
        
/// 背景图像
        
/// </summary>

        [DefaultValue((string)null)]
        
public Image BackImage
        
{
            
get return backImg; }
            
set
            
{
                backImg 
= value;
                m_img 
= backImg;
                
base.Invalidate();
            }

        }


        
/// <summary>
        
/// 按下时的图像
        
/// </summary>

        [DefaultValue((string)null)]
        
public Image PressImage
        
{
            
get return pressImg; }
            
set
            
{
                pressImg 
= value;
                
base.Invalidate();
            }

        }


        
/// <summary>
        
/// 不可用时的图像
        
/// </summary>

        [DefaultValue((string)null)]
        
public Image DisableImage
        
{
            
get return disableImg; }
            
set
            
{
                disableImg 
= value;
                m_img 
= disableImg;
                
base.Invalidate();
            }

        }


        
/// <summary>
        
/// 按钮文字
        
/// </summary>

        public string ButtonText
        
{
            
get
            
{
                
return base.Text;
            }

            
set
            
{
                
base.Text = value;
                
this.Invalidate();
            }

        }


        
/// <summary>
        
/// 获取或设置一个值,该值指示控件是否可以对用户交互作出响应。
        
/// </summary>

        public new bool Enabled
        
{
            
get return base.Enabled; }
            
set
            
{
                
base.Enabled = value;
                
this.Invalidate();
            }

        }

        
/// <summary>
        
/// 引发 System.Windows.Forms.Control.MouseDown 事件。
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnMouseDown(MouseEventArgs e)
        
{
            
this.bPressed = true;
            
this.Invalidate();
            
base.OnMouseDown(e);
        }

        
/// <summary>
        
/// 引发 System.Windows.Forms.Control.MouseUp 事件。
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnMouseUp(MouseEventArgs e)
        
{
            
this.bPressed = false;
            
this.Invalidate();
            
base.OnMouseUp(e);
        }


        
/// <summary>
        
/// 绘制控件的背景。
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnPaintBackground(PaintEventArgs e)
        
{
            Region region1 
= new Region(base.ClientRectangle);
            region1.Exclude(
this._ImageRectangle);
            e.Graphics.FillRegion(
new SolidBrush(this.Parent!=null?this.Parent.BackColor:Color.White), region1);
            region1.Dispose();
        }

        
/// <summary>
        
/// 引发 System.Windows.Forms.Control.Paint 事件。
        
/// </summary>
        
/// <param name="e"></param>

        protected override void OnPaint(PaintEventArgs e)
        
{
            
if (this.bPressed)
            
{
                
if (pressImg == null)
                
{
                    pressImg 
= Resources.PressImage;
                }

                m_img 
= pressImg;
            }

            
else if (this.Enabled)
            
{
                
if (backImg == null)
                
{
                    backImg 
= Resources.BackImage;
                }

                m_img 
= backImg;
            }

            
else
            
{
                
if (disableImg == null)
                
{
                    disableImg 
= Resources.DisableImage;
                }

                m_img 
= disableImg;
            }

            
            e.Graphics.DrawImage(
this.m_img, this._ImageRectangle, new Rectangle(00this.m_img.Width, this.m_img.Height), GraphicsUnit.Pixel);

            
if (this.ButtonText.Length > 0)
            
{
                SizeF size 
= e.Graphics.MeasureString(this.ButtonText, this.Font);
                e.Graphics.DrawString(
this.ButtonText, this.Font, new SolidBrush(Enabled?this.ForeColor:Color.Gray),
                    (
this.ClientSize.Width - size.Width) / 2, (this.ClientSize.Height - size.Height) / 2);
            }

            
//e.Graphics.DrawRectangle(new Pen(this.Parent != null ? this.Parent.BackColor : Color.White), 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
            base.OnPaint(e);
        }

        
/// <summary>
        
/// 引发 System.Windows.Forms.Control.Resize 事件。
        
/// </summary>
        
/// <param name="eventg"></param>

        protected override void OnResize(EventArgs eventg)
        
{
            
base.OnResize(eventg);
            
if ((this.m_sizemode == PictureBoxSizeMode.StretchImage) || (this.m_sizemode == PictureBoxSizeMode.CenterImage))
            
{
                
base.Invalidate();
            }

        }

    }

}

 picButton.Designer.cs

namespace Streamsea.Forms
{
    partial 
class ucPictureButton
    
{
        
/// <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);
        }


        
组件设计器生成的代码
    }

}


在项目属性的资源文件里添加3张图片,名字分别为 BackImage  DisableImage  PressImage,这是默认的图片,在设计和谐程序的时候也可以换用其他图片的。
posted on 2006-10-10 16:04  煙婲離氵茖  阅读(1296)  评论(3编辑  收藏  举报