• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Printing Problem in win32

## Recommended Posts

Hello again. I''m very near to finishing my Vampire Character Sheet Generator, but I have run into a problem that I just can''t figure out. I''m trying to print my sheet. When I select to print the sheet, the print dialog comes up, allowing me to select the printer and any options, but when it actually starts to print, it just feeds a page up, and the ink cartridge heads just pass over the very top of the page a few times quickly. It then ejects the page and does the process again with a new sheet. It will do this for as long as I let it run. I use the same function to draw to the screen as I''m using to draw to the printer. I''ve checked GetDeviceCaps on my printer (HP Deskjet 952C) and it says it supports stretchblt and bitmaps higher than 64k and everything I think I should need. Here is the code when the user selects to print the page: (The output page should only take up one page)
LRESULT CALLBACK SheetProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{

HDC hDC;

PAINTSTRUCT ps;
PRINTDLG pdlg;
DOCINFO docInfo;

switch(Msg)
{
case WM_PAINT:

hDC=BeginPaint(hWndDlg,&ps);

drawCharInfo(hWndDlg,&hDC);

EndPaint(hWndDlg,&ps);

break;
//etc... More regular working code...

case WM_COMMAND:
switch(wParam)
{
case SHEET_FILE_PRINT:
pdlg.lStructSize=sizeof(pdlg);
pdlg.hwndOwner=hWndDlg;
pdlg.hDevMode=NULL;
pdlg.hDevNames=NULL;
pdlg.Flags = PD_ALLPAGES|PD_HIDEPRINTTOFILE|PD_NOPAGENUMS|PD_RETURNDC;
if (PrintDlg(&pdlg)==FALSE){
MB("Cancelled");
return FALSE;
}
hDC=pdlg.hDC;

docInfo.cbSize=sizeof(docInfo);
docInfo.lpszDocName="Vampire Character Sheet";
docInfo.lpszOutput=NULL;
docInfo.lpszDatatype=NULL;

StartDoc(hDC, &docInfo);
StartPage(hDC);
drawCharInfo(hWndDlg, &hDC);
EndPage(hDC);
EndDoc(hDC);
break;
//etc...


and here is the code for drawCharInfo():
void drawCharInfo(HWND hWnd, HDC* hDC)
{
RECT infoRect;
RECT rect;
HDC hdcMem;
HBITMAP hbmOld;
BITMAP bm;
HFONT myFont;

GetClientRect(hWnd,&infoRect);

int cHeight=infoRect.bottom;
int cWidth=infoRect.right;
int y1=469;

//Create a desired font to use

hdcMem=CreateCompatibleDC(*hDC);
if (hdcMem==NULL){MB("Error creating memory device context");}

myFont=CreateFont(18,0,0,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,"Comic Sans MS");

SelectObject(hdcMem,myFont);

SelectObject(hdcMem,myBMP);

hbmOld=(HBITMAP)SelectObject(hdcMem, myBMP);

GetObject(myBMP, sizeof(bm), &bm);

SetBkMode(hdcMem,TRANSPARENT);

//Placing Character Name

rect.bottom=128;
rect.left=115;
rect.right=235;
rect.top=108;
DrawText(hdcMem, charName,-1,&rect,NULL);

//Placing character Nature

rect.bottom=135;
rect.left=315;
rect.right=400;
rect.top=105;
DrawText(hdcMem, nature,-1,&rect,NULL);

//Placing character Demeanor

rect.bottom=140;
rect.left=315;
rect.right=400;
rect.top=122;
DrawText(hdcMem, demeanor,-1,&rect,NULL);

//Placing character Clan

rect.bottom=158;
rect.left=315;
rect.right=400;
rect.top=139;
DrawText(hdcMem, clan,-1,&rect,NULL);

//Placing the character disciplines

rect.bottom=487;
rect.left=245;
rect.right=351;
rect.top=470;
DrawText(hdcMem, discipline[0],-1,&rect,NULL);

rect.bottom=501;
rect.left=245;
rect.right=351;
rect.top=484;
DrawText(hdcMem, discipline[1],-1,&rect,NULL);

rect.bottom=515;
rect.left=245;
rect.right=351;
rect.top=498;
DrawText(hdcMem, discipline[2],-1,&rect,NULL);

//Place the names of the owned backgrounds

for (int i=0;i<10;i++){
if (Background[i]>0){
rect.top=y1;
rect.left=77;
rect.right=180;
rect.bottom=rect.top+16;
DrawText(hdcMem,BackName[i],-1,&rect,NULL);
y1+=14;
}
}

//Fill in the appropriate dots for strength

SelectObject(hdcMem,GetStockObject(BLACK_BRUSH));

Attribute[0]=Physical;
Attribute[1]=Social;
Attribute[2]=Mental;

//Fill in dots for the attributes

for(int h=0;h<3;h++){
for (i=0;i<3;i++){
for (int j=2;j<6;j++){
if (Attribute[h][i]>=j){
Ellipse(hdcMem,ptPhysx+((j-2)*dotXoffset)+(h*columnXoffset),
ptPhysy+(i*dotYoffset),
ptPhysx+((j-2)*dotXoffset)+dotWidth+(h*columnXoffset),
ptPhysy+(i*dotYoffset)+dotHeight);
}
}
}
}

//Fill in dots for the abilities

Ability[0]=Talent;
Ability[1]=Skill;
Ability[2]=Knowledge;

for(h=0;h<3;h++){
for (i=0;i<10;i++){
for (int j=1;j<6;j++){
if (Ability[h][i]>=j){
Ellipse(hdcMem,ptAbilityx+((j-2)*dotXoffset)+(h*columnXoffset),
ptAbilityy+(i*dotYoffset),
ptAbilityx+((j-2)*dotXoffset)+dotWidth+(h*columnXoffset),
ptAbilityy+(i*dotYoffset)+dotHeight);
}
}
}
}

int numbgs=0;
//Fill in dots for the backgrounds

for (i=0;i<10;i++)
{
if (Background[i]>0){
for (int j=1;j<6;j++){
if (Background[i]>=j){
Ellipse(hdcMem,ptbgx+((j-2)*dotXoffset),
ptbgy+(numbgs*dotYoffset),
ptbgx+((j-2)*dotXoffset)+dotWidth,
ptbgy+(numbgs*dotYoffset)+dotHeight);
}
}
numbgs++;
}
}

//Put a dot into each discipline

for (i=0;i<3;i++){
Ellipse(hdcMem,ptdiscx-dotXoffset,ptdiscy+(i*dotYoffset),ptdiscx+dotWidth-dotXoffset,ptdiscy+(i*dotYoffset)+dotHeight);
}

//Put dots into virtues

for (int j=0;j<3;j++){
for (i=1;i<Virtue[j];i++){
Ellipse(hdcMem,ptv1x+((i-1)*dotXoffset),ptv1y+(j*vYoffset),ptv1x+((i-1)*dotXoffset)+dotWidth,ptv1y+(j*vYoffset)+dotHeight);
}
}

//Fill in humanity and then willpower

for(i=0;i<humanity;i++){
Ellipse(hdcMem,pthumx+(i*humXoffset),pthumy,pthumx+(i*humXoffset)+dotWidth+1,pthumy+dotHeight+1);
}

for(i=0;i<willpower;i++){
Ellipse(hdcMem,ptwillx+(i*humXoffset),ptwilly,ptwillx+(i*humXoffset)+dotWidth+1,ptwilly+dotHeight+1);
}

StretchBlt(*hDC,0,0,cWidth,cHeight,hdcMem,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);

SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);

}

Does anyone notice anything that could cause this sort of behavior?

##### Share on other sites
Maybe you''re drawing it too small. Consider the printer''s DPI vs. the monitor''s (also the mapping modes may be different, making things even worse).

##### Share on other sites
Yeah, I thought about that too, but then I wondered why it would keep printing page after page of it. My paper doesn''t get any ink on it either. That, and while I''m drawing my character info, I''m drawing it to a DC in memory and then strectchblitting it to the printer. So it should stretch it to fill a whole page, right?

##### Share on other sites
I found out that so many pages were being printed because of a silly error on my part. I still have the problem with it not printing anything...

I did change a couple of things though. I was setting the size on the printer to the same size as the client rect on the window, but now I have changed it to get the size of the paper with GetDeviceCaps(hDC,PHYSICALWIDTH); got the height the same way, but it still gives me the same problem. It just doesn''t print anything.

##### Share on other sites
Hmm, I think you need to offset your painting with the PHYSICALOFFSETX and PHYSICALOFFSETY and maybe calculate a scaling factor as well. E.g.
double ys = static_cast(GetDeviceCaps(hdc, PHYSICALHEIGHT)) / GetSystemMetrics(SM_CYSCREEN);

That all coordinates are multiplied by (ys will be 1.0 for the window dc).
I've written a small printing tutorial here (look in the Win32 programming tutorial) maybe it can help you.

[edited by - amag on March 19, 2004 9:24:13 AM]