Fredrick,
The call you are using returns the left margin in device pixels. You
need to also get the HORZRES value of pixels in width and the HORZSIZE in
centimeters and convert all of this to pixels/inch x.
For example the following C# code gets all the margins in 100ths of an inch
(the default resolution). Note that the margins may be assymetrical
especially in ink jet printers.
public static short DRIVERVERSION = 0; // Device driver version
public static short TECHNOLOGY = 2; // Device classification
public static short HORZSIZE = 4; // Horizontal size in
millimeters
public static short VERTSIZE = 6; // Vertical size in millimeters
public static short HORZRES = 8; // Horizontal width in pixels
public static short VERTRES = 10; // Vertical height in pixels
public static short BITSPIXEL = 12; // Number of bits per pixel
public static short PLANES = 14; // Number of planes
public static short NUMBRUSHES = 16; // Number of brushes the device
has
public static short NUMPENS = 18; // Number of pens the device
has
public static short NUMMARKERS = 20; // Number of markers the device
has
public static short NUMFONTS = 22; // Number of fonts the device
has
public static short NUMCOLORS = 24; // Number of colors the device
supports
public static short PDEVICESIZE = 26; // Size required for device
descriptor
public static short CURVECAPS = 28; // Curve capabilities
public static short LINECAPS = 30; // Line capabilities
public static short POLYGONALCAPS = 32; // Polygonal capabilities
public static short TEXTCAPS = 34; // Text capabilities
public static short CLIPCAPS = 36; // Clipping capabilities
public static short RASTERCAPS = 38; // Bitblt capabilities
public static short ASPECTX = 40; // Length of the X leg
public static short ASPECTY = 42; // Length of the Y leg
public static short ASPECTXY = 44; // Length of the hypotenuse
public static short PHYSICALWIDTH = 110; // Physical Width in device
units
public static short PHYSICALHEIGHT = 111; // Physical Height in device
units
public static short PHYSICALOFFSETX = 112; // Physical Printable Area x
margin
public static short PHYSICALOFFSETY = 113; // Physical Printable Area y
margin
public static short SCALINGFACTORX = 114; // Scaling factor x
public static short SCALINGFACTORY = 115; // Scaling factor y
/// <summary>
/// Return the device 'hard' margins for the passed in
/// Device Context handle. Return data in 1/100ths inch
/// </summary>
/// <param name="hDc">Input handle</param>
/// <param name="left">output left margin in 1/100ths inch</param>
/// <param name="top">output top margin in 1/100ths inch</param>
/// <param name="right">output right margin in 1/100ths inch</param>
/// <param name="bottom">output bottom margin in 1/100ths inch</param>
public static void GetHardMargins(int hDc, ref float left, ref float top,
ref float right, ref float bottom)
{
float offx = Convert.ToSingle(GetDeviceCaps(hDc, PHYSICALOFFSETX));
float offy = Convert.ToSingle(GetDeviceCaps(hDc, PHYSICALOFFSETY));;
float resx = Convert.ToSingle(GetDeviceCaps(hDc, HORZRES));
float resy = Convert.ToSingle(GetDeviceCaps(hDc, VERTRES));
float hsz = Convert.ToSingle(GetDeviceCaps(hDc, HORZSIZE))/25.4f; //
screen width in inches
float vsz = Convert.ToSingle(GetDeviceCaps(hDc, VERTSIZE))/25.4f; //
screen height in inches
float ppix = resx/hsz;
float ppiy = resy/vsz;
left = (offx/ppix) * 100.0f;
top = (offy/ppix) * 100.0f;
bottom = top + (vsz * 100.0f);
right = left + (hsz * 100.0f);
}
After doing this you should do a TranslateTransform(-left, -top); to the
approprate Graphics (in the PrintPage Event handler). This will get your
print orgin set to the top/left of the actual paper. Note that you won't be
able to print outside the hard margins of the printer though.
This is different than the document margins settings of the document.
As a further refinement I usually check to see if the total print width and
height of the printer is less than the vertical and horizontal extents of my
document's page and scale the graphics accordingly if true. This lets me
print on ink jet printers with larger physical margins without changing any
code.
Ron Allen
[quoted text, click to view] "Frederik Vanderhaegen" <frederik@digital-artworks.be> wrote in message
news:OMkt5AizDHA.1680@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I've tried your suggestion for the use of GetDeviceCaps but still isn't
the
> printresult the same as the preview.
> Maybe I do something wrong, hopefully you can help me, I would appreciate
> it.
>
> This is how I use the function:
> IntPtr hdc = e.Graphics.GetHdc();
> int iMarginLeft=GetDeviceCaps(hdc,112);
> e.Graphics.ReleaseHdc(hdc);
> int iMarginRight=iMarginLeft;
> pDoc.DefaultPageSettings.Margins.Left=iMarginLeft;
> pDoc.DefaultPageSettings.Margins.Right=iMarginLeft;
>
> where pDoc is my printdocument.
>
> Thanks in advance
>
> Frederik
>
>
>
>
> "Ron Allen" <rallen@_nospam_src-us.com> schreef in bericht
> news:#2AumJhzDHA.2580@TK2MSFTNGP09.phx.gbl...
> > Frederik,
> > If I understand your problem correctly, it is due to the 'hard
> margins'
> > on the printers. The actual drawing 0,0 point on the printers is at the
> > top/left hard margin point and for print preview it is at 0,0 on the
> > graphics. You can PInvoke GetDeviceCaps at the start of drawing to get
> the
> > printer margins and then call Translate on the Graphics to compensate.
If
> > you like I have some C# code that gets the hard margins for a printer.
> > You can usualy determine if you are drawing to a printer by checking
> to
> > see if VisibleClipBounds on your page is smaller than the page size.
> >
> > Ron Allen
> > "Frederik Vanderhaegen" <frederik@digital-artworks.be> wrote in message
> > news:uhxKMtezDHA.1576@TK2MSFTNGP11.phx.gbl...
> > > Hi,
> > >
> > > Does anyone knows how to fix following problem:
> > >
> > > I've created a printpreview with the printpreview control.
> > > This looks perfect but the printresult is different from the preview.
> > >
> > > I tested the program with a HP Deskjet 895CXI (win xp) and a HP
Laserjet
> > 4L
> > > (win2k) and on those two printers the aligment is never the same as
the
> > > preview.
> > >
> > > Any help is welcome.
> > > Thanks in advance
> > > Frederik
> > >
> > >
> >
> >
>
>