C#實現微信聊天對話框


using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Sun.WinFormControl
{
/// <summary>
/// 類似微信的聊天對話框。
/// </summary>
/// <remarks>
/// Author:SunYujing
/// DateTime:2012-07-19
/// </remarks>
public class WxChartBox : Control
{
/// <summary>
/// 構造方法。
/// </summary>
public WxChartBox()
{
SetStyle(ControlStyles.DoubleBuffer, true); //雙緩沖防止重繪時閃爍
SetStyle(ControlStyles.AllPaintingInWmPaint, true); //忽略 WM_ERASEBKGND 窗口消息減少閃爍
SetStyle(ControlStyles.UserPaint, true); //自定義繪制控件內容
SetStyle(ControlStyles.SupportsTransparentBackColor, true); //模擬透明
SetStyle(ControlStyles.Selectable, false); //接收焦點
Size = new Size(500, 60); //初始大小
Font = new Font("微軟雅黑", 10);
}
/// <summary>
/// 用戶名。
/// </summary>
private string _username = "用戶名";
/// <summary>
/// 消息日期時間。
/// </summary>
private DateTime _messagetime = DateTime.Now;
/// <summary>
/// 消息內容。
/// </summary>
private string _messagecontent = "消息內容";
/// <summary>
/// 每行消息數據的字節數。
/// </summary>
private int _perlinebit = 68;
/// <summary>
/// 每行字符數。
/// </summary>
private int _perlinechar = 34;
/// <summary>
/// 消息內容的行高。
/// </summary>
private int _lineheight = 22;
/// <summary>
/// 背景圖高。
/// </summary>
private int _imageheight = 8;
/// <summary>
/// 背景圖寬。
/// </summary>
private int _imagewidth = 8;
/// <summary>
/// 消息類型。
/// </summary>
private MessageType _messagetype = MessageType.Reseave;
/// <summary>
/// 獲取或設置用戶名。
/// </summary>
[Description("獲取或設置用戶名。")]
public string UserName
{
get
{
return _username;
}
set
{
_username = value;
Invalidate(false);
}
}

/// <summary>
/// 獲取或設置用戶名。
/// </summary>
[Description("獲取或設置用戶名。")]
public DateTime MessageTime
{
get
{
return _messagetime;
}
set
{
_messagetime = value;
Invalidate(false);
}
}

/// <summary>
/// 獲取或設置消息內容。
/// </summary>
[Description("獲取或設置消息內容。")]
public string MessageContent
{
get
{
return _messagecontent;
}
set
{
_messagecontent = value;
Invalidate(false);
}
}

/// <summary>
/// 獲取或設置消息的類型。
/// </summary>
[Description("獲取或設置消息的類型。")]
public MessageType MType
{
get
{
return _messagetype;
}
set
{
_messagetype = value;
Invalidate(false);
}
}
/// <summary>
/// 自定義繪制。
/// </summary>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Width = 500;
Height = InitHeight();
DrawImage(g);
DrawText(g);
DrawLine(g);
DrawMessageContent(g);
}
/// <summary>
/// 繪制用戶名和消息時間。
/// </summary>
private void DrawText(Graphics g)
{
Font f = new Font("微軟雅黑", 10,FontStyle.Bold);
g.DrawString(UserName+" "+MessageTime.ToString("yyyy-MM-dd HH:mm:ss"), f, new SolidBrush(ForeColor), 8+_imagewidth, 2);
}

/// <summary>
/// 繪制一條直線。
/// </summary>
private void DrawLine(Graphics g)
{
Color color = Color.Green;
if(MType==MessageType.Reseave)
color = Color.Red;
Pen p = new Pen(color);
p.Width = 1;
g.DrawLine(p, 4 + _imagewidth, 22, Width - 8 - _imagewidth , 22);
}
/// <summary>
/// 繪制短信內容。
/// </summary>
private void DrawMessageContent(Graphics g)
{
int initheight = 22;
int rowscount = MessageLineCount();
string contents = MessageContent;
string content = "";
for (int i = 0; i < rowscount; i++)
{
if (contents.Length > _perlinechar)
{
content = contents.Substring(0, _perlinechar);
contents = contents.Remove(0, _perlinechar);
}
else
{
content = contents;
}
g.DrawString(content, Font, new SolidBrush(ForeColor), 4+_imagewidth, initheight + i * _lineheight);
}
}
/// <summary>
/// 繪制背景圖片。
/// </summary>
/// <param name="g"></param>
private void DrawImage(Graphics g)
{
//繪制左上角背景圖
g.DrawImage(Properties.Resources.ZSJ, _imagewidth, 0);
//繪制上邊框
g.DrawImage(Properties.Resources.HS, _imagewidth * 2, 0, Width - _imagewidth * 4, _imageheight);
//繪制右上角背景圖
g.DrawImage(Properties.Resources.YHJ, Width - _imagewidth * 2-3, 0);
//繪制右邊框
if (MType == MessageType.Send)
{
g.DrawImage(Properties.Resources.Y, Width - _imagewidth-1, _imageheight+1);
g.DrawImage(Properties.Resources.SY, Width - _imagewidth * 2, _imageheight * 2+2, _imagewidth, Height - _imageheight * 3-2);
}
else
{
g.DrawImage(Properties.Resources.SY, Width - _imagewidth * 2, _imageheight, _imagewidth, Height - _imageheight * 2);
}
//繪制右下角背景圖
g.DrawImage(Properties.Resources.YXJ, Width - _imagewidth * 2 - 3, Height - _imageheight-2);
//繪制下邊框
g.DrawImage(Properties.Resources.HX, _imagewidth * 2, Height - _imageheight, Width - _imagewidth * 4, _imageheight);
//繪制左下角背景圖
g.DrawImage(Properties.Resources.ZXJ, _imagewidth, Height - _imageheight-2);
//繪制左邊框
if (MType == MessageType.Reseave)
{
g.DrawImage(Properties.Resources.Z, 0, _imageheight+1);
g.DrawImage(Properties.Resources.SZ, _imagewidth, _imageheight * 2+2, _imagewidth, Height - _imageheight * 3-2);
}
else
{
g.DrawImage(Properties.Resources.SZ, _imagewidth, _imageheight, _imagewidth, Height - _imageheight * 2);
}
}
/// <summary>
/// 動態計算控件高度。
/// </summary>
/// <returns>控件高度。</returns>
public int InitHeight()
{
if(MessageLineCount()<2)
return 2 * _lineheight + 22;
else
return MessageLineCount() * _lineheight + 22;
}
/// <summary>
/// 獲取消息行數。
/// </summary>
/// <returns>消息行數。</returns>
private int MessageLineCount()
{
int MessageBits = System.Text.Encoding.Default.GetByteCount(MessageContent.Trim());
return (int)Math.Ceiling(MessageBits * 1.0 / _perlinebit);
}
}

/// <summary>
/// 消息類型。
/// </summary>
public enum MessageType
{
/// <summary>
/// 發送消息。
/// </summary>
Send,
/// <summary>
/// 接收消息。
/// </summary>
Reseave
}
}


免責聲明!

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



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