본문 바로가기
Delphi Tip/+Tip

메모 프린터 기타

by MonoSoft 2022. 6. 29.
728x90
반응형

방안 #1
uses
  Printers;

procedure TForm1.Button1Click(Sender: TObject);
var
  ScaleX, ScaleY: Integer;
  RR: TRect;
begin
  with Printer do
  begin
    BeginDoc;
    // The StartDoc function starts a print job.
    try
      ScaleX := GetDeviceCaps(Handle, logPixelsX) div PixelsPerInch;
      ScaleY := GetDeviceCaps(Handle, logPixelsY) div PixelsPerInch;
      // Retrieves information about the Pixels per Inch of the Printer.
      RR := Rect(0, 0, Image1.picture.Width * scaleX, Image1.Picture.Height * ScaleY);
      Canvas.StretchDraw(RR, Image1.Picture.Graphic);
      // Stretch to fit

    finally
      EndDoc;     end;
  end;
end;

방안 #2
// Based on posting to borland.public.delphi.winapi by Rodney E Geraghty, 8/8/97.


procedure PrintBitmap(Canvas: TCanvas; DestRect: TRect; Bitmap: TBitmap);
var
  BitmapHeader: pBitmapInfo;
  BitmapImage: Pointer;
  HeaderSize: DWORD;
  ImageSize: DWORD;
begin
  GetDIBSizes(Bitmap.Handle, HeaderSize, ImageSize);
  GetMem(BitmapHeader, HeaderSize);
  GetMem(BitmapImage, ImageSize);
  try
    GetDIB(Bitmap.Handle, Bitmap.Palette, BitmapHeader^, BitmapImage^);
    StretchDIBits(Canvas.Handle,
      DestRect.Left, DestRect.Top,    // Destination Origin
      DestRect.Right - DestRect.Left, // Destination Width
      DestRect.Bottom - DestRect.Top, // Destination Height
      0, 0,                           // Source Origin
      Bitmap.Width, Bitmap.Height,    // Source Width & Height
      BitmapImage,
      TBitmapInfo(BitmapHeader^),
      DIB_RGB_COLORS,
      SRCCOPY)
  finally
    FreeMem(BitmapHeader);
    FreeMem(BitmapImage)
  end
end {PrintBitmap};

방안 #3
uses
  printers;

procedure DrawImage(Canvas: TCanvas; DestRect: TRect; ABitmap: TBitmap);
var
  Header, Bits: Pointer;
  HeaderSize: DWORD;
  BitsSize: DWORD;
begin
  GetDIBSizes(ABitmap.Handle, HeaderSize, BitsSize);
  Header := AllocMem(HeaderSize);
  Bits := AllocMem(BitsSize);
  try
    GetDIB(ABitmap.Handle, ABitmap.Palette, Header^, Bits^);
    StretchDIBits(Canvas.Handle, DestRect.Left, DestRect.Top,
      DestRect.Right, DestRect.Bottom,
      0, 0, ABitmap.Width, ABitmap.Height, Bits, TBitmapInfo(Header^),
      DIB_RGB_COLORS, SRCCOPY);
  finally
    FreeMem(Header, HeaderSize);
    FreeMem(Bits, BitsSize);
  end;
end;

procedure PrintImage(Image: TImage; ZoomPercent: Integer);
  // if ZoomPercent=100, Image will be printed across the whole page
var 
  relHeight, relWidth: integer;
begin
  Screen.Cursor := crHourglass;
  Printer.BeginDoc;
  with Image.Picture.Bitmap do 
  begin
    if ((Width / Height) > (Printer.PageWidth / Printer.PageHeight)) then
    begin
      // Stretch Bitmap to width of PrinterPage
      relWidth := Printer.PageWidth;
      relHeight := MulDiv(Height, Printer.PageWidth, Width);
    end 
    else
    begin
      // Stretch Bitmap to height of PrinterPage
      relWidth  := MulDiv(Width, Printer.PageHeight, Height);
      relHeight := Printer.PageHeight;
    end;
    relWidth := Round(relWidth * ZoomPercent / 100);
    relHeight := Round(relHeight * ZoomPercent / 100);
    DrawImage(Printer.Canvas, Rect(0, 0, relWidth, relHeight), Image.Picture.Bitmap);
  end;
  Printer.EndDoc;
  Screen.cursor := crDefault;
end;

// Example Call:

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Print image at 40% zoom:
  PrintImage(Image1, 40);
end;



강제 PC끄기
WriteOption
Okay, thank you for your help (although I already know about those
things)

these are the way that I've found to shutdown even windows XP, 98:
===============================
function SetPrivilege(sPrivilegeName : string;bEnabled : boolean ): boolean;
var
TPPrev,
TP : TTokenPrivileges;
Token : THandle;
dwRetLen : DWord;
begin
Result := False;
OpenProcessToken(GetCurrentProcess,TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,Token);
TP.PrivilegeCount := 1;
if( LookupPrivilegeValue( Nil, PChar( sPrivilegeName ), TP.Privileges[ 0 ].LUID ) )then
begin
if( bEnabled )then
begin
TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;
end else
begin
TP.Privileges[ 0 ].Attributes := 0;
end;
dwRetLen := 0;
Result := AdjustTokenPrivileges( Token,False,TP,SizeOf( TPPrev ),TPPrev,dwRetLen );
end;
CloseHandle( Token );
end;

//
// iFlags:
//
// one of the following must be
// specified
//6512401410   790728
// EWX_LOGOFF
// EWX_REBOOT
// EWX_SHUTDOWN
//
// following attributes may be
// combined with above flags
//
// EWX_POWEROFF
// EWX_FORCE : terminate processes
//
function WinExit( iFlags : integer ) : boolean;
begin
Result := True;
if( SetPrivilege( 'SeShutdownPrivilege', True ) )then
begin
if( not ExitWindowsEx( iFlags, 0 ) )then
begin
// handle errors...
Application.MessageBox('Priviledge error', 'Priviledge() Failed', MB_OK );
Result := False;
end;
SetPrivilege( 'SeShutdownPrivilege', False )
end else
begin
// handle errors...
ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF,0);
Application.MessageBox('NON Priviledge error', 'NON Priviledge() Failed', MB_OK );
Result := False;
end;
end;
===============================
and to use it:

WinExit(EWX_SHUTDOWN+EWX_FORCE+EWX_POWEROFF);

thank you for your help
'Jhs




TPrinter  객체를 이용한 프린트 하기

 

TPrinter 객체의 메서드

 

 메서드

 상세설명

 BeginDoc   

 인쇄를 시작한다.

 EndDoc     

 인쇄를 종료한다.

 NewPage   

 한페이지의 인쇄를 마치고 용지를 새것으로 바꾼다.

 Canvas    

 문서의 내용을 그리는데 사용한다.

 Handle

 프린터의 DC  핸들값

 Abort

 사용자에 의해 인쇄가 강제로 중단되었는지 여부를 나타낸다.

 True - 이후로는 더 해봐야 바로 삭제 되므로 그 즉시 인쇄루틴을 중단하는것이 좋다. 

 Copies

 몇장을 인쇄할것인지 지 결정 

 Orientation

 출력 방향, 가로, 세로 중 선택

 Title

 인쇄가 시작되는 윈도의 인쇄 관리자가 실행되는데, 인쇄 관리자의 인쇄 목록에 제목 

 GetPrinter

 프린터의 상테를 알아옴 

 SetPrinter

 프린터의 상태를 설정 

 PrinterIndex

 윈도에 여러개의 프린터가 설치되어잇을 경우 현재 사용중인 프린터 순서를 나타낸다. 

 Printers

 윈도에 설치된 프린터들의 목록을 보여준다. 

 Fonts

 인쇄를 할 때 사용할 수 있는 글꼴 목록을 보여준다. 

 PageHeight

 용지에서 사용 가능한 면적의 높이를 픽셀 단위로 나타냄

 PageWidth

 용지에서 사용 가능한 면적의 넓이를 픽셀 단위로 나타냄 

 PageNumber

 현재 인쇄되고 있는 페이지를 나타낸다. 

 Printing

 인쇄중인지 나타낸다. 

 

Edit의 내용을 출력하는 간단한 예 (Button 1 Ea , Edit 1 Ea)

 

1. Uses에  Printers 를 추가한다.

 

2. Button  에 코딩

procedure TForm1.Button1Click(sender:TObject);

 var 

 X,y:integer

 

begin

Printer.PrinterIndex :=0 ;  // 출력 대상프린터 지정

x := 20 ;

y := 40 ;   //출력위치를 지정해준다.

 

With Printer.Canvas  do begin

    Printer.BeginDoc  ;    //출력이 시작됨을 알린다.

    TextOut (x,y,Edit1.text) ;  // 출력을 실행한다.

    Printer.EndDoc ; //출력이 끝났음을 알린다.

end;

end.

3. 정말 간단하다. 컴파일후 버튼을 클릭하면   Edit1에 입력한 글자가 출력 된다.

728x90
반응형

댓글