Q&A

  • 화면 캡쳐 프린트에 관한 질문
프린트에 보면 세로로 프린트 가로 프린트가 있는데 대부분 세로로 Default 되어 있는데 이걸 제어할 수 있는 방법을 프린트 다이알 로그를 써야하는지와
캡쳐한 화면을 프린트 다이알 로그를 거쳐서 하는 방법과
그냥 바로 하는 방법의 예제 부탁드립니다.
1  COMMENTS
  • Profile
    홍성락 2002.10.12 21:29
    아래 예제를 보시면
    1. 가로,세로 설정
      if flag = 1 then Printer.Orientation := poPortrait
      else Printer.Orientation := poLandscape;
    호출시  Frm_Print(Frmmain, 2);  //1이면 세로 2이면 가로

    2.프린터 다이로그 컴포넌트를 사용시
      if Frmmain.PrintDialog1.Execute = False then exit;

    ----------------------------------------------------
    procedure TFrmmain.BitBtn2Click(Sender: TObject);
    begin
        Frm_Print(Frmmain, 2);
    end;
    //화면인쇄
    procedure Frm_Print(form1 : TForm; flag : integer);
    var
      dc: HDC;
      isDcPalDevice : Boolean;
      MemDc :hdc;
      MemBitmap : hBitmap;
      OldMemBitmap : hBitmap;
      hDibHeader : Thandle;
      pDibHeader : pointer;
      hBits : Thandle;
      pBits : pointer;
      ScaleX : Double;
      ScaleY : Double;
      ppal : PLOGPALETTE;
      pal : hPalette;
      Oldpal : hPalette;
      i : integer;
    begin
      if flag = 1 then Printer.Orientation := poPortrait
      else Printer.Orientation := poLandscape;
      if Frmmain.PrintDialog1.Execute = False then exit;

      pal := 0;
      OldPal := 0;
      pPal := nil;
      
      dc := GetDc(0);
      MemDc := CreateCompatibleDc(dc);
      MemBitmap := CreateCompatibleBitmap(Dc,form1.width,form1.height);
      OldMemBitmap := SelectObject(MemDc, MemBitmap);
      isDcPalDevice := false;
      if GetDeviceCaps(dc, RASTERCAPS) and RC_PALETTE = RC_PALETTE then begin
         GetMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
         FillChar(pPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
         pPal^.palVersion := $300;
         pPal^.palNumEntries := GetSystemPaletteEntries(dc,0,256,pPal^.palPalEntry);
         if pPal^.PalNumEntries <> 0 then begin
            pal := CreatePalette(pPal^);
            oldPal := SelectPalette(MemDc, Pal, false);
            isDcPalDevice := true
         end
         else begin
            FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
         end;
      end;
      BitBlt(MemDc, 0, 0, form1.width, form1.height, Dc, form1.left, form1.top, SrcCopy);
      if isDcPalDevice = true then begin
         SelectPalette(MemDc, OldPal, false);
         DeleteObject(Pal);
      end;
      SelectObject(MemDc, OldMemBitmap);
      DeleteDc(MemDc);
      hDibHeader := GlobalAlloc(GHND,sizeof(TBITMAPINFO) + (sizeof(TRGBQUAD) * 256));
      pDibHeader := GlobalLock(hDibHeader);
      FillChar(pDibHeader^, sizeof(TBITMAPINFO) + (sizeof(TRGBQUAD) * 256), #0);
      PBITMAPINFOHEADER(pDibHeader)^.biSize := sizeof(TBITMAPINFOHEADER);
      PBITMAPINFOHEADER(pDibHeader)^.biPlanes := 1;
      PBITMAPINFOHEADER(pDibHeader)^.biBitCount := 8;
      PBITMAPINFOHEADER(pDibHeader)^.biWidth := form1.width;
      PBITMAPINFOHEADER(pDibHeader)^.biHeight := form1.height;
      PBITMAPINFOHEADER(pDibHeader)^.biCompression := BI_RGB;
      GetDIBits(dc, MemBitmap, 0, form1.height, nil, TBitmapInfo(pDibHeader^), DIB_RGB_COLORS);
      hBits := GlobalAlloc(GHND, PBitmapInfoHeader(pDibHeader)^.BiSizeImage);
      pBits := GlobalLock(hBits);
      GetDIBits(dc, MemBitmap, 0, form1.height, pBits, PBitmapInfo(pDibHeader)^, DIB_RGB_COLORS);
      if isDcPalDevice = true then begin
        for i := 0 to (pPal^.PalNumEntries - 1) do begin
         PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed :=
           pPal^.palPalEntry[i].peRed;
         PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen :=
           pPal^.palPalEntry[i].peGreen;
         PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue :=
           pPal^.palPalEntry[i].peBlue;
       end;
       FreeMem(pPal, sizeof(TLOGPALETTE) +
              (255 * sizeof(TPALETTEENTRY)));
      end;
      ReleaseDc(0, dc);
      DeleteObject(MemBitmap);
      //Printer.Orientation := poLandscape;
      Printer.BeginDoc;
      if Printer.PageWidth < Printer.PageHeight then begin
         ScaleX := Printer.PageWidth;
         ScaleY := Form1.Height * (Printer.PageWidth / Form1.Width);
      end else begin
         ScaleX := Form1.Width * (Printer.PageHeight / Form1.Height);
         ScaleY := Printer.PageHeight;
      end;
      isDcPalDevice := false;
      if GetDeviceCaps(Printer.Canvas.Handle, RASTERCAPS) and
         RC_PALETTE = RC_PALETTE then begin
         GetMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
         FillChar(pPal^, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)), #0);
         pPal^.palVersion := $300;
         pPal^.palNumEntries := 256;
         for i := 0 to (pPal^.PalNumEntries - 1) do begin
             pPal^.palPalEntry[i].peRed := PBitmapInfo(pDibHeader)^.bmiColors[i].rgbRed;
             pPal^.palPalEntry[i].peGreen := PBitmapInfo(pDibHeader)^.bmiColors[i].rgbGreen;
             pPal^.palPalEntry[i].peBlue := PBitmapInfo(pDibHeader)^.bmiColors[i].rgbBlue;
         end;
         pal := CreatePalette(pPal^);
         FreeMem(pPal, sizeof(TLOGPALETTE) + (255 * sizeof(TPALETTEENTRY)));
         oldPal := SelectPalette(Printer.Canvas.Handle, Pal, false);
         isDcPalDevice := true
      end;
      StretchDiBits(Printer.Canvas.Handle, 0, 0, Round(scaleX), Round(scaleY), 0, 0,
                    Form1.Width, Form1.Height, pBits, PBitmapInfo(pDibHeader)^, DIB_RGB_COLORS, SRCCOPY);
      if isDcPalDevice = true then begin
         SelectPalette(Printer.Canvas.Handle, oldPal, false);
         DeleteObject(Pal);
      end;
      GlobalUnlock(hBits);
      GlobalFree(hBits);
      GlobalUnlock(hDibHeader);
      GlobalFree(hDibHeader);
      Printer.EndDoc;
    end;
    hsr///////////////////////////////////////////////