我想做一个ImageButton控件,就是先显示一张图片,鼠标放上去时显示另一张,离开时还原。现在要写两个属性 ImagePath1和ImagePath2,我想控件做好后,在设置这两个属性时,用文件选择框去选择文件,但只是存储文件路径,不是存储文件(ImagePath1 and 2 不能用Image类型)请问这两个属性怎么写呢?请高人指定一下,谢谢。
像这样行是行,可是文件存储在控件里了。
private Image m_Image;
[Localizable(true), Description("项图片"), Category("Appearance")]
public Image Image
{
set
{
this.m_Image = value;
} get
{
return this.m_Image;
}
}

解决方案 »

  1.   

    我也有过类似的需求,源码给你,下面两个类编译后即可使用using System;
    using System.Drawing.Design;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
    using System.ComponentModel;
    using System.ComponentModel.Design;namespace WindowsApplication2
    {
    /// <summary>
    /// ImageButton 的摘要说明。
    /// </summary>
    public class ImageButton : Control
    {
    private string m_imagePath1 = "";
    private string m_imagePath2 = ""; private bool m_mouseHover = false; public ImageButton()
    {
                this.SetStyle(ControlStyles.ResizeRedraw,true);
    this.SetStyle(ControlStyles.DoubleBuffer,true);
    this.SetStyle(ControlStyles.UserPaint,true);
    this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
    } [Editor(typeof(FilePathEditor),typeof(UITypeEditor))]
    public string ImagePath1
    {
    get
    {
                    return m_imagePath1;
    }
    set
    {
    m_imagePath1 = value;
    this.Invalidate();
    }
    } public string ImagePath2
    {
    get
    {
    return m_imagePath2;
    }
    set
    {
    m_imagePath2 = value;
    this.Invalidate();
    }
    } protected override void OnMouseEnter(EventArgs e)
    {
    base.OnMouseEnter (e);
    m_mouseHover = true;
    this.Invalidate();
    } protected override void OnMouseLeave(EventArgs e)
    {
    base.OnMouseLeave (e);
    m_mouseHover = false;
    this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint (e); if (!m_mouseHover)
    {
    if (m_imagePath1 != null && m_imagePath1.Length > 0 && File.Exists(m_imagePath1))
    {
    try
    {
    Image image = Image.FromFile(m_imagePath1);
    if (image != null)
    {
    e.Graphics.DrawImage(image,0,0,image.Width,image.Height);
    }
    }
    catch
    {
    }
    }
    }
    else
    {
    if (m_imagePath2 != null && m_imagePath2.Length > 0 && File.Exists(m_imagePath2))
    {
    try
    {
    Image image = Image.FromFile(m_imagePath2);
    if (image != null)
    {
    e.Graphics.DrawImage(image,0,0,image.Width,image.Height);
    }
    }
    catch
    {
    }
    }
    }
    } }
    }
    ///////////////////////////////////////////////////////
    ////////////////////////////////////////////////////
    using System;
    using System.Windows.Forms;
    using System.Drawing.Design;namespace WindowsApplication2.Design
    {
    /// <summary>
    /// UITypeEditor 的摘要说明。
    /// </summary>
    public class FilePathEditor : UITypeEditor
    {
    public FilePathEditor()
    {

    } public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    {
    return UITypeEditorEditStyle.Modal;
    } public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.ShowDialog();
    return ofd.FileName;
    }
    }
    }
      

  2.   

    private string str_Image;public string Image_Path
    {
    set
    {
    this.str_Image = value;
    }get
    {
    return this.str_Image;
    }
    }
      

  3.   

    谢谢,sjshhzh(老孙头) ,搞定了,非常感谢。因为只有20分,少了点,只有全部给sjshhzh了,大家别介意啊。