Mooseworks Software, LLC
Controls That Work
|
One question that comes up often is "How do I print from Windows CE?" There are a variety of answers to this question, usually depending on platform and printer hardware. The method we present here is through use of the PCL (Printer Control Language from Hewlett Packard) driver. The PCL driver is widely available in Windows CE devices. This code was tested on a 5" x 7" Windows CE 4.2 panel with a USB host port. Since the CE version was 4.2, the sample is done in Visual Studio 2003. The code should work in Visual Studio 2005 as well, however. The sample application simple prints a screen capture. It has an option for automatically fitting the image to the page. Most of the work is done using API calls, so there are extensive DllImport functions. The first step is to capture the screen image. We use the GetCapture API function to get the window handle, and then copy it to a bitmap. The code is shown below.
'Capture the window handle
'Get DC for entire screen (be sure to destroy later!)
Dim screenWidth As
Integer
If (Not
TypeOf (ctl) Is
Form) Then
Dim hBitmap As
IntPtr = CreateCompatibleBitmap(hdcInput, screenWidth, screenHeight)
Dim hdcOutput As
IntPtr = CreateCompatibleDC(hdcInput) Dim hbmOld As IntPtr = SelectObject(hdcOutput, hBitmap)
'Copy the image into the bitmap.... After some clean up, we load the device context (DC) for the printer using the PCL driver. In this case, we are using the PCL Inkjet device, and LPT1, which is actually the USB host port. Dim hPrinterDC As IntPtr = CreateDC("pcl.dll", "PCL Inkjet", "LPT1", IntPtr.Zero) We then get the printer page size in case we're auto sizing.
Dim pageSizeX As
Integer = GetDeviceCaps(hPrinterDC, HORZRES)
' page width in pixels We put our screen bitmap into a memory DC for transfer to the printer DC.
'Select bitmap into the generic DC. Then we start the print document. The di parameter is a DOCINFO struct that contains the document name and file name. Once the document is started we draw to the printers DC just like we would draw to any other DC. We use the StretchBlt API function to copy the image to the printer DC. StretchBlt allows us to resize the image. BitBlt could be used if resizing is not desired.
If (StartDoc(hPrinterDC, di) > 0)
Then
Dim bmpInfo As
BITMAP_STRUC = New BITMAP_STRUC
'Copy generic DC to the printer (destination) DC.
'Call end of page and end of document. And thats it. This print out will use the printers default setup, likely monochrome and portrait. To print in a non-default mode requires use of the ChangeDisplaySettingsEx API function and the DeviceMode structure. |
Products Graph Trend Graph Visual Dashboard Instrumentation User Entry Download Demos Graph Trend Graph Visual Dashboard Instrumentation User Entry Support Contact Testimonials Testimonials Awards Articles VS2005 Control Design Windows CE Printing Links About Us Home Contact News Legend |