描述:
就是说我编一个COM组件,在组件中输出接口函数,如PRINT
客户端只用调用我编的这个COM接口的PRINT函数实现打印
至于打印什么没关系,几行字也行,文件也行,我只想请教这怎么实现
最好给段简单的代码示例
解决方案1:
int get_line_height(HDC hdc)
{ //return text line height
//return value < 0 if map mode is not MM_TEXT, so that you can always use
//y+= line_height when drawing to device.
int map_mode = GetMapMode(hdc);
TEXTMETRIC tm;
GetTextMetrics(hdc,&tm);
int h = tm.tmHeight + tm.tmExternalLeading;
if(MM_TEXT != map_mode)
h = 0 - h;
return h;
}
double get_screen_pixel_width(int map_mode)
{ //purpose: a pixel displayed in MM_TEXT mode should be about the same size
//when displayed in other map mode.
HDC hdc = GetDC(NULL);
double hroz_size = GetDeviceCaps(hdc,HORZSIZE);//Width, in millimeters, of the physical screen.
double horz_res = GetDeviceCaps(hdc, HORZRES);//Width, in pixels, of the screen.
double pixel_width = hroz_size / horz_res; // Width, in millimeters
// 1 inch = 25.4 mm
const double INCH_TO_MM = 25.4;
const double INCH_TO_TWIP = 1440;
switch(map_mode)
{
case MM_LOMETRIC:
pixel_width *= 10;
break;
case MM_HIMETRIC:
pixel_width *= 100;
break;
case MM_TEXT:
break;
case MM_LOENGLISH: //Each logical unit is mapped to 0.01 inch
pixel_width = pixel_width / INCH_TO_MM * 100;
break;
case MM_HIENGLISH: //Each logical unit is mapped to 0.001 inch
pixel_width = pixel_width / INCH_TO_MM * 1000;
break;
case MM_TWIPS: //Each logical unit is mapped to one twentieth of a printer's point (1/1440 inch, also called a twip).
pixel_width = pixel_width / INCH_TO_MM * INCH_TO_TWIP;
break;
default:
break;
}
return pixel_width;//Width, in logical units according to the map_mode
}
void draw(HDC hdc)
{
int map_mode = MM_TWIPS;
LPCTSTR map_name = "MM_TWIPS";
SetMapMode(hdc,map_mode); //Each logical unit is mapped to 0.1 millimeter.
int x = 0;
int y = 0;
int line_h = get_line_height(hdc);
SelectObject(hdc,GetStockObject(BLACK_PEN));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc,x, y,map_name,strlen(map_name));
y += line_h;
LPCTSTR error_msg = "print to default printer from COM method";
TextOut(hdc,x,y,error_msg,strlen(error_msg));
}
STDMETHODIMP CTestObj::Test()
{
// print to default printer from COM method
// Onega(www.fruitfruit.com)
// VC6.0 Sp6, windows xp sp2
HDC hdc;
PAINTSTRUCT ps;
static PRINTDLG pd;
static DOCINFO dci;
static LPTSTR lpszDocName="Print Test";
ZeroMemory(&pd,sizeof(pd));
pd.lStructSize=sizeof(pd);
pd.Flags=PD_RETURNDC|PD_HIDEPRINTTOFILE |PD_RETURNDEFAULT;
pd.nFromPage=1;
pd.nToPage=1;
pd.nMinPage=1;
pd.nMaxPage=1;
pd.nCopies=1;
dci.cbSize=sizeof(dci);
dci.lpszDocName=lpszDocName;
dci.lpszOutput=NULL;
dci.lpszDatatype=NULL;
dci.fwType=0;
if (!PrintDlg(&pd))
return E_FAIL;
hdc=pd.hDC;
StartDoc(hdc,&dci);
StartPage(hdc);
draw(hdc);
EndPage(hdc);
EndDoc(hdc);
return S_OK;
}
if you don't know how to print a bitmap, refer to
http://www.fruitfruit.com/vc/release/post/print.htm