一般ERP系统中单据打印,涉及文字较多的内容,比如品名规格、备注,会需要自动缩小字体,而且要自动换行,这样才能尽量把文字显示的清楚一些。很多报表工具,有直接这样的功能可以使用,但是如果不是用第三方报表组件,而是自己控制打印,就比较麻烦。通过努力也实现了百分之90的效果了:
而且,格式能够自适应纸张,根据用户选择的纸张去调整内容位置,和表格打印行数,这样就能满足更多的打印需求。
计算打印高度的代码:
//计算字符串在固定宽度内打印需要的高度,高度不够的话,循环缩小字体测试
//直到打印高度够,后续就用该字体打印
//本函数是计算高度的核心
UINT GdiPrint::GetHeight(CString m_str,UINT width,int& width_all)
{
UINT m_height;//返回总高度
UINT line_height=0;//行高
UINT line_height_sum=0;//累计行高
UINT width_sum = 0;//累计宽度
int str_length = m_str.GetLength();//字符个数
CSize size_temp;//单个字符的尺寸
CString str_single;//单个字符
width_all = 0;//实际需要的打印宽度
for(int i=0;i<str_length;i++)
{
str_single.Format(_T("%c"),m_str.GetAt(i));
size_temp = pDC->GetTextExtent(str_single,1);//获取单个字符的高和宽
width_sum += size_temp.cx;//累计宽度
width_all += size_temp.cx;
//如果累计宽度大于单元格宽度,则行数增加一
if(width_sum>width)
{
width_sum = size_temp.cx;//重置宽度
line_height_sum = line_height_sum + line_height;
line_height = 0;//
}
//获取单个字符的最大高度
line_height = (line_height>size_temp.cy)?line_height:size_temp.cy;
}
m_height = line_height_sum + line_height;
return m_height;
}
系统下载地址:huaxunsoft。com