2012-06-05 2 views
0

라이브 메신저처럼 스마일 박스를 만들고 싶습니다.C# Winform - 메신저와 같은 스마일 박스

Smiley box

내가 어떻게 할 수 있습니까? 선택한 사진을 가져 가고 싶습니까?

고마워요?

+0

"ImageSelected"이벤트와 함께 PictureBox Grid를 사용하여 새 양식을 정의해야한다고 생각합니다. – cansik

+0

새 양식으로 할 경우 어떻게해야 기본 단추에 표시 할 수 있습니까? –

+0

View = Tile 인 ListView가 닫습니다. OwnerDraw를 사용하면 정확하게 그걸 얻을 수 있습니다. –

답변

2

ToolStripToolStripSplitButton 컨트롤을 사용하고 나 자신의 컨트롤을 만들어서 그림 상자에 포함 된 미소를 보여주는 패널을 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 이벤트의 함수에서 해당 문자열을 읽는 것뿐입니다.