新建一个类,把我贴的代码封装在一起,然后另写个程序引用这个类,在工具箱里添加选择项,在组件选项页里用浏览按钮选择这个类,会出现RichTextBoxPrintCtrl这个控件,添加成功后把这个控件当成richtextbox用就行了。是原richtextbox的增强版,可以改变字体大小,改变颜色,还能导出到rtf文件中。
public class RichTextBoxPrintCtrl : RichTextBox
{
//Convert the unit used by the .NET framework (1/100 inch)
//and the unit used by Win32 API calls (twips 1/1440 inch)
private const double anInch = 14.4;
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CHARRANGE
{
public int cpMin; //First character of range (0 for start of doc)
public int cpMax; //Last character of range (-1 for end of doc)
}
[StructLayout(LayoutKind.Sequential)]
private struct FORMATRANGE
{
public IntPtr hdc; //Actual DC to draw on
public IntPtr hdcTarget; //Target DC for determining text formatting
public RECT rc; //Region of the DC to draw to (in twips)
public RECT rcPage; //Region of the whole DC (page size) (in twips)
public CHARRANGE chrg; //Range of text to draw (see earlier declaration)
}
private const int WM_USER = 0x0400;
private const int EM_FORMATRANGE = WM_USER + 57;
[DllImport("USER32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
// Render the contents of the RichTextBox for printing
//Return the last character printed + 1 (printing start from this point for next page)
public int Print(int charFrom, int charTo, PrintPageEventArgs e)
{
//Calculate the area to render and print
RECT rectToPrint;
rectToPrint.Top = (int)(e.MarginBounds.Top * anInch);
rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch);
rectToPrint.Left = (int)(e.MarginBounds.Left * anInch);
rectToPrint.Right = (int)(e.MarginBounds.Right * anInch);
//Calculate the size of the page
RECT rectPage;
rectPage.Top = (int)(e.PageBounds.Top * anInch);
rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch);
rectPage.Left = (int)(e.PageBounds.Left * anInch);
rectPage.Right = (int)(e.PageBounds.Right * anInch);
IntPtr hdc = e.Graphics.GetHdc();
FORMATRANGE fmtRange;
fmtRange.chrg.cpMax = charTo;//Indicate character from to character to
fmtRange.chrg.cpMin = charFrom;
fmtRange.hdc = hdc; //Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc; //Point at printer hDC
fmtRange.rc = rectToPrint; //Indicate the area on page to print
fmtRange.rcPage = rectPage; //Indicate size of page
IntPtr res = IntPtr.Zero;
IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam = IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
Marshal.StructureToPtr(fmtRange, lparam, false);
//Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);
//Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam);
//Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc);
//Return last + 1 character printer
return res.ToInt32();
}
}
#region RTF工具
public class RTFUtil
{
public static string RTFEncodeString(string inStr)
{
string t = System.Web.HttpUtility.UrlEncode(System.Text.UTF8Encoding.Default.GetBytes(inStr));
return t.Replace("%", "\\'").Replace("+","\\~");
}
public static string RTFEncodeColor(Color c)
{
byte r = c.R;
byte g = c.G;
byte b = c.B;
return "\\red" + r.ToString() + "\\green" + g.ToString() + "\\blue" + b.ToString() + ";";
}
}
#endregion
#region RTF颜色表
public class RTFColorTable : System.Collections.CollectionBase
{
public RTFColorTable()
{
List.Add(Color.Black);
List.Add(Color.White);
List.Add(Color.Red);
List.Add(Color.Blue);
List.Add(Color.Gray);
List.Add(Color.Purple);
}
public void Add(Color c)
{
if (!List.Contains(c))
List.Add(c);
}
public void Delete(Color c)
{
List.Remove(c);
}
public void Delete(int index)
{
List.RemoveAt(index);
}
public bool Contains(Color c)
{
return List.Contains(c);
}
public int IndexOf(Color c)
{
return List.IndexOf(c);
}
public string RTFSource
{
get
{
string r = "{\\colortbl ";
for (int iCnt = 0; iCnt < List.Count; iCnt++)
{
r += RTFUtil.RTFEncodeColor((Color)List[iCnt]);
}
r += "}";
return r;
}
}
}
#endregion
#region RTF字体表
public class RTFFontTable : System.Collections.CollectionBase
{
public RTFFontTable()
{
List.Add("宋体");
List.Add("FixedSys");
List.Add("Cordia New");
}
public void Add(string fName)
{
List.Add(fName);
}
public void Delete(string fName)
{
List.Remove(fName);
}
public bool Contains(string fName)
{
return List.Contains(fName);
}
public void Delete(int index)
{
List.RemoveAt(index);
}
public int IndexOf(string f)
{
return List.IndexOf(f);
}
public string RTFSource
{
get
{
string r = "{\\fonttbl";
for (int iCnt = 0; iCnt < List.Count; iCnt++)
{
r += "{\\f" + iCnt.ToString() + "\\fnil\\fprq6\\fcharset134 ";
r += RTFUtil.RTFEncodeString((string)List[iCnt]) + ";}";
}
r += "}";
return r;
}
}
}
#endregion
#region RTF文档类
public class RTFDocument
{
private string RTFHeader = "{\\rtf1\\ansi\\ansicpg936\\deff0\\deflang1033\\deflangfe2052";
private string RTFGenerator = "{\\*\\generator eglic.com;}";
public RTFColorTable Colors;
private RTFFontTable Fonts;
private string RTFContent = "";
public RTFDocument()
{
Colors = new RTFColorTable();
Fonts = new RTFFontTable();
}
public void AppendText(string sText, int iSize, Color cColor, string sFont,
bool bBlod, bool bItalic, bool bUnderLine, bool bNewLine)
{
if (!Colors.Contains(cColor))
Colors.Add(cColor);
if (!Fonts.Contains(sFont))
Fonts.Add(sFont);
string tmp = "";
if (cColor == Color.Gray || cColor == Color.Black)
{
tmp = RTFUtil.RTFEncodeString(sText+" ");
}
else
{
tmp = RTFUtil.RTFEncodeString(sText );
}
if (bBlod) tmp = "\\b" + tmp + "\\b0";
if (bUnderLine) tmp = "\\ul" + tmp + "\\ulnone";
if (bItalic) tmp = "\\i" + tmp + "\\i0";
int ci = Colors.IndexOf(cColor); if (ci < 1) ci = 0;
int fi = Fonts.IndexOf(sFont); if (fi < 0) fi = 0;
tmp = "\\fs" + iSize.ToString()+" " + tmp + "\\fs10";
tmp = "\\f" + fi.ToString() + tmp + "\\f0";
tmp = "\\cf" + ci.ToString() + tmp + "\\cf0";
if (bNewLine) tmp = "\\par "+tmp;
RTFContent += tmp;
}
public string RTFSource
{
get
{
return RTFHeader + this.Fonts.RTFSource + this.Colors.RTFSource + this.RTFGenerator + @"\viewkind4" + RTFContent + "}";
}
}
}
#endregion
具体的调用方法。
Alldj.RTFDocument rtf = new RTFDocument();
Color ctitle = Color.Red;
Color csubtitle = Color.Blue;
Color ctt = Color.Black;
Color cmtt = Color.Gray;
Color cstt = Color.Purple;
string fname = "宋体";
int ftitle = 40;
int fsubtitle = 30;
int ftt = 20;
string text="";
DataSet ds=new DataSet();
ds=Func.DBbind("select * from jkda_family where fid='"+fid+"' \n select * from personal a inner join jkda_familyMemberrelation b on a.pid=b.pid where b.fid='"+fid+"'");
//idcard,name,sex,birthday,nation,hostrelation,livestate
//district,street,hamlet,road,alley,number,room,host,homephone,createtime
if (ds.Tables[0].Rows.Count<=0)
{
return;
}
rtf.AppendText("社区家庭健康档案", ftitle, ctitle, fname, true, false, false, false);
try
{
text = "建档日期:";
rtf.AppendText(text, ftt, csubtitle, fname,false, false, false, true);
text = Convert.ToDateTime(ds.Tables[0].Rows[0]["createtime"].ToString()).ToLongDateString() + " " + Convert.ToDateTime(ds.Tables[0].Rows[0]["createtime"].ToString()).ToLongTimeString();
rtf.AppendText(text, ftt, ctt, fname, false, false, false, false);
text = "建档医生:";
rtf.AppendText(text, ftt, csubtitle, fname, false, false, false, true);
text = ds.Tables[0].Rows[0]["doctor"].ToString().Trim();
rtf.AppendText(text, ftt, ctt, fname, false, false, false, false);
text = "户主:";
rtf.AppendText(text, ftt, csubtitle, fname, false, false, false, true);
text = ds.Tables[0].Rows[0]["host"].ToString().Trim();
rtf.AppendText(text, ftt, ctt, fname, false, false, false, false);
string address="";
if (ds.Tables[0].Rows[0]["district"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["district"].ToString().Trim()+"区 ";
}
if (ds.Tables[0].Rows[0]["street"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["street"].ToString().Trim()+"街道 ";
}
if (ds.Tables[0].Rows[0]["hamlet"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["hamlet"].ToString().Trim()+"居委 ";
}
if (ds.Tables[0].Rows[0]["road"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["road"].ToString().Trim()+"路 ";
}
if (ds.Tables[0].Rows[0]["alley"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["alley"].ToString().Trim()+"弄 ";
}
if (ds.Tables[0].Rows[0]["alley"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["number"].ToString().Trim()+"号 ";
}
if (ds.Tables[0].Rows[0]["room"].ToString().Trim()!="")
{
address+=ds.Tables[0].Rows[0]["room"].ToString().Trim()+"室";
}
text = "家庭地址:";
rtf.AppendText(text, ftt, csubtitle, fname, false, false, false, false);
text = ds.Tables[0].Rows[0]["host"].ToString().Trim();
rtf.AppendText(address, ftt, ctt, fname, false, false, false, false);
text = "电话:";
rtf.AppendText(text, ftt, csubtitle, fname, false, false, false, false);
text = ds.Tables[0].Rows[0]["homephone"].ToString().Trim();
rtf.AppendText(text, ftt, ctt, fname, false, false, false, false);
text = "家庭成员";
rtf.AppendText(text, ftt,cstt, fname, false, false, false, true);
this.rtfamily.Rtf = rtf.RTFSource;
}
catch(Exception eee)
{
Func.Msg(eee.Message);
}
}
发表于 @ 2007年04月14日 10:06:00 | 评论( loading... ) | 举报| 收藏