Tuesday, April 24, 2012



Prevent Expand\collapse event of treeview and load our functionality, if shift button is pressed.

Add Event "PreviewMouseDown" for the treeview,


pTreeView->PreviewMouseDown += gcnew System::Windows::Input::MouseButtonEventHandler(this, &OnPreviewMouseDown);



void OnPreviewMouseDown( System::Object^ sender, System::Windows::Input::MouseButtonEventArgs^ e)
{

if(System::Windows::Input::Keyboard::IsKeyDown(System::Windows::Input::Key::LeftShift) ||System::Windows::Input::Keyboard::IsKeyDown(System::Windows::Input::Key::RightShift))
{
System::Windows::Controls::TreeView^ pTreeView   = nullptr;
System::Windows::DependencyObject^ pDependencyObject = (System::Windows::DependencyObject^)e->OriginalSource;

pTreeView = (System::Windows::Controls::TreeView^)sender;
FindAncestor(pDependencyObject,System::Windows::Controls::TreeViewItem::typeid);
if(pDependencyObject)
{
System::Windows::Controls::TreeViewItem^ pTreeViewItem = (System::Windows::Controls::TreeViewItem^)pDependencyObject;
int iType = m_pCtrlAdmin->GetTreeitemType( (int)pTreeViewItem->Tag );
if( pTreeViewItem->Items->Count > 0 )
{
SetTreeviewAllItemState(iType);
e->Handled = true;
}

}

}
}



void FindAncestor(System::Windows::DependencyObject^% pDependencyObject,System::Type^ pType)
{
if(pDependencyObject->GetType() == pType)
return;
else
{
pDependencyObject = System::Windows::Media::VisualTreeHelper::GetParent(pDependencyObject);
if(pDependencyObject)
FindAncestor(pDependencyObject,pType);
else
return;
}
}


void SetTreeviewAllItemState(int iType)
{
    //Write your functionality

}


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;


}


Load Print propertie from C++/CLI

Load Print propertie from C++/CLI

void OnProperties()
{
HANDLE hPrinter = NULL;
int sizeOfDevMode = 0;
DWORD dwRet = 0;
DEVMODE *pDevMode1 = NULL;
PRINTER_DEFAULTS pd;
BOOL bFlag;
LONG lFlag;

System::Drawing::Printing::PrinterSettings^ printerSettings = gcnew System::Drawing::Printing::PrinterSettings();
std::string pDevice = m_CWPFDlgPrintPreview->GetPrinterName();
System::String^ pPrinterName = gcnew System::String( pDevice.c_str() );

printerSettings->PrinterName = pPrinterName;

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

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

//pd.DesiredAccess = PRINTER_ALL_ACCESS;
pd.DesiredAccess = PRINTER_ACCESS_USE;
System::IntPtr thPrinter = (System::IntPtr)hPrinter;

if( !OpenPrinter(pPrinterName, thPrinter, pd) )
return;


IntPtr hDevMode = printerSettings->GetHdevmode();
IntPtr pDevMode = GlobalLock(hDevMode);
Int32 fMode = 0;
int sizeNeeded = DocumentPropertiesSize(pHandle, thPrinter, pPrinterName, pDevMode, pDevMode, fMode);
IntPtr devModeData = Marshal::AllocHGlobal(sizeNeeded);

//fMode = DM_OUT_BUFFER;

fMode = DM_IN_BUFFER | DM_PROMPT | DM_OUT_BUFFER;

DocumentProperties(pHandle, thPrinter, pDevice, devModeData, pDevMode, fMode);
GlobalUnlock(hDevMode);
printerSettings->SetHdevmode(devModeData);
printerSettings->DefaultPageSettings->SetHdevmode(devModeData);
GlobalFree(hDevMode);
Marshal::FreeHGlobal(devModeData);

}

Tuesday, August 16, 2011


For printing, in c++, we need to find the printer's aspect ratio. For get the printer/monitor's aspect ratio use this function


GetAspectRatio( HDC p_hDC, int& p_nAspectX, int& p_nAspectY )
{
//p_hDC is printer's /Monitor's handle

HDC l_hScreenDC = GetDC(NULL);
p_nAspectX = GetDeviceCaps( l_hScreenDC, LOGPIXELSX ) / GetDeviceCaps( p_hDC, LOGPIXELSX );
p_nAspectY = GetDeviceCaps( l_hScreenDC, LOGPIXELSY ) / GetDeviceCaps( p_hDC, LOGPIXELSY ) ;
}

Tuesday, June 14, 2011

RGB to long and Long to RGB

bool ReadColor(long nDecimal, int& nRVal,int& nGVal,int& nBVal)
{

/*nAVal = nDecimal & 0x000000FF;
nDecimal = nDecimal>>8;*/

nRVal = (nDecimal)& 0x000000FF;
nDecimal = nDecimal>>8;

nGVal = nDecimal & 0x000000FF;
nDecimal = nDecimal>>8;

nBVal = nDecimal & 0x000000FF;

return true;

}
bool CreateColor(long &nDecimal, int nRVal,int nGVal,int nBVal)
{


nDecimal = (nDecimal)& 0x00000000;
nDecimal = (nDecimal) | nBVal;
nDecimal = nDecimal<<8;
nDecimal = (nDecimal) | nGVal;
nDecimal = nDecimal<<8;
nDecimal = nDecimal | nRVal;
return true;



/*nDecimal = (nDecimal)& 0x00000000;
nDecimal = (nDecimal) | nRVal;
nDecimal = nDecimal<<8;
nDecimal = (nDecimal) | nGVal;
nDecimal = nDecimal<<8;
nDecimal = nDecimal | nBVal;
return true;*/
}

Wednesday, March 9, 2011

Store RGB values ( color values ) in a single variable and retrevie it back

bool ReadColor( long nDecimal, int& nRVal, int& nGVal, int& nBVal )
{

/*nAVal = nDecimal & 0x000000FF;
nDecimal = nDecimal>>8;*/

nRVal = (nDecimal)& 0x000000FF;
nDecimal = nDecimal>>8;

nGVal = nDecimal & 0x000000FF;
nDecimal = nDecimal>>8;

nBVal = nDecimal & 0x000000FF;

return true;

}

bool CreateColor(long &nDecimal, int nRVal, int nGVal, int nBVal )
{


nDecimal = (nDecimal)& 0x00000000;
nDecimal = (nDecimal) | nBVal;
nDecimal = nDecimal<<8;
nDecimal = (nDecimal) | nGVal;
nDecimal = nDecimal<<8;
nDecimal = nDecimal | nRVal;
return true;

}

Wednesday, October 6, 2010

Set DateFormat

We can set current application's date style using this in C++/CLI

System::Threading::Thread^ pThread;
System::Globalization::CultureInfo^ pCultureInfo;
pThread = System::Threading::Thread::CurrentThread;
pCultureInfo = (System::Globalization::CultureInfo^)pThread->CurrentCulture->Clone();
pCultureInfo->DateTimeFormat->ShortDatePattern = "dd.MM.yyyy";
pThread->CurrentCulture = pCultureInfo;

In WPF application, the same thing is doing
like

CultureInfo culutreInfo = System.Threading.Thread.CurrentThread.CurrentCulture.Clone() as CultureInfo;
culutreInfo.DateTimeFormat.ShortDatePattern = "MMM dd/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = culutreInfo;

or

Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";

Also we can set a particular datepicker's date format like this,

CultureInfo cultureInfo = new CultureInfo("en-US");
DateTimeFormatInfo dateInfo = new DateTimeFormatInfo();
dateInfo.ShortDatePattern = "MM/yyyy";
cultureInfo.DateTimeFormat = dateInfo;
radDatePicker1.Culture = cultureInfo;


or

CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
culture.DateTimeFormat.ShortDatePattern = "MMM/dd/yyyy";
datePicker1.Culture = culture;