2011-09-12 38 views
6

Tôi đã tự hỏi làm thế nào tôi có thể làm điều này. Tôi biết tôi có thể sử dụng các thành phần nút nhưng nó có những thứ màu xám nhỏ xung quanh nó khi tôi cho nó một hình ảnh. Với nút hình ảnh làm thế nào tôi có thể hiển thị một hình ảnh cho hiệu ứng hoverLàm thế nào để làm cho nút hình ảnh

+1

Bạn đang nói về WinForms, WPF hoặc ASP.NET? – Alxandr

+0

Tôi đang nói về WinForms – Matthewj

+0

Tôi không có kỹ năng trong WinForms, nhưng tôi nghĩ bạn có thể biến biên giới và nền trên ImageButtons, và sau đó, có thể có một số sự kiện để di chuột hoặc thứ gì đó mà bạn có thể sử dụng để thay đổi hình ảnh. – Alxandr

Trả lời

15

Bạn muốn tạo một nút không có biên giới nhưng hiển thị hình ảnh khác nhau khi người dùng di chuột qua nó với chuột? Đây là cách bạn có thể làm điều đó:

  1. Thêm một điều khiển ImageList mẫu tại thêm hai hình ảnh, một cho sự xuất hiện bình thường của nút và một cho khi con chuột được lơ lửng trên.

  2. Thêm nút của bạn và thiết lập các thuộc tính sau:
    FlatStyle = Flat
    FlatAppearance.BorderColor (và có thể MouseOverBackColor & MouseDownBackColor) để màu nền của hình thức của bạn
    ImageList = ImageList bạn thêm vào biểu mẫu
    ImageIndex đến giá trị chỉ mục của hình ảnh bình thường của bạn

Mã MouseHover và MouseL các sự kiện dự báo cho nút như sau:

private void button1_MouseHover(object sender, EventArgs e) { 
    // ImageList index value for the hover image. 
    button1.ImageIndex = 1; 
} 

private void button1_MouseLeave(object sender, EventArgs e) { 
    // ImageList index value for the normal image. 
    button1.ImageIndex = 0; 
} 

Tôi tin rằng sẽ mang đến cho bạn hiệu ứng hình ảnh mà bạn đang tìm kiếm.

+0

Hoàn toàn hoàn hảo – motoDrizzt

+0

Math.pow (vote_up, current_up_votes * 2) –

1

Bạn có thể gán thuộc tính BackgroundImage cho nút. Bạn cũng có thể sử dụng các sự kiện OnMouseEnter và OnMouseExit để thay đổi nền theo yêu cầu của bạn.

Xem BackgroundImageOnMouseEnterOnMouseLeave

0

WinForm không có một ImageButton không giống như Web Forms. Thêm một hình ảnh vào một nút bình thường là có thể, nhưng không nhìn tốt trong một số trường hợp. Bạn có thể phải sử dụng nút điều khiển của bên thứ ba như WinForm ImageButton hoặc tạo CustomControl của riêng bạn.

1

tóm tắt nhỏ (Border, MouseDownBackColor, MouseOverBackColor)

FlatApperance

BorderColor = Đen hoặc những gì đã bao giờ bạn muốn
BorderSize = có thể được thiết lập để 0
MouseDownBackColor = Transparent
MouseOverBackColor = Transparent

0.123.

Text = none

Đối MouseDown:

private void button1_MouseDown(object sender, MouseEventArgs e) { // ImageList index value for the mouse down image. button1.ImageIndex = 2; }

0

tôi cũng cần một nút hình ảnh, nhưng tôi muốn có một cái như ToolstripMenuButton. Với đường viền và màu sắc chính xác khi di chuột. Vì vậy, tôi đã thực hiện quyền kiểm soát tùy chỉnh để thực hiện điều đó:

using System; 
using System.ComponentModel; 
using System.Windows.Forms; 

namespace LastenBoekInfrastructure.Controls.Controls 
{ 
    [DefaultEvent("Click")] 
    public class ImageButton : UserControl 
    { 
     public string ToolTipText 
     { 
      get { return _bButton.ToolTipText; } 
      set { _bButton.ToolTipText = value; } 
     } 

     public bool CheckOnClick 
     { 
      get { return _bButton.CheckOnClick; } 
      set { _bButton.CheckOnClick = value; } 
     } 

     public bool DoubleClickEnabled 
     { 
      get { return _bButton.DoubleClickEnabled; } 
      set { _bButton.DoubleClickEnabled = value; } 
     } 

     public System.Drawing.Image Image 
     { 
      get { return _bButton.Image; } 
      set { _bButton.Image = value; } 
     } 

     public new event EventHandler Click; 
     public new event EventHandler DoubleClick; 

     private ToolStrip _tsMain; 
     private ToolStripButton _bButton; 

     public ImageButton() 
     { 
      InitializeComponent(); 
     } 

     private void InitializeComponent() 
     { 
      var resources = new ComponentResourceManager(typeof(ImageButton)); 
      _tsMain = new ToolStrip(); 
      _bButton = new ToolStripButton(); 
      _tsMain.SuspendLayout(); 
      SuspendLayout(); 

      // 
      // tsMain 
      // 
      _tsMain.BackColor = System.Drawing.Color.Transparent; 
      _tsMain.CanOverflow = false; 
      _tsMain.Dock = DockStyle.Fill; 
      _tsMain.GripMargin = new Padding(0); 
      _tsMain.GripStyle = ToolStripGripStyle.Hidden; 
      _tsMain.Items.AddRange(new ToolStripItem[] { 
      _bButton}); 
      _tsMain.Location = new System.Drawing.Point(0, 0); 
      _tsMain.Name = "_tsMain"; 
      _tsMain.Size = new System.Drawing.Size(25, 25); 
      _tsMain.TabIndex = 0; 
      _tsMain.Renderer = new ImageButtonToolStripSystemRenderer(); 
      // 
      // bButton 
      // 
      _bButton.DisplayStyle = ToolStripItemDisplayStyle.Image; 
      _bButton.Image = ((System.Drawing.Image)(resources.GetObject("_bButton.Image"))); 
      _bButton.ImageTransparentColor = System.Drawing.Color.Magenta; 
      _bButton.Name = "_bButton"; 
      _bButton.Size = new System.Drawing.Size(23, 22); 
      _bButton.Click += bButton_Click; 
      _bButton.DoubleClick += bButton_DoubleClick; 
      // 
      // ImageButton 
      // 
      Controls.Add(_tsMain); 
      Name = "ImageButton"; 
      Size = new System.Drawing.Size(25, 25); 
      _tsMain.ResumeLayout(false); 
      _tsMain.PerformLayout(); 
      ResumeLayout(false); 
      PerformLayout(); 
     } 

     void bButton_Click(object sender, EventArgs e) 
     { 
      if (Click != null) 
      { 
       Click(this, e); 
      } 
     } 

     void bButton_DoubleClick(object sender, EventArgs e) 
     { 
      if(DoubleClick != null) 
      { 
       DoubleClick(this, e); 
      } 
     } 

     public class ImageButtonToolStripSystemRenderer : ToolStripSystemRenderer 
     { 
      protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e) 
      { 
       //base.OnRenderToolStripBorder(e); 
      } 
     } 
    } 
} 
Các vấn đề liên quan