C#實現微信聊天對話框


最近喜歡上了玩微信,覺得微信的了聊天對話框挺不錯的,於是自己也使用C#實現了一個類似的控件,代碼如下:

  1 using System;
  2 using System.ComponentModel;
  3 using System.Drawing;
  4 using System.Drawing.Drawing2D;
  5 using System.Windows.Forms;
  6 
  7 namespace Sun.WinFormControl
  8 {
  9     /// <summary>
 10     /// 類似微信的聊天對話框。
 11     /// </summary>
 12     /// <remarks>
 13     /// Author:SunYujing
 14     /// DateTime:2012-07-19
 15     /// </remarks>
 16     public class WxChartBox : Control
 17     {
 18         /// <summary>
 19         /// 構造方法。
 20         /// </summary>
 21         public WxChartBox()
 22         {
 23             SetStyle(ControlStyles.DoubleBuffer, true);                   //雙緩沖防止重繪時閃爍
 24             SetStyle(ControlStyles.AllPaintingInWmPaint, true);           //忽略 WM_ERASEBKGND 窗口消息減少閃爍
 25             SetStyle(ControlStyles.UserPaint, true);                      //自定義繪制控件內容
 26             SetStyle(ControlStyles.SupportsTransparentBackColor, true);   //模擬透明            
 27             SetStyle(ControlStyles.Selectable, false);                     //接收焦點
 28             Size = new Size(500, 60);                                      //初始大小
 29             Font = new Font("微軟雅黑", 10);
 30         }
 31         /// <summary>
 32         /// 用戶名。
 33         /// </summary>
 34         private string _username = "用戶名";
 35         /// <summary>
 36         /// 消息日期時間。
 37         /// </summary>
 38         private DateTime _messagetime = DateTime.Now;
 39         /// <summary>
 40         /// 消息內容。
 41         /// </summary>
 42         private string _messagecontent = "消息內容";
 43         /// <summary>
 44         /// 每行消息數據的字節數。
 45         /// </summary>
 46         private int _perlinebit = 68;
 47         /// <summary>
 48         /// 每行字符數。
 49         /// </summary>
 50         private int _perlinechar = 34;
 51         /// <summary>
 52         /// 消息內容的行高。
 53         /// </summary>
 54         private int _lineheight = 22;
 55         /// <summary>
 56         /// 背景圖高。
 57         /// </summary>
 58         private int _imageheight = 8;
 59         /// <summary>
 60         /// 背景圖寬。
 61         /// </summary>
 62         private int _imagewidth = 8;
 63         /// <summary>
 64         /// 消息類型。
 65         /// </summary>
 66         private MessageType _messagetype = MessageType.Reseave;
 67         /// <summary>
 68         /// 獲取或設置用戶名。
 69         /// </summary>
 70         [Description("獲取或設置用戶名。")]
 71         public string UserName
 72         {
 73             get
 74             {
 75                 return _username;
 76             }
 77             set
 78             {
 79                 _username = value;
 80                 Invalidate(false);
 81             }
 82         }
 83 
 84         /// <summary>
 85         /// 獲取或設置用戶名。
 86         /// </summary>
 87         [Description("獲取或設置用戶名。")]
 88         public DateTime MessageTime
 89         {
 90             get
 91             {
 92                 return _messagetime;
 93             }
 94             set
 95             {
 96                 _messagetime = value;
 97                 Invalidate(false);
 98             }
 99         }
100 
101         /// <summary>
102         /// 獲取或設置消息內容。
103         /// </summary>
104         [Description("獲取或設置消息內容。")]
105         public string MessageContent
106         {
107             get
108             {
109                 return _messagecontent;
110             }
111             set
112             {
113                 _messagecontent = value;
114                 Invalidate(false);
115             }
116         }
117 
118         /// <summary>
119         /// 獲取或設置消息的類型。
120         /// </summary>
121         [Description("獲取或設置消息的類型。")]
122         public MessageType MType
123         {
124             get
125             {
126                 return _messagetype;
127             }
128             set
129             {
130                 _messagetype = value;
131                 Invalidate(false);
132             }
133         }
134         /// <summary>
135         /// 自定義繪制。
136         /// </summary>
137         protected override void OnPaint(PaintEventArgs e)
138         {
139             base.OnPaint(e);
140             Graphics g = e.Graphics;
141             g.SmoothingMode = SmoothingMode.HighQuality;
142             g.PixelOffsetMode = PixelOffsetMode.HighQuality;
143             Width = 500;
144             Height = InitHeight();
145             DrawImage(g);
146             DrawText(g);
147             DrawLine(g);
148             DrawMessageContent(g);
149         }
150         /// <summary>
151         /// 繪制用戶名和消息時間。
152         /// </summary>
153         private void DrawText(Graphics g)
154         {
155             Font f = new Font("微軟雅黑", 10,FontStyle.Bold);
156             g.DrawString(UserName+"  "+MessageTime.ToString("yyyy-MM-dd HH:mm:ss"), f, new SolidBrush(ForeColor), 8+_imagewidth, 2);
157         }
158 
159         /// <summary>
160         /// 繪制一條直線。
161         /// </summary>
162         private void DrawLine(Graphics g)
163         {
164             Color color = Color.Green;
165             if(MType==MessageType.Reseave)
166                 color = Color.Red;
167             Pen p = new Pen(color);
168             p.Width = 1;
169             g.DrawLine(p, 4 + _imagewidth, 22, Width - 8 - _imagewidth , 22);
170         }
171         /// <summary>
172         /// 繪制短信內容。
173         /// </summary>
174         private void DrawMessageContent(Graphics g)
175         {
176             int initheight = 22;
177             int rowscount = MessageLineCount();
178             string contents = MessageContent;
179             string content = "";
180             for (int i = 0; i < rowscount; i++)
181             {
182                 if (contents.Length > _perlinechar)
183                 {
184                     content = contents.Substring(0, _perlinechar);
185                     contents = contents.Remove(0, _perlinechar);
186                 }
187                 else
188                 {
189                     content = contents;
190                 }
191                 g.DrawString(content, Font, new SolidBrush(ForeColor), 4+_imagewidth, initheight + i * _lineheight);
192             }
193         }
194         /// <summary>
195         /// 繪制背景圖片。
196         /// </summary>
197         /// <param name="g"></param>
198         private void DrawImage(Graphics g)
199         {
200             //繪制左上角背景圖
201             g.DrawImage(Properties.Resources.ZSJ, _imagewidth, 0);
202             //繪制上邊框
203             g.DrawImage(Properties.Resources.HS, _imagewidth * 2, 0, Width - _imagewidth * 4, _imageheight);
204             //繪制右上角背景圖
205             g.DrawImage(Properties.Resources.YHJ, Width - _imagewidth * 2-3, 0);
206             //繪制右邊框
207             if (MType == MessageType.Send)
208             {
209                 g.DrawImage(Properties.Resources.Y, Width - _imagewidth-1, _imageheight+1);
210                 g.DrawImage(Properties.Resources.SY, Width - _imagewidth * 2, _imageheight * 2+2, _imagewidth, Height - _imageheight * 3-2);
211             }
212             else
213             {
214                 g.DrawImage(Properties.Resources.SY, Width - _imagewidth * 2, _imageheight, _imagewidth, Height - _imageheight * 2);
215             }
216             //繪制右下角背景圖
217             g.DrawImage(Properties.Resources.YXJ, Width - _imagewidth * 2 - 3, Height - _imageheight-2);
218             //繪制下邊框
219             g.DrawImage(Properties.Resources.HX, _imagewidth * 2, Height - _imageheight, Width - _imagewidth * 4, _imageheight);
220             //繪制左下角背景圖
221             g.DrawImage(Properties.Resources.ZXJ, _imagewidth, Height - _imageheight-2);
222             //繪制左邊框
223             if (MType == MessageType.Reseave)
224             {
225                 g.DrawImage(Properties.Resources.Z, 0, _imageheight+1);
226                 g.DrawImage(Properties.Resources.SZ, _imagewidth, _imageheight * 2+2, _imagewidth, Height - _imageheight * 3-2);
227             }
228             else
229             {
230                 g.DrawImage(Properties.Resources.SZ, _imagewidth, _imageheight, _imagewidth, Height - _imageheight * 2);
231             }
232         }
233         /// <summary>
234         /// 動態計算控件高度。
235         /// </summary>
236         /// <returns>控件高度。</returns>
237         public int InitHeight()
238         {
239             if(MessageLineCount()<2)
240                 return 2 * _lineheight + 22;
241             else
242                 return MessageLineCount() * _lineheight + 22;
243         }
244         /// <summary>
245         /// 獲取消息行數。
246         /// </summary>
247         /// <returns>消息行數。</returns>
248         private int MessageLineCount()
249         {
250             int MessageBits = System.Text.Encoding.Default.GetByteCount(MessageContent.Trim());
251             return (int)Math.Ceiling(MessageBits * 1.0 / _perlinebit);
252         }
253     }
254 
255     /// <summary>
256     /// 消息類型。
257     /// </summary>
258     public enum MessageType
259     {
260         /// <summary>
261         /// 發送消息。
262         /// </summary>
263         Send,
264         /// <summary>
265         /// 接收消息。
266         /// </summary>
267         Reseave
268     }
269 }

最終實現結果如圖:

 資源文件中的圖片文件下載:http://files.cnblogs.com/sunyujing/%E8%B5%84%E6%BA%90%E6%96%87%E4%BB%B6%E5%9B%BE%E7%89%87.rar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM