2012-06-05 51 views
0

我想讓笑臉盒像生活信使一樣。C#Winform - 喜歡信使的笑臉盒

Smiley box

我怎樣才能做到這一點?我想獲取選定的圖片?

謝謝?

+0

我認爲你必須爲它定義一個新的Form,其上有一個PictureBox Grid和一個「ImageSelected」事件。 – cansik

+0

如果我使用新形式,我如何在主按鈕上顯示它? –

+0

具有View = Tile的ListView已關閉。你需要OwnerDraw才能得到它。 –

回答

2

我會使用ToolStripToolStripSplitButton控制,然後我會創建自己的控件inhrited形式的面板顯示包含在圖片框中的微笑何時會發生點擊ToolStripSplitButton。

我試圖模擬你想要做的行爲。因此,這裏是一些代碼:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     this.SuspendLayout(); 

     List<Bitmap> Smiles = new List<Bitmap>(); //Add images 

     ToolStripSplitButton _toolStripSplitButton = new ToolStripSplitButton(); 
     _toolStripSplitButton.Size = new Size(23, 23); 
     //_toolStripSplitButton.Image = myImage; //Add the image of the stripSplitButton 

     ToolStrip _toolStrip = new ToolStrip(); 
     _toolStrip.Size = new Size(ClientSize.Width, 10); 
     _toolStrip.Location = new Point(0, this.ClientSize.Height - _toolStrip.Height); 
     _toolStrip.BackColor = Color.LightGray; 
     _toolStrip.Dock = DockStyle.Bottom; 
     _toolStrip.Items.AddRange(new ToolStripItem[] { _toolStripSplitButton }); 

     SmileBox smilebox = new SmileBox(new Point(_toolStripSplitButton.Bounds.Location.X, _toolStrip.Location.Y - 18), 6); 
     smilebox.Visible = false; 

     Controls.Add(smilebox); 

     foreach (Bitmap bmp in Smiles) 
      smilebox.AddItem(bmp); 

     _toolStripSplitButton.Click += new EventHandler(delegate(object sender, EventArgs e) 
     { 
      smilebox.Visible = true; 
     }); 

     Click += new EventHandler(delegate(object sender, EventArgs e) 
     { 
      smilebox.Visible = false; 
     }); 

     this.Controls.Add(_toolStrip); 
     this.ResumeLayout(); 
    } 

    void Form1_Click(object sender, EventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 
} 

class SmileBox : Panel 
{ 
    public List<Item> Items 
    { 
     get; 
     set; 
    } 

    Size _ItemSpace = new Size(20, 20); 
    Point _ItemLocation; 
    int _rowelements = 0; 

    public SmileBox(Point Location, int RowElements) 
    { 
     BackColor = Color.LightGray; 

     Height = _ItemSpace.Height; 
     Width = _ItemSpace.Width * RowElements; 

     this.Location = new Point(Location.X, Location.Y - Height); 
     _ItemLocation = new Point(0, 0); 
     _rowelements = RowElements; 
    } 

    int count = 1; 
    public void AddItem(Bitmap Image) 
    { 
     Item item = new Item(_ItemSpace, _ItemLocation, Image); 

     if (_ItemLocation.X + _ItemSpace.Width >= Width) 
      _ItemLocation = new Point(0, _ItemLocation.Y); 
     else 
      _ItemLocation = new Point(_ItemLocation.X + _ItemSpace.Width, _ItemLocation.Y); 

     if (count == _rowelements) 
     { 
      _ItemLocation = new Point(_ItemLocation.X, _ItemLocation.Y + _ItemSpace.Height); 
      Height += _ItemSpace.Height; 
      Location = new Point(Location.X, Location.Y - _ItemSpace.Height); 

      count = 0; 
     } 

     count++; 

     Controls.Add(item); 
    } 
} 

class Item : PictureBox 
{ 
    int _BorderSpace = 2; 

    public Item(Size Size, Point Location, Bitmap Image) 
    { 
     this.Size = new Size(Size.Width - 2 * _BorderSpace, Size.Height - 2 * _BorderSpace); 
     this.Location = new Point(Location.X + _BorderSpace, Location.Y + _BorderSpace); 
     this.Image = new Bitmap(Image, this.ClientSize); 

     Click += new EventHandler(delegate(object sender, EventArgs e) 
     { 
      //Here what do you want to do when the user click on the smile 
     }); 

     MouseEnter += new EventHandler(delegate(object sender, EventArgs e) 
     { 
      Focus(); 
      Invalidate(); 
     }); 
    } 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     this.Focus(); 
     base.OnMouseDown(e); 
    } 

    protected override void OnEnter(EventArgs e) 
    { 
     this.Invalidate(); 
     base.OnEnter(e); 
    } 

    protected override void OnLeave(EventArgs e) 
    { 
     this.Invalidate(); 
     base.OnLeave(e); 
    } 

    protected override void OnPaint(PaintEventArgs pe) 
    { 
     base.OnPaint(pe); 

     if (this.Focused) 
     { 
      ClientRectangle.Inflate(-1, -1); 
      Rectangle rect = ClientRectangle; 
      ControlPaint.DrawFocusRectangle(pe.Graphics, rect); 
     } 
    } 
} 

這裏是什麼創建這個代碼的快照:

enter image description here

而當你過了一個微笑:

enter image description here

而且在這裏你可以點擊單個項目。

+0

當我點擊它,微笑框不開放? –

+0

對不起,我編輯了我的代碼...現在它應該可以工作。 –

+0

對不起,但它還沒有工作 –

0

創建一個用戶控件或一個新窗體(如果你想浮動的東西),每個表情符號的圖片框。每當用戶點擊一個圖片框(點擊事件)時,您可以註冊點擊並將笑臉粘貼到適當的位置。

實際上,您可以創建一個新的圖片框類,它繼承了內置圖片框的所有內容,並添加一個公共字符串,其中包含笑臉代碼。您在事件中獲取發件人對象,因此您只需在Click事件的函數中讀取該字符串即可。