Recent

Author Topic: getting printer resolution (winApi) problem  (Read 18993 times)

ensimek

  • Jr. Member
  • **
  • Posts: 55
getting printer resolution (winApi) problem
« on: June 23, 2009, 11:23:56 pm »
Hi guys,
I've problem while retreiving printer X/Y DPI. In uses section I've 'Printers' and in project code:
rozdzX:=GetDeviceCaps(drukarka,LogPixelSX);

but while compiling i get error Identifier not found GetDeviceCaps.... what's wrong?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #1 on: June 23, 2009, 11:35:19 pm »
Hi guys,
I've problem while retreiving printer X/Y DPI. In uses section I've 'Printers' and in project code:
rozdzX:=GetDeviceCaps(drukarka,LogPixelSX);

but while compiling i get error Identifier not found GetDeviceCaps.... what's wrong?

Printer.XDPI. Printer.YDPI

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #2 on: June 23, 2009, 11:37:43 pm »
changing 'drukarka' to 'Printer.XDPI' not working, steel getting ' Identifier not found GetDeviceCaps'

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #3 on: June 23, 2009, 11:39:19 pm »
changing 'drukarka' to 'Printer.XDPI' not working, steel getting ' Identifier not found GetDeviceCaps'

Printer.XDPI returns the Printer Horizontal Dots Per Inch. GetDeviceCaps is not necessary.

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #4 on: June 23, 2009, 11:42:17 pm »
thanks theo, poor me :)

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #5 on: June 24, 2009, 01:06:27 pm »
theo, I also got this problem
---------------------------------------
rozdzX:=drukarka.XDPI;
rozdzY:=drukarka.YDPI;

   obliczX := round(rozdzX / 2.54);
   obliczX := obliczX * 10;
   obliczY := round(rozdzY / 2.54);
   obliczY := obliczY * 10;
drukarka.Canvas.TextOut(obliczY, obliczX, 'PRINT TEST');
----------------------------------------

Now, First ObliczX gives me how many pixels is in 1cm and in next increasing pixels number * 10 so it's 10cm.

But when I print this on a paper 'PRINT TEST' is 10,5cm from TOP, and 13,5cm from LEFT.
There should be the same space from TOP/LEFT while ObliczX and ObliczY has the same value..... Y/X DPI of printer is 600dpi.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #6 on: June 24, 2009, 02:36:21 pm »
I'm not an expert. It probably has to do with printer margins.

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #7 on: June 24, 2009, 02:38:35 pm »
I found out that drukarka.XDPI and drukarka.YDPI is not giving the real DPI. So I've to use GetDeviceCaps but it's not working so my first question is active :]

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #8 on: June 24, 2009, 05:01:14 pm »
Hmm it's working for me.

Place a TPrintDialog and a TButton on a Form. Add "Printers" to uses.
Click the Button1 and replace the code with:

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
  if PrintDialog1.Execute then
  begin
   Printer.BeginDoc;
   For i:=1 to 21 do
   begin
    Printer.Canvas.MoveTo(round(i*Printer.XDPI / 2.54),round(2*Printer.YDPI / 2.54));
    Printer.Canvas.LineTo(round(i*Printer.XDPI / 2.54),round(3*Printer.YDPI / 2.54));
   end;
   Printer.EndDoc;
  end;
end; 

Nothing else necessary.
I'm getting twenty lines. This seems OK since A4 is 21 centimeters, the last line is not visible because on the edge.

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #9 on: June 25, 2009, 01:58:07 pm »
theo so why when I'am using this code:
-----------------------------------------------------------------------------
   drukarka.PaperSize.PaperName := 'A5';     
   drukarka.BeginDoc;                     
   drukarka.Canvas.Font.Color := clBlack;   
   drukarka.Canvas.Font.Size := 9;       
   drukarka.Canvas.TextOut(round(drukarka.XDPI / 2.54),round(drukarka.YDPI / 2.54), 'A');
   drukarka.EndDoc;
-----------------------------------------------------------------------------
Letter 'A' should be in the same distance from top and left side of paper but it's not. It's from top about 1,5 cm, and from left about 4,5 cm with 600dpi X/Y printer. Whats wrong?     

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #10 on: June 25, 2009, 02:13:29 pm »
Does my code work for you?
What is "drukarka" exactly? Why don't you use "Printer"? It's already there, you don't have to create it.

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #11 on: June 25, 2009, 03:33:40 pm »
Code is working but not exactly the way i supposed to get.
drukarka is Printer (drukarka := Printer) so thats not the problem....
the problem is why for example 2360px from top is 1,5 cm, and from left is 4,5 cm while X/Y is the same :/

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #12 on: June 25, 2009, 03:49:55 pm »
I think I cannot help you.
If I execute the following code on Linux, it produces the attached PDF (CUPS-PDF as Printer). The result is as expected afaics.


Code: [Select]
uses ... Printers;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
  if PrintDialog1.Execute then
  begin
   Printer.BeginDoc;
    Printer.Canvas.Rectangle(
    round(2*Printer.XDPI / 2.54),
    round(2*Printer.YDPI / 2.54),
    round(3*Printer.XDPI / 2.54),
    round(3*Printer.YDPI / 2.54));
   Printer.EndDoc;
  end;
end;

ensimek

  • Jr. Member
  • **
  • Posts: 55
Re: getting printer resolution (winApi) problem
« Reply #13 on: June 25, 2009, 07:42:42 pm »
as You see your rectangle is the same space from top/left side of paper. if i use your example i wont get the same result :shit: have no clue why :/. Guys from $.pl told me that .xdpi differs from using GetDeviceCaps. Maybe it's ok on *nix, but not on windows, but i tried to get dpi using 'windows' unit from Lazarus but i got error 'got TPrinter expected LongWord'

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: getting printer resolution (winApi) problem
« Reply #14 on: June 26, 2009, 12:15:29 am »
Please help a bit more. I cannot see what you do.
Is the printed rectangle a square?
Is it 1x1 centimeters?
Then it could be an offset problem. If not It's probably a bug.

Does this code (just guesswork) return values <> 0 ?

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintDialog1.Execute;
Caption:=Inttostr(TPrinterCanvas(Printer.Canvas).LeftMargin)+' '+Inttostr(TPrinterCanvas(Printer.Canvas).TopMargin);
end;

 

TinyPortal © 2005-2018