Thursday, October 27, 2011

Paper setting and read it in C++/CLI

Paper setting and read it in C++/CLI

[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesW", SetLastError = true, ExactSpelling = true, CallingConvention = System::Runtime::InteropServices::CallingConvention::StdCall)]
static int DocumentProperties( System::IntPtr hwnd, System::IntPtr hPrinter, [MarshalAs(UnmanagedType::LPWStr)] System::String^ pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);

void OnBtnPropertiesClick( Object^ sender, RoutedEventArgs^ e )
{

System::Windows::Controls::ComboBoxItem^ pComboBoxItem = nullptr;
System::Printing::PrintQueue^ CurrentPrinter = nullptr;

pComboBoxItem = (System::Windows::Controls::ComboBoxItem^)m_CWPFDlgPrintPreview->m_CWPFDlgPrintPreview->CmbPrinter->SelectedItem;
if( pComboBoxItem->Tag != nullptr )
{
CurrentPrinter = (System::Printing::PrintQueue^)pComboBoxItem->Tag;
}

if( CurrentPrinter )
{
HWND pHWND = NULL;
System::IntPtr pHandle = System::IntPtr::Zero;
pHWND = m_CWPFDlgPrintPreview->GetHWND();

if( pHWND )
pHandle = (System::IntPtr)pHWND;

System::Printing::Interop::PrintTicketConverter^ PrintTicketConverter = nullptr;
PrintTicketConverter = gcnew System::Printing::Interop::PrintTicketConverter( CurrentPrinter->FullName, CurrentPrinter->ClientPrintSchemaVersion );
array ^myDevMode = PrintTicketConverter->ConvertPrintTicketToDevMode(CurrentPrinter->UserPrintTicket, System::Printing::Interop::BaseDevModeType::UserDefault);
System::Runtime::InteropServices::GCHandle^ pinnedDevMode = System::Runtime::InteropServices::GCHandle::Alloc(myDevMode, GCHandleType::Pinned);
System::IntPtr pDevMode = pinnedDevMode->AddrOfPinnedObject();
int result = DocumentProperties(pHandle, IntPtr::Zero, CurrentPrinter->FullName, pDevMode, pDevMode, 14);

if (result == 1)
{
CurrentPrinter->UserPrintTicket = PrintTicketConverter->ConvertDevModeToPrintTicket(myDevMode);
pinnedDevMode->Free();
// int PrintCopyCount = CurrentPrinter->UserPrintTicket->CopyCount->Value;
// SetPageOrientation(CurrentPrinter.UserPrintTicket.PageOrientation);
//SetCurrentPaper(CurrentPrinter->UserPrintTicket->PageMediaSize);
// ExecuteSetPrintingOptions(null);

m_CWPFDlgPrintPreview->PreparePrintContent();


}
}



void* WPFDialog::CWPFDlgPrintPreview::GetPrintQueue()
{

System::Runtime::InteropServices::GCHandle pGCHandle;
System::Windows::Controls::ComboBoxItem^ pComboBoxItem = nullptr;
System::Printing::PrintQueue^ l_pPrintQueue = nullptr;
System::Object^ pObject = nullptr;
void* pPrintQueueVoid;

if( m_CWPFDlgPrintPreview->CmbPrinter->SelectedIndex >= 0 )
{
pComboBoxItem = (System::Windows::Controls::ComboBoxItem^)m_CWPFDlgPrintPreview->CmbPrinter->SelectedItem;

if( pComboBoxItem->Tag != nullptr )
l_pPrintQueue = (System::Printing::PrintQueue^)pComboBoxItem->Tag;
}

if( l_pPrintQueue )
{
pObject = (System::Object^)l_pPrintQueue;

if( pObject )
{
pPrintQueueVoid = GCHandle::ToIntPtr(GCHandle::Alloc(pObject)).ToPointer();
}
}

return pPrintQueueVoid;


}


No comments: