Re: Open, process & print documents without user interface

Tech-Archive recommends: Fix windows errors by optimizing your registry



My example came in the VS.NET 2003 MSDN

CPrintDialog::GetPrinterDC

Retrieves a handle to the printer device context.

HDC GetPrinterDC( ) const;

Return Value
A handle to the printer device context if successful; otherwise NULL.

Remarks
If the bPrintSetupOnly parameter of the CPrintDialog constructor was FALSE (indicating
that the Print dialog box is displayed), then GetPrinterDC returns a handle to the printer
device context. You must call the Windows DeleteDC function to delete the device context
when you are done using it.

Example

CPrintDialog dlg(FALSE);
if (dlg.DoModal() == IDOK)
{
// Get a handle to the printer device context (DC).
HDC hdc = dlg.GetPrinterDC();
ASSERT(hdc);

// Do something with the HDC...

// Clean up.
CDC::FromHandle(hdc)->DeleteDC();
}

On Sun, 30 Jul 2006 23:38:06 GMT, "David Ching" <dc@xxxxxxxxxxxxxxxxxxxxxx> wrote:


"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:k79qc2p15f7ensl4262jvbig0e89r8gref@xxxxxxxxxx
So the
question is, who has responsibility for the lifetime of the kernel object?
If you do,
then you must delete it, because it won't be deleted when a temporary
object is freed
during the OnIdle processing (the temporary object is seen as a proxy for
the kernel
object, which must remain untouched).

See the example shown under CPrintDialog::GetPrinterDC, which explicitly
illustrates the
need to delete the DC.

You're right about not using FromHandle() if you are responsible for
deleting the underlying GDI object. The CPrintDialog::GetPrinterDC() doc
doesn't say one way or the other if the returned HDC needs to be deleted. I
couldn't find any example in the MSDN that came with either VC6 or VC2003.

-- David

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Printer drivers and StretchDIBits calls
    ... StretchDIBits function is called for instance when printing from MSPaint. ... suppose it uses the printer device context. ... > Are you sure the HDC used in StretchDIBits is not a memory device context? ...
    (microsoft.public.development.device.drivers)
  • Re: Text Scaling in Metafile
    ... The problem may be with your choice of the reference device context. ... HDC hdcMetafile = CreateEnhMetaFile, ... SetWorldTransform(hdcMetafile, &xform); ... In the code I'm creating a font with size 1 and using ...
    (microsoft.public.win32.programmer.gdi)
  • Re: How do I get a device context from an indexed bitmap?
    ... function takes an hdc parameter itself - which is exactly what I was ... Use a memory device context. ... draw on it using GDI functions, such as RoundRect, etc? ... How do I get a device context handle to this bitmap so I can ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Printing to hDc in VBA
    ... object exposes a Printer Device Context the VBA object model, ... To get a valid hDC for the printer you would have to open the Printer ... Dialog ActiveX control to return the DC for the current default printer so I ... > but the PrintToHdc method specifies an integer for the hDc parameter. ...
    (comp.databases.ms-access)
  • Re: How to measure the length of a string in twips?
    ... > single-width border. ... >> ByVal hDC As Long, ByVal hObject As Long) As Long ... >> ' Select the font in to the device context, ...
    (microsoft.public.vb.syntax)