locked
How can I get lines drawn on screen into a document for printing ?

    Question

  • void SimpleTextRenderer::blockprinter()
    {
    
    	  // Call static method to get PrintManager object.
        PrintManager^ m_printManager = Windows::Graphics::Printing::PrintManager::GetForCurrentView();
    
        // Start print task event.
        m_printManager->PrintTaskRequested +=
    		ref new TypedEventHandler<PrintManager^, PrintTaskRequestedEventArgs^>(this, &SimpleTextRenderer::SetPrintTask);
    
    
    
    	 D2DFactoryLock factoryLock(m_d2dFactory.Get());
    
    	 
    
    }
    
    
    
    void SimpleTextRenderer::SetPrintTask(
        _In_ PrintManager^ /*sender*/,
        _In_ PrintTaskRequestedEventArgs^ args
        )
    {
       PrintTaskSourceRequestedHandler^ sourceHandler =
            ref new PrintTaskSourceRequestedHandler([this](PrintTaskSourceRequestedArgs^ args)-> void{
                
    			
                   // Here CDocumentSource is inherited from IPrintDocumentSource, cast it back to an object.
                IPrintDocumentSource^ objSource(
    				reinterpret_cast<IPrintDocumentSource^>(m_d2dContext.Get())
                    );
    
    
    
    
        args->SetSource(objSource);
    	
    
    
       &SimpleTextRenderer::PrintRender;
    
       
       });
    
    		
    
    
    
    
    
        // Request initializing print task.
        PrintTask^ printTask = args->Request->CreatePrintTask(
            L"Direct2D image and effect printing sample",
            sourceHandler
            );
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    


    n.Wright

    Wednesday, August 21, 2013 10:08 PM

All replies

  • Hi W,

    I'm not sure exactly what you are asking here.

    You cannot scrape the screen to print what is on it, but you can print your on renderings.

    See Direct2D printing from Windows Store apps for more details.

    --Rob

    Thursday, August 22, 2013 8:41 PM
    Owner
  • I understand what you are saying.

    I have tried to integrate the MS examples into my code but fail every time miserably.

    All sorts of errors occur. The compiler gets very upset at my adding printing code.

    I am using the SimpleTextRenderer code example to encapsulate my own program.

    From there I want to be able to print.

    The examples have bits of code all over the place. When I did it with wpf it was pretty straight forward and I had no problems.


    n.Wright

    Friday, August 23, 2013 1:25 AM
  • I got a bit further with integrating the printer code.

    However it crashes so it needs a bit more work.

    // RAII (Resource Acquisition Is Initialization) class for manually
    // acquiring/releasing the D2D lock.
    class D2DFactoryLock
    {
    public:
        D2DFactoryLock(_In_ ID2D1Factory* d2dFactory)
        {
            DX::ThrowIfFailed(
                d2dFactory->QueryInterface(IID_PPV_ARGS(&m_d2dMultithread))
                );
    
            m_d2dMultithread->Enter();
        }
    
        ~D2DFactoryLock()
        {
            m_d2dMultithread->Leave();
        }
    
    private:
        Microsoft::WRL::ComPtr<ID2D1Multithread> m_d2dMultithread;
    };
    
    
    
    
    
    void SimpleTextRenderer::blockprinter()
    {
    
    
    
    	  D2D1_RECT_F textBox = D2D1::RectF(
        0,
        0,
        12000,
        12000
        );
    
    	  D2D1_SIZE_F pageSize = D2D1::SizeF(
        12000, 12000);
    
    
    
            // If a page-level print ticket is not specified here, 
            // the package print ticket is applied for each page.
            PrintPage(
                1,
                textBox,
                pageSize,
                nullptr 
                );
       
    
    HRESULT hrClose = ClosePrintControl();
    
    }
    
    
    HRESULT SimpleTextRenderer::ClosePrintControl()
    {
        return (m_d2dPrintControl == nullptr) ? S_OK : m_d2dPrintControl->Close();
    }
    
    
    IFACEMETHODIMP
    	SimpleTextRenderer::MakeDocument(
        _In_ IInspectable*                docOptions,
        _In_ IPrintDocumentPackageTarget* docPackageTarget
        )
    {
            // ...
            // Get print settings from PrintTaskOptions for printing, 
            // such as page description, which contains page size, printable area, and DPI.
            PrintTaskOptions^ option = reinterpret_cast<PrintTaskOptions^>(docOptions);
            PrintPageDescription pageDesc = option->GetPageDescription(1); // Get the description of the first page.
    
            // ...
    
    }
    
    
    
    void SimpleTextRenderer::PrintPage(
        _In_ uint32                 /*pageNumber*/,
        _In_ D2D1_RECT_F            imageableArea,
        _In_ D2D1_SIZE_F            pageSize,
        _In_opt_ IStream*           pagePrintTicketStream
        )
    {
        // Create a new D2D device context for generating the print command list.
        // D2D device contexts are stateful, and hence a unique device context must
        // be used on each thread.
        ComPtr<ID2D1DeviceContext> d2dContext;
        DX::ThrowIfFailed(
            m_d2dDevice->CreateDeviceContext(
                D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
                &d2dContext
                )
            );
    
        ComPtr<ID2D1CommandList> printCommandList;
        DX::ThrowIfFailed(
            d2dContext->CreateCommandList(&printCommandList)
            );
    
        d2dContext->SetTarget(printCommandList.Get());
    
        // Create and initialize the page renderer context for print.
        // In this case, we want to use the bitmap source that already has
        // the color context embedded in it. Thus, we pass NULL for the
        // color context parameter.
    
    	
    				D2D1_RECT_F rc;	
               0;
    			0;
    			12000;
    			12000;
    	
    	
    	SimpleTextRenderer^ printPageRendererContext =
            ref new SimpleTextRenderer();
    
        d2dContext->BeginDraw();
    
        // Draw page content on a command list.
        Draw(1);
    
        // The document source handles D2DERR_RECREATETARGET, so it is okay to throw this error
        // here.
        DX::ThrowIfFailed(
            d2dContext->EndDraw()
            );
    
        DX::ThrowIfFailed(
            printCommandList->Close()
            );
    
        DX::ThrowIfFailed(
            m_d2dPrintControl->AddPage(printCommandList.Get(), pageSize, pagePrintTicketStream)
            );
    		
    
    
    		}
    
    
    
    
    
    
    // Draws the scene to a rendering device context or a printing device context.
    void SimpleTextRenderer::Draw(_In_ float scale)
    {
        // Clear rendering background with CornflowerBlue and clear preview
        // background with white color. For the printing case (command list), it
        // is recommended not to clear because the surface is clean when created.
        
            m_d2dContext->Clear(D2D1::ColorF(D2D1::ColorF::CornflowerBlue));
       
    
        // We use scale matrix to shrink the text size and scale is only available
        // for preview.  For on-screen rendering or printing, scale is 1.f, that
        // is, the Identity is the transform matrix.
        m_d2dContext->SetTransform(D2D1::Matrix3x2F(1/scale, 0, 0, 1/scale, 0, 0));
    
        D2D1_RECT_F textBox =
            D2D1::RectF(
                0,
                0,
                5000,
                5000
                );
    
        const char16 textString[] = L"\
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, \
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris \
    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in \
    reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla\
    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in \
    culpa qui officia deserunt mollit anim id est laborum.";
    
        m_d2dContext->DrawText(
            textString,
            ARRAYSIZE(textString) - 1,
            m_textFormat.Get(),
            textBox,
            m_blackBrush.Get()
            );
    }
    


    n.Wright

    Saturday, August 24, 2013 1:22 AM