Printing a control 
Author Message
 Printing a control

Hi,

Can anybody give me pointers on how to print a control as it appears on a
form (to a printer). Specifically the MSChart activex control (I am
producing a graph and I need to be able to print it).

Any help appreciated.

Jess



Sun, 27 Nov 2005 19:53:51 GMT  
 Printing a control
Jess,

        This procedure will get create a bitmap of a window or control given
its hwnd. Create a printdocument and print the bitmap.

api declares.
Private Structure RECT

    Public Left As Integer

    Public Top As Integer

    Public Right As Integer

    Public Bottom As Integer

End Structure

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Integer,
_

    ByRef lpRect As RECT) As Integer

Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" _

    (ByVal hDestDC As Integer, ByVal x As Integer, _

    ByVal y As Integer, ByVal nWidth As Integer, _

    ByVal nHeight As Integer, ByVal hSrcDC As Integer, _

    ByVal xSrc As Integer, ByVal ySrc As Integer, _

    ByVal dwRop As Integer) As Integer

Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" _

    (ByVal hwnd As Integer) As Integer

Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Integer, _

    ByVal hdc As Integer) As Integer

Public Const SRCCOPY As Integer = &HCC0020

procedure
Private Function GetWindowPicture(ByVal hWnd As Integer) As Bitmap

    Dim g As Graphics

    Dim hdcDest As IntPtr

    Dim hdcSrc As Integer

    Dim bm As Bitmap

    Dim r As New RECT

    Dim w, h As Integer

    GetWindowRect(hWnd, r)

    w = r.Right - r.Left

    h = r.Bottom - r.Top

    bm = New Bitmap(w, h)

    g = g.FromImage(bm)

    hdcSrc = GetWindowDC(hWnd)

    hdcDest = g.GetHdc

    BitBlt( _

        hdcDest.ToInt32, 0, 0, w, h, hdcSrc, 0, 0, SRCCOPY)

    g.ReleaseHdc(hdcDest)

    ReleaseDC(hWnd, hdcSrc)

    Return bm

End Function

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e
As System.Drawing.Printing.PrintPageEventArgs) Handles
PrintDocument1.PrintPage

    Dim g As Graphics = e.Graphics

    g.DrawImage(GetWindowPicture(AxMSChart1.Handle.ToInt32), 0, 0)

End Sub

Ken
---------------

Quote:
> Hi,

> Can anybody give me pointers on how to print a control as it appears on a
> form (to a printer). Specifically the MSChart activex control (I am
> producing a graph and I need to be able to print it).

> Any help appreciated.

> Jess



Sun, 27 Nov 2005 20:21:13 GMT  
 Printing a control
Hi Jess,



Quote:
> Hi,

> Can anybody give me pointers on how to print a control as it appears on a
> form (to a printer). Specifically the MSChart activex control (I am
> producing a graph and I need to be able to print it).

> Any help appreciated.

> Jess

Here's some code I found in the net (with some modifications):

Private Type RECTL
   Left As Long
   Top As Long
   right As Long
   Bottom As Long
End Type

Private Declare Function OleDraw Lib "ole32.dll" _
        (ByVal pUnk As Object, ByVal dwAspect As Long, _
        ByVal hdcDraw As Long, lprcBounds As RECTL) As Long

Dim lngRet As Long
    Dim rc As RECTL
    rc.Left = 0
    rc.Top = 0
    rc.right = 1500
    rc.Bottom = 1500

    Printer.Print " "
    lngRet = OleDraw(MsChart1.Object, 1, Printer.hDC, rc)
    Printer.EndDoc

the API function OleDraw draws your control (and every other control) to
your printer or even to another window if you use the hDC of this window (I
never tried this but the supplier of this code told that this is possible.
The settings for 'rc' are somehow arbitrary and you have to find another
boundaries for you rectangle (rc specifies a rectangle that the object is
drawn into) you'll have to experiment with Printer.ScaleWidth,
Mschart.Width. For my needs the following is sufficient:

    MsChart1.Width = 1.5 * MsChart1.Height
    'I don't know if this is necessary but I set it because I want an aspect
ratio of 2:3

    Printer.ScaleMode = vbTwips
    Dim co As Double
    co = Printer.Width / MsChart1.Width / 2.55
    '2.55 seems to be a good value for me

    Dim lngRet As Long
    Dim rc As RECTL
    rc.Left = 0
    rc.Top = 0
    rc.right = MsChart1.Width * co
    rc.Bottom = MsChart1.Height * co

Main advantage of this is that every scalable component of the control is
printed out scalable (vector format). That's very fine if you want to print
to PDF printer device because you get small file sizes (in the contrary to
blitting some bitmap formats)

Hope this helps

Christian



Sat, 10 Dec 2005 14:24:24 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. PROBLEM PRINTING FROM PRINT PREVIEW CONTROL

2. Printing and using the Print Preview Control?

3. Controlling the Print Preview Control

4. Conditionally print a control

5. Print Preview Control

6. Print Preview Control

7. Print Preview Control

8. Printing mschart control - almost there!

9. Print Dialog Control

10. Printing ListView Control

11. Printing webbrowser control -how

12. Printing ListView Control

 

 
Powered by phpBB® Forum Software