home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / impdes / dessin.bas next >
Encoding:
BASIC Source File  |  1995-09-07  |  21.0 KB  |  376 lines

  1. '                                                                                                                                                                                                               '
  2. '                                                                                                                                                                                                                '
  3. 'DΘfinition des types                                                                                                                                                                                             '
  4. '                                                                                                                                                                                                                  '
  5. '                                                                                                                                                                                                                   '
  6.     Option Explicit
  7.     DefInt A-Z
  8.  
  9.     Type BITMAPINFOHEADER_Type
  10.     biSize As Long
  11.     biWidth As Long
  12.     biHeight As Long
  13.     biPlanes As Integer
  14.     biBitCount As Integer
  15.     biCompression As Long
  16.     biSizeImage As Long
  17.     biXPelsPerMeter As Long
  18.     biYPelsPerMeter As Long
  19.     biClrUsed As Long
  20.     biClrImportant As Long
  21.     End Type
  22.      
  23.     Type BITMAPINFO_Type
  24.     BitmapInfoHeader As BITMAPINFOHEADER_Type
  25.     bmiColors As String * 1024
  26.     End Type
  27.      
  28.     Type RectType
  29.     Left As Integer
  30.     Top As Integer
  31.     Right As Integer
  32.     Bottom As Integer
  33.     End Type
  34.     Type PointType
  35.     X As Integer
  36.     Y As Integer
  37.     End Type
  38. '                                                                                                                                                                                                               '
  39. '                                                                                                                                                                                                                '
  40. 'API                                                                                                                                                                                            '
  41. '                                                                                                                                                                                                                  '
  42. '                                                                                                                                                                                                                   '
  43.     Declare Function CreateCompatibleDC Lib "gdi" (ByVal hDC)
  44.     Declare Function GetWindowDC Lib "user" (ByVal hWnd)
  45.     Declare Function GetDC Lib "user" (ByVal hWnd)
  46.     Declare Function ReleaseDC Lib "user" (ByVal hWnd, ByVal hDC)
  47.     Declare Function DeleteDC Lib "gdi" (ByVal hDC)
  48.     ' Graphics related API
  49.     Declare Function BitBlt Lib "gdi" (ByVal hDC, ByVal X, ByVal Y, ByVal w, ByVal h, ByVal hDC, ByVal X, ByVal Y, ByVal o As Long)
  50.     Declare Function GetDIBits Lib "gdi" (ByVal hDC, ByVal hBitmap, ByVal nStartScan, ByVal nNumScans, ByVal LpBits As Long, BitmapInfo As BITMAPINFO_Type, ByVal wUsage)
  51.     Declare Function StretchDIBits Lib "gdi" (ByVal hDC, ByVal DestX, ByVal DestY, ByVal wDestWidth, ByVal wDestHeight, ByVal SrcX, ByVal SrcY, ByVal wSrcWidth, ByVal wSrcHeight, ByVal LpBits&, BitsInfo As BITMAPINFO_Type, ByVal wUsage, ByVal dwRop&)
  52.     ' General attribute related API
  53.     Declare Function GetDeviceCaps Lib "gdi" (ByVal hDC, ByVal nIndex)
  54.     Declare Function GetWindowRect Lib "user" (ByVal hWnd, lpRect As RectType)
  55.     Declare Function GetClientRect Lib "user" (ByVal hWnd, lpRect As RectType)
  56.     ' Memory allocation related API
  57.     Declare Function GlobalAlloc Lib "kernel" (ByVal wFlags, ByVal lMem&)
  58.     Declare Function GlobalLock Lib "kernel" (ByVal HMem) As Long
  59.     Declare Function GlobalUnlock Lib "kernel" (ByVal HMem)
  60.     Declare Function GlobalFree Lib "kernel" (ByVal HMem)
  61.     ' Graphics object related API
  62.     Declare Function CreateCompatibleBitmap Lib "gdi" (ByVal hDC, ByVal nWidth, ByVal nHeight)
  63.     Declare Function DeleteObject Lib "gdi" (ByVal hObject)
  64.     Declare Function SelectObject Lib "gdi" (ByVal hDC, ByVal hObject)
  65.     Declare Function ClientToScreen Lib "user" (ByVal hWnd, P As PointType)
  66.     Declare Function LPToDP Lib "gdi" (ByVal hDC, P As PointType, ByVal nCount)
  67. '                                                                                                                                                                                                               '
  68. '                                                                                                                                                                                                                '
  69. 'DΘfinition des Global Constantes                                                                                                                                                                                             '
  70. '                                                                                                                                                                                                                  '
  71. '                                                                                                                                                                                                                   '
  72.     Global Const HORZRES = 8
  73.     Global Const VERTRES = 10
  74.     Global Const SRCCOPY = &HCC0020
  75.     Global Const NEWFRAME = 1
  76.     Global Const BITSPIXEL = 12
  77.     Global Const PLANES = 14
  78.     Global Const BI_RGB = 0
  79.     Global Const BI_RLE8 = 1
  80.     Global Const BI_RLE4 = 2
  81.     Global Const DIB_PAL_COLORS = 1
  82.     Global Const DIB_RGB_COLORS = 0
  83.     Global Const GMEM_MOVEABLE = 2
  84.  
  85. Sub ImprimeGraphique (F As Form)
  86.     Dim R As Integer
  87. '                                                                                                                                                                                                                                                                                                                                                                                               '
  88. '                                                                                                                                                                                                                                                                                                                                                                                                '
  89. 'Cette procΘdure permet l'impression d'une fenΩtre                                                                                                                                                                                                                                                                                                                                                '
  90. '                                                                                                                                                                                                                                                                                                                                                                                                  '
  91. '                                                                                                                                                                                                                                                                                                                                                                                                   '
  92.     Printer.ScaleMode = 3
  93.     Screen.MousePointer = 11
  94.     Printer.Print ""
  95.     'Appel de la fonction                                                                                                                                                                                                                                                                                                                                                       '
  96.     R = PrintWindow(Printer.hDC, 100, 100, Printer.ScaleWidth - 200, Printer.ScaleHeight - 200, F.hWnd)
  97.     If Not R Then
  98.     MsgBox "Unable to print the form"
  99.     Else
  100.     Printer.EndDoc
  101.     End If
  102.     Screen.MousePointer = 0
  103. End Sub
  104.  
  105. Function PrintClient (ByVal hDC_Dest, ByVal DestX, ByVal DestY, ByVal hWnd_SrcWindow, Ratio As Integer)
  106. '                                                                                                                                                                                                                                                               '
  107. '                                                                                                                                                                                                                                                                '
  108. 'ProcΘdure permettant l'impression d'un contr⌠le sur l'imprimante                                                                                                                                                                                                 '
  109. '                                                                                                                                                                                                                                                                  '
  110. '                                                                                                                                                                                                                                                                   '
  111.     Dim Cr$
  112.     Dim hDC_Window As Integer
  113.     Dim HDC_Mem As Integer
  114.     Dim R As Integer
  115.     Dim Window_Width As Integer
  116.     Dim Window_Height As Integer
  117.     Dim R1 As Integer
  118.     Dim R2 As Integer
  119.     Dim ScreenWidth As Integer
  120.     Dim screenHeight As Integer
  121.     Dim HPrevBmp As Integer
  122.     Dim HBMP_Window As Integer
  123.     Dim Client_Width As Integer
  124.     Dim Client_Height As Integer
  125.     Dim XDiff As Integer
  126.     Dim YDiff As Integer
  127.     Dim HDC_MemClient As Integer
  128.     Dim HBMP_Client As Integer
  129.     Dim HBMPClientPrev As Integer
  130.     Dim BitsPerPixel As Integer
  131.     Dim ColorPlanes As Integer
  132.     Dim WidthRatio!
  133.     Dim HeightAspectRatio!
  134.     Dim PrintWidth As Integer
  135.     Dim PrintHeight As Integer
  136.     Dim BytesNeeded&
  137.     Dim HMem As Integer
  138.     Dim LpBits&
  139.     Dim R3 As Integer
  140.     Dim Rect As RectType, RectClient As RectType
  141.     Dim BitmapInfo As BITMAPINFO_Type
  142.     Dim pWindow As PointType, pClient As PointType, pDiff As PointType
  143.     Cr$ = Chr$(13)
  144.  
  145.     ' Get the DC for the entire window including the non-client area.
  146.     hDC_Window = GetWindowDC(hWnd_SrcWindow)
  147.     HDC_Mem = CreateCompatibleDC(hDC_Window)
  148.     
  149.     ' Get the pixel dimensions of the screen.
  150.     ScreenWidth = GetDeviceCaps(hDC_Window, HORZRES)
  151.     screenHeight = GetDeviceCaps(hDC_Window, VERTRES)
  152.  
  153.     ' Get the pixel dimensions of the window to be printed.
  154.     R = GetWindowRect(hWnd_SrcWindow, Rect)
  155.     Window_Width = Abs(Rect.Right - Rect.Left)
  156.     Window_Height = Abs(Rect.Bottom - Rect.Top)
  157.     
  158.     ' Create a bitmap compatible with the window DC.
  159.     HBMP_Window = CreateCompatibleBitmap(hDC_Window, Window_Width, Window_Height)
  160.  
  161.     ' Select the bitmap to hold the window image into the memory DC.
  162.     HPrevBmp = SelectObject(HDC_Mem, HBMP_Window)
  163.  
  164.     ' Copy the image of the window to the memory DC.
  165.     R1 = BitBlt(HDC_Mem, 0, 0, Window_Width, Window_Height, hDC_Window, 0, 0, SRCCOPY)
  166.     
  167.     ' Get the dimensions of the client area.
  168.     R = GetClientRect(hWnd_SrcWindow, RectClient)
  169.     Client_Width = Abs(RectClient.Right - RectClient.Left)
  170.     Client_Height = Abs(RectClient.Bottom - RectClient.Top)
  171.  
  172.     ' Calculate the pixel difference (x and y) between the upper-left corner of the non-client area and the upper-left corner of the client area.
  173.     pClient.X = RectClient.Left
  174.     pClient.Y = RectClient.Top
  175.     R = ClientToScreen(hWnd_SrcWindow, pClient)
  176.     XDiff = Abs(pClient.X - Rect.Left)
  177.     YDiff = Abs(pClient.Y - Rect.Top)
  178.     
  179.     ' Create a DC and bitmap to represent the client area of the window.
  180.     HDC_MemClient = CreateCompatibleDC(hDC_Window)
  181.     HBMP_Client = CreateCompatibleBitmap(hDC_Window, Client_Width, Client_Height)
  182.     HBMPClientPrev = SelectObject(HDC_MemClient, HBMP_Client)
  183.  
  184.     ' Bitblt client area of window to memory bitmap representing the client area.
  185.     R = BitBlt(HDC_MemClient, 0, 0, Client_Width, Client_Height, HDC_Mem, XDiff, YDiff, SRCCOPY)
  186.     
  187.     ' Reselect in the previous bitmap and select out the source image bitmap.
  188.     R = SelectObject(HDC_Mem, HPrevBmp)
  189.  
  190.     ' Delete the DC a and bitmap associated with the window.
  191.     R = DeleteDC(hDC_Window)
  192.     R = DeleteObject(HBMP_Window)
  193.     BitsPerPixel = GetDeviceCaps(HDC_MemClient, BITSPIXEL)
  194.     ColorPlanes = GetDeviceCaps(HDC_MemClient, PLANES)
  195.     BitmapInfo.BitmapInfoHeader.biSize = 40
  196.     BitmapInfo.BitmapInfoHeader.biWidth = Client_Width
  197.     BitmapInfo.BitmapInfoHeader.biHeight = Client_Height
  198.     BitmapInfo.BitmapInfoHeader.biPlanes = 1
  199.     BitmapInfo.BitmapInfoHeader.biBitCount = BitsPerPixel * ColorPlanes
  200.     BitmapInfo.BitmapInfoHeader.biCompression = BI_RGB
  201.     BitmapInfo.BitmapInfoHeader.biSizeImage = 0
  202.     BitmapInfo.BitmapInfoHeader.biXPelsPerMeter = 0
  203.     BitmapInfo.BitmapInfoHeader.biYPelsPerMeter = 0
  204.     BitmapInfo.BitmapInfoHeader.biClrUsed = 0
  205.     BitmapInfo.BitmapInfoHeader.biClrImportant = 0
  206.     
  207.     ' Calculate the ratios based on the source and destination devices. This will help to cause the size of the window image to
  208.     ' be approximately the same proportion on another device such as a printer.
  209.     PrintWidth = Ratio * Window_Width
  210.     PrintHeight = Ratio * Window_Height
  211.     ' Calculate the number of bytes needed to store the image assuming 8 bits/pixel.
  212.     BytesNeeded& = CLng(Window_Width + 1) * (Window_Height + 1)
  213.  
  214.     ' Allocate a buffer to hold the bitmap bits.
  215.     HMem = GlobalAlloc(GMEM_MOVEABLE, BytesNeeded&)
  216.     If hDC_Window <> 0 And HBMP_Window <> 0 And hDC_Dest <> 0 And HMem <> 0 Then
  217.     LpBits& = GlobalLock(HMem)
  218.     ' Get the bits that make up the image and copy them to the
  219.     ' destination device.
  220.     R2 = GetDIBits(HDC_MemClient, HBMP_Client, 0, Client_Height, LpBits&, BitmapInfo, DIB_RGB_COLORS)
  221.     R3 = StretchDIBits(hDC_Dest, DestX, DestY, PrintWidth, PrintHeight, 0, 0, Client_Width, Client_Height, LpBits&, BitmapInfo, DIB_RGB_COLORS, SRCCOPY)
  222.     End If
  223.     
  224.     ' Select in the previous bitmap.
  225.     R = SelectObject(HDC_MemClient, HBMPClientPrev)
  226.  
  227.     ' Release or delete DC's, memory and objects.
  228.     R = GlobalUnlock(HMem)
  229.     R = GlobalFree(HMem)
  230.     R = DeleteDC(HDC_MemClient)
  231.     R = DeleteObject(HBMP_Client)
  232.     R = ReleaseDC(hWnd_SrcWindow, hDC_Window)
  233.      
  234.     ' Return true if the window was successfully printed.
  235.     If R2 <> 0 And R3 <> 0 Then
  236.     PrintClient = True
  237.     Else
  238.     PrintClient = False
  239.     End If
  240.  
  241. End Function
  242.  
  243. Function PrintWindow (ByVal hDC_Dest, ByVal DestX, ByVal DestY, ByVal DestDevWidth, ByVal DestDevHeight, ByVal hWnd_SrcWindow)
  244. '                                                                                                                                                                                                                                                               '
  245. '                                                                                                                                                                                                                                                                '
  246. 'ProcΘdure permettant l'impression d'une fenΩtre sur l'imprimante                                                                                                                                                                                                 '
  247. '                                                                                                                                                                                                                                                                  '
  248. '                                                                                                                                                                                                                                                                   '
  249.     Dim Rect As RectType
  250.     Dim BitmapInfo As BITMAPINFO_Type
  251.     Dim Ratio As Single
  252.     Dim Cr$
  253.     Dim hDC_Window As Integer
  254.     Dim HDC_Mem As Integer
  255.     Dim R As Integer
  256.     Dim Window_Width As Integer
  257.     Dim Window_Height As Integer
  258.     Dim R1 As Integer
  259.     Dim R2 As Integer
  260.     Dim ScreenWidth As Integer
  261.     Dim screenHeight As Integer
  262.     Dim HPrevBmp As Integer
  263.     Dim HBMP_Window As Integer
  264.     Dim Client_Width As Integer
  265.     Dim Client_Height As Integer
  266.     Dim XDiff As Integer
  267.     Dim YDiff As Integer
  268.     Dim HDC_MemClient As Integer
  269.     Dim HBMP_Client As Integer
  270.     Dim HBMPClientPrev As Integer
  271.     Dim BitsPerPixel As Integer
  272.     Dim ColorPlanes As Integer
  273.     Dim WidthRatio!
  274.     Dim HeightAspectRatio!
  275.     Dim PrintWidth As Integer
  276.     Dim PrintHeight As Integer
  277.     Dim BytesNeeded&
  278.     Dim HMem As Integer
  279.     Dim LpBits&
  280.     Dim R3 As Integer
  281.     Cr$ = Chr$(13)
  282.     
  283.     ' Get the DC for the entire window including the non-client area.
  284.     hDC_Window = GetWindowDC(hWnd_SrcWindow)
  285.     HDC_Mem = CreateCompatibleDC(hDC_Window)
  286.     
  287.     ' Get the pixel dimensions of the screen.  This is necessary so
  288.     ' that we can determine the relative size of the window compared to
  289.  
  290.     ' the screen
  291.     ScreenWidth = GetDeviceCaps(hDC_Window, HORZRES)
  292.     screenHeight = GetDeviceCaps(hDC_Window, VERTRES)
  293.  
  294.     ' Get the pixel dimensions of the window to be printed.
  295.     R = GetWindowRect(hWnd_SrcWindow, Rect)
  296.     Window_Width = Abs(Rect.Right - Rect.Left)
  297.     Window_Height = Abs(Rect.Bottom - Rect.Top)
  298.     
  299.     ' Create a bitmap compatible with the window DC.
  300.     HBMP_Window = CreateCompatibleBitmap(hDC_Window, Window_Width, Window_Height)
  301.  
  302.     ' Select the bitmap to hold the window image into the memory DC.
  303.  
  304.     HPrevBmp = SelectObject(HDC_Mem, HBMP_Window)
  305.  
  306.     ' Copy the image of the window to the memory DC.
  307.     R1 = BitBlt(HDC_Mem, 0, 0, Window_Width, Window_Height, hDC_Window, 0, 0, SRCCOPY)
  308.  
  309.     BitsPerPixel = GetDeviceCaps(HDC_Mem, BITSPIXEL)
  310.     ColorPlanes = GetDeviceCaps(HDC_Mem, PLANES)
  311.  
  312.     BitmapInfo.BitmapInfoHeader.biSize = 40
  313.     BitmapInfo.BitmapInfoHeader.biWidth = Window_Width
  314.     BitmapInfo.BitmapInfoHeader.biHeight = Window_Height
  315.     BitmapInfo.BitmapInfoHeader.biPlanes = 1
  316.     BitmapInfo.BitmapInfoHeader.biBitCount = BitsPerPixel * ColorPlanes
  317.     BitmapInfo.BitmapInfoHeader.biCompression = BI_RGB
  318.  
  319.     BitmapInfo.BitmapInfoHeader.biSizeImage = 0
  320.     BitmapInfo.BitmapInfoHeader.biXPelsPerMeter = 0
  321.     BitmapInfo.BitmapInfoHeader.biYPelsPerMeter = 0
  322.     BitmapInfo.BitmapInfoHeader.biClrUsed = 0
  323.     BitmapInfo.BitmapInfoHeader.biClrImportant = 0
  324.     
  325.     ' Calculate the ratios based on the source and destination
  326.     ' devices. This will help to cause the size of the window image
  327.     ' to be approximately the same proportion on another device
  328.     ' such as a printer.
  329.     WidthRatio! = DestDevWidth / Window_Width
  330.     HeightAspectRatio! = DestDevHeight / Window_Height
  331.     If WidthRatio! > HeightAspectRatio! Then Ratio = HeightAspectRatio!:  Else Ratio = WidthRatio!
  332.  
  333.     PrintWidth = Ratio * Window_Width
  334.     PrintHeight = Ratio * Window_Height
  335.  
  336.     ' Calculate the number of bytes needed to store the image assuming
  337.     ' 8 bits/pixel.
  338.  
  339.     BytesNeeded& = CLng(Window_Width + 1) * (Window_Height + 1)
  340.  
  341.     ' Allocate a buffer to hold the bitmap bits.
  342.     HMem = GlobalAlloc(GMEM_MOVEABLE, BytesNeeded&)
  343.     
  344.     If hDC_Window <> 0 And HBMP_Window <> 0 And hDC_Dest <> 0 And HMem <> 0 Then
  345.     
  346.     LpBits& = GlobalLock(HMem)
  347.     
  348.     ' Get the bits that make up the image and copy them to the
  349.     ' destination device.
  350.     R2 = GetDIBits(HDC_Mem, HBMP_Window, 0, Window_Height, LpBits&, BitmapInfo, DIB_RGB_COLORS)
  351.  
  352.     R3 = StretchDIBits(hDC_Dest, DestX, DestY, PrintWidth, PrintHeight, 0, 0, Window_Width, Window_Height, LpBits&, BitmapInfo, DIB_RGB_COLORS, SRCCOPY)
  353.     End If
  354.  
  355.     ' Reselect in the previous bitmap and select out the source
  356.     ' image bitmap.
  357.     R = SelectObject(HDC_Mem, HPrevBmp)
  358.  
  359.     ' Release or delete DC's, memory and objects.
  360.     R = GlobalUnlock(HMem)
  361.     R = GlobalFree(HMem)
  362.     R = DeleteDC(hDC_Window)
  363.     R = DeleteObject(HBMP_Window)
  364.     R = ReleaseDC(hWnd_SrcWindow, hDC_Window)
  365.  
  366.     
  367.     ' Return true if the window was successfully printed.
  368.     If R2 <> 0 And R3 <> 0 Then
  369.     PrintWindow = True
  370.     Else
  371.     PrintWindow = False
  372.     End If
  373.  
  374. End Function
  375.  
  376.