May 08, 2024, 01:55:34 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


CBITMAP::Render question

Started by Haim, May 06, 2006, 02:18:59 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Haim

From looking at the RENDER class method of CBITMAP, it seems that RENDER can print the bitmap to a printer and not only to a screen device. As far as I understand this happens if the m_hprintdc member of the window contains a printer HDC.

Can someone explain this, or better still, show how I can RENDER a bitmap to the printer?

Ionic Wind Support Team

Use PrintWindow.

Open the sample imgview.src which shows how to scale an image.

Ionic Wind Support Team

Ionic Wind Support Team

Or if you are into low level windows coding, and have the source license.

The PrintBitmap function takes a handle to a printer DC, a handle to the bitmap, and a rendering rectangle.  The rendering rectangle is in printer device coordinates, not pixels.  The source to the PrintWindow function shows how to open the standard printer dialog to get the handle to a printer device context.

You can use the GetDeviceCaps windows API function with the printer DC to get the printers horizontal and vertical resolution.  For Aurora I use MM_ANISOTROPIC mapping mode with the printer so the image can be scaled from screen coordinates to printer coordinates.

Paul.
Ionic Wind Support Team

Haim

Thanks,
I do have the source license. That is why I raised the question about Render, in the first place.
How do I et the handle of a bitmap once I have loaded it from a file, using the loadfromfile method of the CBITMAP class method?
Is it the m_himage member of CBITMAP? - (I tried using m_himage but it did not work.)

Ionic Wind Support Team

For some reason my first reply dissapeared.

Yes m_hImage is the handle to the bitmap.   Before using PrintBitmap you need to prepare the printer for output by using the API functions StartDoc, StartPage, EndPage and EndDoc.    Otherwise the printer will just stare at you blindly and do nothing ;)

If you post the code you have so far we can give you a hand.

Paul.
Ionic Wind Support Team

Haim

Thanks,
OK here is the code. It is just a very basic test program for a Cprinter class I was trying to create.
I aplogize for it's being so untidy and inelegant.
If I succeed in printing images then I'll try to complete the class.

//Cprinter Class tester

#typedef UINT unsigned int
/****************************************** PRINTDLG CONSTANTS ***********************************************************/
#define PD_USEDEVMODECOPIES  0x40000
#define PD_ALLPAGES 0
#define PD_NOPAGENUMS  0x8
#define PD_HIDEPRINTTOFILE  0x100000
#define PD_NOSELECTION  0x4
#define PD_RETURNDC  0x100
#define PD_RETURNDEFAULT 0x400
#define DMORIENT_LANDSCAPE 2


/******************************************* STRUCTURES *****************************************************************/
struct DOCINFO
{
   DEF cbSize as INT;
   DEF lpszDocName as POINTER;
   DEF lpszOutput as POINTER;
   DEF lpszDataType as POINTER;
   DEF fwType as UINT;
}

struct PRINTDLG,1
{
    DEF lStructSize as UINT;
    DEF hwndOwner as UINT;
    DEF hDevMode as UINT;
    DEF hDevNames as UINT;
    DEF hDC as UINT;
    DEF Flags as UINT;
    DEF nFromPage as WORD;
    DEF nToPage as WORD;
    DEF nMinPage as WORD;
    DEF nMaxPage as WORD;
    DEF nCopies as WORD;
    DEF hInstance as UINT;
    DEF lCustData as UINT;
    DEF lpfnPrintHook as POINTER;
    DEF lpfnSetupHook as POINTER;
    DEF lpPrintTemplateName as POINTER;
    DEF lpSetupTemplateName as POINTER;
    DEF hPrintTemplate as UINT;
    DEF hSetupTemplate as UINT;
}
struct DEVNAMES,1 {
    DEF wDriverOffset as WORD;
    DEF wDeviceOffset as WORD;
    DEF wOutputOffset as WORD;
    DEF wDefault as WORD;
    // driver, device, and port name strings follow wDefault
}
/****************************************** API and External FUNCTION DECLARATIONS ***************************************/
DECLARE IMPORT,PrintDlg ALIAS PrintDlgA(pd as PRINTDLG),INT;
DECLARE IMPORT,ZeroMemory ALIAS RtlZeroMemory(pvoid as POINTER,length as INT),INT;
DECLARE EXTERN _fpreset();
DECLARE IMPORT,GlobalLock(hMem as UINT),POINTER;
DECLARE IMPORT,GlobalFree(hMem as UINT),INT;
DECLARE IMPORT,GlobalUnlock(hMem as UINT),INT;
DECLARE IMPORT,StartDoc ALIAS StartDocA(hdc as UINT,lpDocInfo as POINTER),INT;
DECLARE IMPORT,EndDoc(hdc as UINT),INT;
DECLARE IMPORT,StartPage(hdc as UINT),INT;
DECLARE IMPORT,EndPageA ALIAS EndPage(hdc as UINT),INT;
DECLARE IMPORT,DeleteDC(hdc as UINT),int;
DECLARE EXTERN PRINTBITMAP(hdc as UINT,hBmp as UINT,rcBitmap as RECT),INT;
/********************************************** CPRINTER CLASS DEFINITION ************************************************/
Class Cprinter
{
declare Cprinter();
declare _Cprinter();
declare Assign(),int;


Int m_valid;
PRINTDLG PD;
String m_name;
}

Cprinter::CPrinter()
{
m_valid= false;
m_name="";
// initialize to default printer

pointer pstring;
pointer ptemp;
int offset;
zeromemory(pd,len(printdlg));

pd.lstructsize=len(printdlg);
pd.Flags = PD_RETURNDEFAULT|PD_RETURNDC;
m_valid=printdlg(pd);
if m_valid
{
_fpreset();

IF pd.hDevNames
{
   pString = GlobalLock(pd.hDevNames);
   ptemp=pstring;
   offset=*(devnames)pstring.woutputoffset;
   pString += offset;
   strprinter=*(string)pstring;
   if(strPrinter[0] != "\\")
   {
         ptemp+=*(devnames)ptemp.wdeviceoffset;
         
         m_name = *(string)pTemp;
   }
      GlobalFree(pd.hDevNames);      
}
}
return;
}

Cprinter::_CPrinter()
{
if PD.hdc
deletedc(PD.hdc);
IF pd.hDevMode
            {
               GlobalUnlock(pd.hDevMode);
               GlobalFree(pd.hDevMode);
            }
m_valid=0;   
print ("released");
return;
}

Cprinter::Assign(),int
{
pointer pstring,ptemp;
ZeroMemory(pd,LEN(PRINTDLG));
PD.lStructSize = LEN(PRINTDLG);
PD.nMinPage = 1;
PD.nMaxPage = 1;
PD.nFromPage = 1;
PD.nToPage = 1;
PD.nCopies = 1;
PD.Flags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC ;
PD.hwndOwner = null;
m_valid= PrintDlg(PD);
if m_valid
      {
         _fpreset();
         
         IF pd.hDevNames
         {
         pString = GlobalLock(pd.hDevNames);
         pTemp = pString;
         pString += *(DEVNAMES)pString.wOutputOffset;
         strPrinter = *(STRING)pString;
         if(strPrinter[0] <> "\\")
         {
            pTemp += *(DEVNAMES)pTemp.wDeviceOffset;
            m_name = *(STRING)pTemp;
         }
         GlobalFree(PD.hDevNames);
         }
       }

return m_valid;
}


global sub main()
{
docinfo doc;
string fname,filter;
rect rc;
point size;
doc.cbSize=20;
doc.lpszDocName="TestDoc";
doc.lpszOutput=null;
doc.lpszDataType=null;
Cbitmap bmp;
Cprinter prn;
openconsole();

print(prn.m_name," valid=",prn.m_valid) ;

if prn.assign()
print (prn.m_name);

fname=filerequest("Load Bitmap",0,1,filter,"bmp");
         if len(fname)
         {
         if bmp.LoadFromFile(fname,0)
            {
            
            size = bmp.GetSize();
            rc.left=0;
            rc.top=0;
            rc.right=size.x;
            rc.bottom=size.y;
            
            }
         }


if prn.m_valid
{
StartDoc(prn.PD.hdc,doc);
StartPage(Prn.PD.hDC);
if bmp.m_himage
print(PRINTBITMAP(Prn.PD.hDC,bmp.m_himage,rc));   // This is where it crashes
// No problem so far with printing graphic primitives or text
EndPageA(Prn.PD.hDC);
EndDoc(Prn.PD.hDC);
}
while getkey()="";
closeconsole();
}


Ionic Wind Support Team

The rectangle you give PrintBitmap is in printer device coordinates, not the size of the bitmap.   You have to scale appropriately based on the horizontal and vertical resolution of the printer.  Again this is done int the PrintWindow function for an autodrawn window which uses the screen dimensions as the basis of the scaling factor.

Think about it for a moment.   A standard letter size (US) page is 8.5 * 11.5 inches.  If the printer has a horizontal resolution of 600 DPI then that is 600 * 8.5 pixels across the page.  5100 pixels in the X direction.  If you managed to get your code to work as written with say a 200x200 bitmap then you would end up with a tiny little black square on the page.

You need to use GetDeviceCaps as I do in the PrintWindow function to retrieve the resolution of the selected page size from the printer dialog. Then determine your scaling factors based on where you want to put the bitmap on the page.  For example if you wanted the bitmap to take up 1/4 of the page in the upper left corner then that would be 4.24 * 5.25 inches.  Or just divide both the horiz/vert resolutions by 2.

In your class your calling PrintDialog twice, couldn't figure out why just by looking at it.  Also you would need a bit more error checking before calling PrintBitmap.  Be sure the dc is not null, the handle to the bitmap is not null and the rectangle contains valid printer device coordinates.

Paul.

Ionic Wind Support Team

Haim

Thanks,
Printdlg is called once in the constructor, in order to initialize Cprinter to  the default printer. The second time is called in the Assign method - to select and setup any available printer.
I am aware of the coordinates and scaling issue. I intend to work on those later, once I can output the bitmap without crashing...

I check for the validity of the printer device context as well as for the validity of the handle to the bitmap and still it crashes. This is frustrating...

By the way,  do I need tocall the destructor explicitly, so as to avoid memory leaks?

Ionic Wind Support Team

No you never call a destructor directly. Never do this:

doc.cbSize=20;

Always use the LEN operator since packing of structures can effect the actual size. 

Other things your code needs:

StartDoc returns a result that must be checked.  If it is greater than zero then it is alright to proceed with StartPage, otherwise you'll end up with crashes.  EndPageA returns a result that must be checked so you can either call AbortDoc or EndDoc.  Never assume the page printed. 

You won't be able to output a bitmap without correct coordinates and scaling.  It might be that PrintBitmap is crashing due to extremly small rectangle your passing it, it was never designed as a general purpose function although it does have extensive error checking.  Find out where it is giving you a GPF specifially by building a debug executable and running it in debug mode. 

The bitmap itself could be a problem.  I've only tested the function with a DDB bitmap, used internally by Windows.  The code does convert from DDB to DIB before printing.

Paul.

Ionic Wind Support Team

Haim

Thanks,
I'll try to implement your suggestions.
Thanks again for your help!

Ionic Wind Support Team

Let me know how you do ;)

If you really get stuck let me know and I'll throw together a basic example.

Paul.
Ionic Wind Support Team

Haim

An example would be great, if you can find the time for it...

Ionic Wind Support Team

Sure thing.  I'll have something tomorrow.
Ionic Wind Support Team

Ionic Wind Support Team

Still working on the example.  Got tied up yesterday at work ;)

The example displays a psuedo printer page and lets you place the bitmap where you want on the page before printing.  It will be the first of two examples, the first showing how to do it with the default DC mapping modes (MM_TEXT), and the second showing how to do it with a standardized mapping mode like MM_HIENGLISH where you don't need to do your own aspect ration/scaling.

Ionic Wind Support Team

Haim

Graet! I'll be waiting patiently.. :)

Ionic Wind Support Team

And I found the bug in the PRINTBITMAP function that was causing your crash.  I'll have an updated gui.lib to give you as well.
Ionic Wind Support Team

Ionic Wind Support Team

Ok here is the first example.  About 98% completed.  You can load a bitmap, position it visually, and print it using the PrintBitmap function.  As usual I tend to go a bit overboard on my examples, but wanted to include a full template to work from.

This example uses MM_TEXT mode for both the printer and screen.  Scaling factors are applied when printing to match a standard US Letter sized paper.  If you print on a different size, such as Metric dimensions, the code will need to be adjusted accordingly.

You will need the "Misc GUI.LIB update 5/11/2006" update posted in the Update announcements category.


#typedef UINT unsigned int;

struct PRINTDLG,1
{
    UINT lStructSize;
    UINT hwndOwner;
    UINT hDevMode;
    UINT hDevNames;
    UINT hDC;
    UINT Flags;
    WORD nFromPage;
    WORD nToPage;
    WORD nMinPage;
    WORD nMaxPage;
    WORD nCopies;
    UINT hInstance;
    UINT lCustData;
    void *lpfnPrintHook;
    void *lpfnSetupHook;
    void *lpPrintTemplateName;
    void *lpSetupTemplateName;
    UINT hPrintTemplate;
    UINT hSetupTemplate;
}

struct DEVNAMES,1 {
    word wDriverOffset;
    word wDeviceOffset;
    word wOutputOffset;
    word wDefault;
    // driver, device, and port name strings follow wDefault
}

struct DOCINFO {
int cbSize;
string *lpszDocName;
string *lpszOutput;
string *lpszDataType;
UINT fwType;
}

struct DEVMODE
{
    byte dmDeviceName[32];
    WORD dmSpecVersion;
    WORD dmDriverVersion;
    WORD dmSize;
    WORD dmDriverExtra;
    UINT dmFields;
    WORD dmOrientation;
    WORD dmPaperSize;
    WORD dmPaperLength;
    WORD dmPaperWidth;
    WORD dmScale;
    WORD dmCopies;
    WORD dmDefaultSource;
    WORD dmPrintQuality;
    WORD dmColor;
    WORD dmDuplex;
    WORD dmYResolution;
    WORD dmTTOption;
    WORD dmCollate;
    byte  dmFormName[32];
    WORD  dmLogPixels;
    UINT  dmBitsPerPel;
    UINT  dmPelsWidth;
    UINT  dmPelsHeight;
    UINT  dmDisplayFlags;
    UINT  dmDisplayFrequency;
    UINT  dmICMMethod;
    UINT  dmICMIntent;
    UINT  dmMediaType;
    UINT  dmDitherType;
    UINT  dmICCManufacturer;
    UINT  dmICCModel;
    UINT  dmPanningWidth;
    UINT  dmPanningHeight;
}

//CPrintWindow definition
class CPrintWindow : CWindow
{
declare CPrintWindow();
declare _CPrintWindow();
declare virtual OnClose(),int;
declare virtual OnCreate(),int;
declare virtual OnHScroll(int nCommand,int nPos,int nID),int;
declare virtual OnLButtonDown(int x,int y, int flags),int;
declare virtual OnLButtonUp(int x,int y,int flags),int;
declare virtual OnMenuPick(int nID),int;
declare virtual OnMouseMove(int x,int y,int flags);
declare virtual OnPaint(),int;
declare virtual OnVScroll(int nCommand,int nPos,int nID),int;
declare DoPrint(),int;
//Member Variables;
unsigned int m_hPrinterDC;  //handle to a printer device context
string m_strDefaultPrinter; //name of the default printer
RECT m_rectBitmap; //rendering rectangle for the screen
POINT m_bmpSize; //size of the bitmap
POINT m_ptDragStart;
CBitmap m_bmp; //A CBitmap object for loading the bitmap;
int m_bSelected;
int m_bDragging;
//page width and height on the screen (in pixels);
int m_nPageWidth;
int m_nPageHeight;
//the name of the loaded bitmap
string m_strFileName;


}

#define appWidth 800
#define appHeight 600
#define IDM_OPENBITMAP 100
#define IDM_QUIT 200
#define IDM_PRINT 300
//API imports
DECLARE IMPORT,DeleteObject(unsigned int hObject),int;
DECLARE IMPORT,ZeroMemory ALIAS RtlZeroMemory(pointer pvoid,int length),int;
DECLARE IMPORT,InvalidateRect(unsigned int hWnd,RECT *lpRect,int bErase),int;
DECLARE IMPORT,StretchBlt(unsigned int HDCDest,int destx,int desty,int destw,int desth,unsigned int HDCSrc,
int srcx,int srcy,int srcw,int srch,int rop),int;
DECLARE IMPORT,CreateCompatibleDC(unsigned int hDC),unsigned int;
DECLARE IMPORT,DeleteDC(unsigned int hDC),int;
DECLARE IMPORT,SelectObject(unsigned int hDC,unsigned int hObject),int;
import int PtInRect(
RECT *lprc, // address of structure with rectangle
POINT pt BYVAL // structure with point
);
DECLARE IMPORT,OffsetRect(RECT *lpRect,int dx,int dy);


global sub main()
{
CPrintWindow win;
if(win.Create(0,0,appWidth,appHeight,AWS_VISIBLE|AWS_CAPTION|AWS_SYSMENU|AWS_SIZE|AWS_MINIMIZEBOX|AWS_MAXIMIZEBOX,
0,"Bitmap Printer",NULL) != 0)
{
win.SetWindowColor(RGB(192,192,192));
do {wait(); }until !win.IsValid();
}
}

//CPrintWindow Implementation
CPrintWindow::CPrintWindow()
{
m_hPrinterDC = NULL;
m_strDefaultPrinter = "";
m_bSelected = false;
m_bDragging = false;
ZeroMemory(m_rectBitmap,LEN(RECT));
m_bmpSize.x = 0;
m_bmpSize.y = 0;
//assume 8.5 x 11 inches
//This could be adjusted to whatever is returned by a printer setup dialog
m_nPageWidth = 400;
m_nPageHeight = m_nPageWidth * (11.0 / 8.5);

}

CPrintWindow::_CPrintWindow()
{
}

CPrintWindow::OnClose(),int
{
Destroy();
return 0;
}

CPrintWindow::OnCreate(),int
{
CenterWindow();
CMenu m;
m.BeginMenu();
m.MenuTitle("File");
m.MenuItem("Open Bitmap",0,IDM_OPENBITMAP);
m.MenuItem("Print",0,IDM_PRINT);
m.MenuItem("Quit",0,IDM_QUIT);
m.EndMenu();
SetMenu(m.Detach());
return 0;
}

CPrintWindow::OnHScroll(int nCommand,int nPos,int nID),int
{
return 0;
}

CPrintWindow::OnLButtonDown(int x,int y, int flags),int
{
point pt;pt.x = x;pt.y=y;
if(PtInRect(&m_rectBitmap,pt))
{
m_ptDragStart = pt;
m_bDragging = true;
if(!m_bSelected)
{
m_bSelected = TRUE;
InvalidateRect(m_hWnd,0,0);
}
}
else
{
m_bDragging = false;
if(m_bSelected)
{
m_bSelected = FALSE;
InvalidateRect(m_hWnd,0,0);
}
}


return 0;
}

CPrintWindow::OnLButtonUp(int x,int y,int flags),int
{
point pt;pt.x = x;pt.y = y;
if(m_bSelected && m_bDragging)
{
//test for valid positioning
if(m_rectBitmap.top < 10)
OffsetRect(m_rectBitmap,0,10-m_rectBitmap.top);
if(m_rectBitmap.bottom > (10 + m_nPageHeight))
OffsetRect(m_rectBitmap,0,(10 + m_nPageHeight) - m_rectBitmap.bottom);
if(m_rectBitmap.left < (appWidth / 2.0 - m_nPageWidth / 2.0))
OffsetRect(m_rectBitmap,(appWidth / 2.0 - m_nPageWidth / 2.0)- m_rectBitmap.left,0);
if(m_rectBitmap.right > (appWidth / 2.0 + m_nPageWidth / 2.0))
OffsetRect(m_rectBitmap,(appWidth / 2.0 + m_nPageWidth / 2.0)-m_rectBitmap.right,0);
InvalidateRect(m_hWnd,0,1);
}
m_bDragging = false;
return 0;
}

CPrintWindow::OnMouseMove(int x,int y,int flags),int
{
point pt;pt.x = x;pt.y = y;
if(m_bSelected && m_bDragging && (flags & 0x01))
{
//move the bitmap rectangle
int offsetX = pt.x - m_ptDragStart.x;
int offsetY = pt.y - m_ptDragStart.y;
SetRasterOp(RMXORPEN);
SetLineStyle(LSSOLID,3);
DrawRect(m_rectBitmap.left,m_rectBitmap.top,m_rectBitmap.right - m_rectBitmap.left,
m_rectBitmap.bottom-m_rectBitmap.top,RGB(255,255,255));
//offset the rectangle by the drag amount
OffsetRect(m_rectBitmap,offsetX,offsetY);

DrawRect(m_rectBitmap.left,m_rectBitmap.top,m_rectBitmap.right - m_rectBitmap.left,
m_rectBitmap.bottom-m_rectBitmap.top,RGB(255,255,255));
SetLineStyle(LSSOLID,1);
SetRasterOp(RMCOPYPEN);
m_ptDragStart = pt;


}
}

CPrintWindow::OnMenuPick(int nID),int
{
select(nID)
{
case IDM_OPENBITMAP:
string filename = FileRequest("Select Bitmap File",this,true,"(*.bmp)|*.bmp");
if(len(filename))
{
unsigned int hBmpOld = m_bmp.Detach();
if(hBmpOld)
DeleteObject(hBmpOld);
m_bSelected = false;
m_bDragging = false;
if(m_bmp.LoadFromFile(filename))
{
m_strFileName = filename;
m_bmpSize = m_bmp.GetSize();
//Setup our initial rendering rectangle
m_rectBitmap.left = appWidth / 2.0 - m_nPageWidth / 2.0;
m_rectBitmap.top = 10; //top of the page;
m_rectBitmap.right = m_rectBitmap.left + m_bmpSize.x;
m_rectBitmap.bottom = m_rectBitmap.top + m_bmpSize.y;
//if it is bigger than our on screen page size scale it down
double ratio = m_bmpSize.x / (m_bmpSize.y+0.0);
if(m_bmpSize.x > m_bmpSize.y)
ratio = 1.0/ratio;
if((m_rectBitmap.right - m_rectBitmap.left) > m_nPageWidth)
{
m_rectBitmap.right = m_rectBitmap.left + m_nPageWidth;
m_rectBitmap.bottom = m_rectBitmap.top + (m_nPageWidth * ratio);
}
if((m_rectBitmap.bottom - m_rectBitmap.top) > m_nPageHeight)
{
m_rectBitmap.bottom = m_rectBitmap.top + m_nPageHeight;
m_rectBitmap.right = (m_nPageHeight * ratio);
}
//invalidate the window so it redraws
InvalidateRect(m_hWnd,0,0);
}
}
case IDM_PRINT:
DoPrint();
case IDM_QUIT:
Destroy();
}
return 0;
}

CPrintWindow::OnVScroll(int nCommand,int nPos,int nID),int
{
return 0;
}

CPrintWindow::OnPaint(),int
{
//draw our fake 'page'
RECT rcPage;
rcPage.left = appWidth / 2.0 - m_nPageWidth / 2.0;
rcPage.right = rcPage.left + m_nPageWidth;
rcPage.top = 10; // just a margin to make it look better
rcPage.bottom = rcPage.top + m_nPageHeight;
DrawRect(rcPage.left,rcPage.top,m_nPageWidth,m_nPageHeight,0,RGB(255,255,255));
if(m_bmp.m_hImage)
{
//use StretchBlt to draw the bitmap on screen
unsigned int hDCSrc = CreateCompatibleDC(NULL);
unsigned int hDCDest = GetHDC();
temp = SelectObject(hDCSrc,m_bmp.m_hImage);
StretchBlt(hDCDest,m_rectBitmap.left,m_rectBitmap.top,m_rectBitmap.right - m_rectBitmap.left,
m_rectBitmap.bottom-m_rectBitmap.top,hdcSrc,0,0,m_bmpSize.x,m_bmpSize.y,0x00CC0020);
SelectObject(hDCSrc,temp);
DeleteDC(hDCSrc);
ReleaseHDC(hDCDest);
if(m_bSelected)
{
SetRasterOp(RMXORPEN);
SetLineStyle(LSSOLID,3);
DrawRect(m_rectBitmap.left,m_rectBitmap.top,m_rectBitmap.right - m_rectBitmap.left,
m_rectBitmap.bottom-m_rectBitmap.top,RGB(255,255,255));
SetLineStyle(LSSOLID,1);
SetRasterOp(RMCOPYPEN);
}


}

return 0;
}

DECLARE IMPORT,PrintDlg ALIAS PrintDlgA(pd as PRINTDLG),INT;
DECLARE IMPORT,GetSystemMetrics(index as int),int;
DECLARE IMPORT,GetDeviceCaps(hdc as UINT,nIndex as INT),INT;
DECLARE IMPORT,GetWindowTextA(hwnd as UINT,lpszText as STRING,nMaxCount as INT),INT;
DECLARE IMPORT,GetWindowTextLengthA(hwnd as UINT),INT;
DECLARE IMPORT,StartDocA(hdc as UINT, di as DOCINFO),INT;
DECLARE IMPORT,StartPage(hdc as UINT),INT;
DECLARE IMPORT,EndPageA alias EndPage(hdc as UINT),INT;
DECLARE IMPORT,EndDoc(hdc as UINT),INT;
DECLARE IMPORT,AbortDoc(hdc as UINT),INT;
DECLARE IMPORT,GlobalLock(hMem as UINT),POINTER;
DECLARE IMPORT,GlobalUnlock(hMem as UINT),INT;
DECLARE IMPORT,GlobalFree(hMem as UINT),INT;
DECLARE IMPORT,SetMapMode(hdc as UINT,mode as INT),INT;
DECLARE IMPORT,SetViewportOrgEx(hdc as UINT,X as INT,Y as INT,lpPoint as POINTER),INT;
DECLARE IMPORT,SetWindowOrgEx(hdc as UINT,X as INT,Y as INT,lpPoint as POINTER),INT;
DECLARE IMPORT,SetWindowExtEx(hdc as UINT,nXExtent as INT,nYExtent as INT,lpSize as POINTER),INT;
DECLARE IMPORT,SetViewportExtEx(hdc as UINT,nXExtent as INT,nYExtent as INT,lpSize as POINTER),INT;
DECLARE EXTERN _fpreset();
DECLARE EXTERN PRINTBITMAP(hdc as UINT,hBmp as UINT,rcBitmap as RECT),INT;
CONST PD_ALLPAGES = 0;
CONST PD_CAN_DRAW_DIB = 1;
CONST PD_CAN_STRETCHDIB = 2;
CONST PD_COLLATE = 0x10;
CONST PD_CURRENTPAGE = 0x400000;
CONST PD_DISABLEPRINTTOFILE = 0x80000;
CONST PD_ENABLEPRINTHOOK = 0x1000;
CONST PD_ENABLEPRINTTEMPLATE = 0x4000;
CONST PD_ENABLEPRINTTEMPLATEHANDLE = 0x10000;
CONST PD_ENABLESETUPHOOK = 0x2000;
CONST PD_ENABLESETUPTEMPLATE = 0x8000;
CONST PD_ENABLESETUPTEMPLATEHANDLE = 0x20000;
//CONST PD_EXCL_COPIESANDCOLLATE = (DM_COPIES | DM_COLLATE)
CONST PD_EXCLUSIONFLAGS = 0x1000000;
//CONST PD_HDR_LEN = sizeof(POLICY_DATA)
CONST PD_HIDEPRINTTOFILE = 0x100000;
CONST PD_NOCURRENTPAGE = 0x800000;
CONST PD_NONETWORKBUTTON = 0x200000;
CONST PD_NOPAGENUMS = 0x8;
CONST PD_NOSELECTION = 0x4;
CONST PD_NOWARNING = 0x80;
CONST PD_PAGENUMS = 0x2;
CONST PD_PRINTSETUP = 0x40;
CONST PD_PRINTTOFILE = 0x20;
CONST PD_RESULT_APPLY = 2;
CONST PD_RESULT_CANCEL = 0;
CONST PD_RESULT_PRINT = 1;
CONST PD_RETURNDC = 0x100;
CONST PD_RETURNDEFAULT = 0x400;
CONST PD_RETURNIC = 0x200;
CONST PD_SELECTION = 0x1;
CONST PD_SHOWHELP = 0x800;
CONST PD_STRETCHDIB_1_1_OK = 0x4;
CONST PD_STRETCHDIB_1_2_OK = 0x8;
CONST PD_STRETCHDIB_1_N_OK = 0x10;
CONST PD_USEDEVMODECOPIES = 0x40000;
CONST PD_USEDEVMODECOPIESANDCOLLATE = 0x40000;
CONST PD_USELARGETEMPLATE = 0x10000000;
CONST SM_CXSCREEN = 0;
CONST SM_CYSCREEN = 1;
CONST HORZRES = 8;
CONST VERTRES = 10;
CONST MM_TEXT = 1;
CONST DMORIENT_LANDSCAPE = 2;

CPrintWindow::DoPrint(),int
{
PRINTDLG pd;
DOCINFO di;
RECT rectPrint;
int prtX,prtY,nWidth,nHeight;
double sX,sY;
if(m_bmp.m_hImage)
{
ZeroMemory(pd,LEN(PRINTDLG));
pd.lStructSize = LEN(PRINTDLG);
pd.nMinPage = 1;
pd.nMaxPage = 1;
pd.nFromPage = 1;
pd.nToPage = 1;
pd.nCopies = 1;
pd.Flags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC ;
pd.hwndOwner = m_hWnd;
IF PrintDlg(pd)
{
_fpreset();
IF pd.hDevMode
pointer pMode = GlobalLock(pd.hDevMode);
ZeroMemory (di, LEN(DOCINFO));
di.cbSize = LEN(DOCINFO);
di.lpszDocName = m_strFileName;
IF StartDocA(pd.hdc,di) > 0
{
StartPage(pd.hdc);
SetMapMode(pd.hdc,MM_TEXT);
prtX = GetDeviceCaps(pd.hdc,HORZRES);
prtY = GetDeviceCaps(pd.hdc,VERTRES);
//Get our bitmaps rectangle, in screen coordinates
//Offset it so the origin 0,0 is in the upper left.
//This is because our onscreen 'page' is drawn centered.
//So we need to account for the position difference.
rectPrint = m_rectBitmap;
OffsetRect(rectPrint,-(appWidth / 2.0 - m_nPageWidth / 2.0),-10);
//calculate the scaling factors
sX = prtX / (m_nPageWidth + 0.0);
sY = prtY / (m_nPageHeight + 0.0);
//apply the scaling factors
nWidth = (rectPrint.Right - rectPrint.Left) * sX;
nHeight = (rectPrint.Bottom - rectPrint.Top) * sY;
rectPrint.Left *= sX;
rectPrint.top *= sY;
rectPrint.Right = rectPrint.Left + nWidth;
rectPrint.Bottom = rectPrint.top + nHeight;
//print the bitmap.
PRINTBITMAP(pd.hdc,m_bmp.m_hImage,rectPrint);
if (EndPageA(pd.hdc) <= 0)
AbortDoc(pd.hdc)
else
EndDoc(pd.hdc);


}
if(pd.hDevMode)
{
GlobalUnlock(pd.hDevMode);
GlobalFree(pd.hDevMode);
}
if(pd.hDC)
DeleteDC(pd.hDC);

}

}
}
Ionic Wind Support Team

Haim

Thank you very much for your help!!

Ionic Wind Support Team

Your welcome.

Don't forget to download the update.  Or the example won't work.

http://www.ionicwind.com/forums/index.php?topic=578.0

Ionic Wind Support Team