Hi,
Here I create custom WPF command button using C++/CLI.
//.h file
#pragma once
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
ref class MyButton: public System::Windows::Controls::Button
{
public:
MavisButton();
~MavisButton();
void SetControl();
};
//.cpp file
#include "MyButton.h"
MyButton::MyButton()
{
this->SetControl();
}
MyButton::~MyButton()
{
}
void MyButton::SetControl()
{
System::Windows::Controls::ControlTemplate^ pControlTemplate;
System::Drawing::ColorConverter^ pColorConverter;
System::Windows::Media::RadialGradientBrush^ pGradient_HoverGlassBackgroundFill;
System::Windows::Media::LinearGradientBrush^ pHighlightFill;
System::Windows::Media::LinearGradientBrush^ pBorderStroke;
System::Windows::Setter^ pSetter;
System::Windows::Style^ pStyle;
System::Windows::Trigger^ tg_IsMouseOver;
System::Windows::Trigger^ tg_IsPressed;
System::Windows::Setter^ str_IsPressed;
System::Windows::Setter^ str_IsMouseOver;
//-----------------------------------------------GlassBackgroundFill-------------------------------------------------------------
pColorConverter = gcnew System::Drawing::ColorConverter();
System::Windows::Media::LinearGradientBrush^ gradient_GlassBackgroundFill = gcnew System::Windows::Media::LinearGradientBrush();
gradient_GlassBackgroundFill->StartPoint = System::Windows::Point(0, 0);
gradient_GlassBackgroundFill->EndPoint = System::Windows::Point(0, 1);
gradient_GlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FF4B58" )), 0) );
gradient_GlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#A91E28" )), 1) );
//--------------------------------------------------HoverGlassBackgroundFill-------------------------------------------------------------
pGradient_HoverGlassBackgroundFill = gcnew System::Windows::Media::RadialGradientBrush();
pGradient_HoverGlassBackgroundFill->Center = System::Windows::Point(0.5, 0.5);
pGradient_HoverGlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#a91e28" )), 0 ));
pGradient_HoverGlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#ff4b58" )), 1 ));
//-------------------------------------------HighlightFill------------------------------------------------------------
pHighlightFill = gcnew System::Windows::Media::LinearGradientBrush();
pHighlightFill->StartPoint = System::Windows::Point(0, 0);
pHighlightFill->EndPoint = System::Windows::Point(0, 1);
pHighlightFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFFFFF" )), 0 ));
pHighlightFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#00000000" )), 1 ));
//-----------------------------------------------------BorderStroke-------------------------------------------------------
pBorderStroke = gcnew System::Windows::Media::LinearGradientBrush();
pBorderStroke->StartPoint = System::Windows::Point(0, 0);
pBorderStroke->EndPoint = System::Windows::Point(0, 1);
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#c7c7c7" )), 0 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#b0b0b0" )), 1 ));
//--------------------------------------------------------------------------------------------------
pControlTemplate = gcnew System::Windows::Controls::ControlTemplate();
pControlTemplate->TargetType = (gcnew System::Windows::Controls::Button)->GetType();
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryGrid = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Grid)->GetType() );
pFrameworkElementFactoryGrid->Name ="Grid";
pFrameworkElementFactoryGrid->SetValue( System::Windows::Controls::Grid::RenderTransformOriginProperty, System::Windows::Point(0.5, 0.5));
//--------------------------------------------------------------------------------------------------------------
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBorder = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Border)->GetType() );
pFrameworkElementFactoryBorder->Name = "Outline";
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::CornerRadiusProperty, gcnew System::Windows::CornerRadius(12.0));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BackgroundProperty, System::Windows::Media::Brushes::Red);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderBrushProperty, pBorderStroke ) ;
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderThicknessProperty, gcnew System::Windows::Thickness(1));
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBackground = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Shapes::Rectangle)->GetType() );
pFrameworkElementFactoryBackground->Name = "GlassBackground";
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::MarginProperty , System::Windows::Thickness(1, 1, 2, 2));
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::RadiusXProperty, 9.0 );
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::RadiusYProperty, 9.0 ) ;
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::FillProperty, gradient_GlassBackgroundFill );
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryHighlight = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Shapes::Rectangle)->GetType() );
pFrameworkElementFactoryHighlight->Name = "Highlight";
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::MarginProperty , System::Windows::Thickness(1, 1, 2, 2));
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::RadiusXProperty, 9.0 );
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::RadiusYProperty, 9.0 ) ;
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::OpacityProperty, 1.0 ) ;
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::FillProperty, pHighlightFill );
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryContentPresenter = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::ContentPresenter)->GetType() );
pFrameworkElementFactoryContentPresenter->SetValue ( System::Windows::Controls::ContentPresenter::VerticalAlignmentProperty, System::Windows::VerticalAlignment::Center );
pFrameworkElementFactoryContentPresenter->SetValue ( System::Windows::Controls::ContentPresenter::HorizontalAlignmentProperty, System::Windows::HorizontalAlignment::Center );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryBorder );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryBackground );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryHighlight );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryContentPresenter );
pControlTemplate->VisualTree = pFrameworkElementFactoryGrid;
//------------------------------------------------ControlTemplate->Triggers->IsMouseOver---------------------------------------------------------
tg_IsMouseOver = gcnew System::Windows::Trigger();
tg_IsMouseOver->Property = System::Windows::UIElement::IsMouseOverProperty;
tg_IsMouseOver->Value = true;
str_IsMouseOver = gcnew System::Windows::Setter();
str_IsMouseOver->TargetName = "GlassBackground";
str_IsMouseOver->Property = System::Windows::Shapes::Rectangle::FillProperty;
str_IsMouseOver->Value = pGradient_HoverGlassBackgroundFill;
tg_IsMouseOver->Setters->Add( str_IsMouseOver );
pControlTemplate->Triggers->Add ( tg_IsMouseOver );
//------------------------------------------------ControlTemplate.Triggers.IsPressed---------------------------------------------------------
tg_IsPressed = gcnew System::Windows::Trigger();
tg_IsPressed->Property = System::Windows::Controls::Button::IsPressedProperty;
tg_IsPressed->Value = true;
str_IsPressed = gcnew System::Windows::Setter();
str_IsPressed->TargetName = "GlassBackground";
str_IsPressed->Property = System::Windows::Shapes::Rectangle::FillProperty;
str_IsPressed->Value = System::Windows::Media::Brushes::Red;
tg_IsPressed->Setters->Add( str_IsPressed );
pControlTemplate->Triggers->Add ( tg_IsPressed );
this->Template = pControlTemplate;
//Style
pStyle = gcnew System::Windows::Style();
pStyle->TargetType = (gcnew System::Windows::Controls::Button)->GetType();
//Margin
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::MarginProperty, gcnew System::Windows::Thickness(1.0));
pStyle->Setters->Add( pSetter );
//SnapsToDevicePixelsProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::SnapsToDevicePixelsProperty, true);
pStyle->Setters->Add( pSetter );
//OverridesDefaultStyleProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::OverridesDefaultStyleProperty, true);
pStyle->Setters->Add( pSetter );
//MinHeightProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::MinHeightProperty, 16.0 );
pStyle->Setters->Add( pSetter );
//MinWidthProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::MinWidthProperty, 16.0 );
pStyle->Setters->Add( pSetter );
//FontFamilyProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::FontFamilyProperty, gcnew System::Windows::Media::FontFamily("Verdana") );
pStyle->Setters->Add( pSetter );
//FontSizeProperty
System::Windows::FontSizeConverter^ pFontSizeConverter = gcnew System::Windows::FontSizeConverter();
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::FontSizeProperty, (double)pFontSizeConverter->ConvertFromString("11px"));
pStyle->Setters->Add( pSetter );
//ForegroundProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::ForegroundProperty, System::Windows::Media::Brushes::White);
pStyle->Setters->Add( pSetter );
//TemplateProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Control::TemplateProperty, pControlTemplate );
pStyle->Setters->Add( pSetter );
this->Style = pStyle;
}
How to use this contorl in your application
MavisButton^ m_pOkButton = gcnew MavisButton();
m_pOkButton->Width = 80;
m_pOkButton->Height = 25;
m_pOkButton->Content = "OK";
m_pOkButton->Click += gcnew System::Windows::RoutedEventHandler( this, &AboutScreen::OKButton_Click );
Canvas::SetTop( m_pOkButton, 360 );
Canvas::SetLeft( m_pOkButton, 260 );
m_MainCanvas->Children->Add ( m_pOkButton );
Friday, December 11, 2009
Custom ToolTip in WPF
Hi,
I create a static class for set a tooltip style in VC++, using WPF and C++/CLI. Using this class we can set the custom tooltip style for our WPF application.
Eg:
System::Windows::Controls::ToolTip^ pToolTip = gcnew System::Windows::Controls::ToolTip();
pToolTip->Style = mvdata::CToolTipStyle::ToopTipStyle( "Tool tip Text" );
System::Windows::Controls::YourControl->ToolTip = pToolTip;
//pSubMenuItem->ToolTip = pToolTip;
#pragma once
#include "stdafx.h"
//Class for SetToolTip Style
namespace myTooltipStyle
{
public ref class CToolTipStyle
{
private:
static CToolTipStyle()
{
}
public:
static System::Windows::Style^ ToopTipStyle(System::String^ pToolTipText)
{
System::Windows::Style^ pToopTipStyle;
System::Windows::Setter^ pSetter;
System::Windows::Controls::ControlTemplate^ pControlTemplate;
System::Windows::Media::LinearGradientBrush^ pBorderStroke;
System::Windows::Media::LinearGradientBrush^ pBackground;
//-----------------------------------------------------Background-------------------------------------------------------
pBackground = gcnew System::Windows::Media::LinearGradientBrush();
pBackground->StartPoint = System::Windows::Point(0.5, 0);
pBackground->EndPoint = System::Windows::Point(0.5, 1);
pBackground->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#CF181818" )), 0 ));
pBackground->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#BE1C1C1C" )), 1 ));
//-----------------------------------------------------BorderStroke-------------------------------------------------------
pBorderStroke = gcnew System::Windows::Media::LinearGradientBrush();
pBorderStroke->StartPoint = System::Windows::Point(0.5, 0);
pBorderStroke->EndPoint = System::Windows::Point(0.5, 1);
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#80FFFFFF" )), 0 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#7FFFFFFF" )), 1 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFF18D" )), 0.344 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFF4AB" )), 0.647 ));
//--------------------------------------------------------------------------------------------------
pControlTemplate = gcnew System::Windows::Controls::ControlTemplate();
pControlTemplate->TargetType = (gcnew System::Windows::Controls::ToolTip)->GetType();
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBorder = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Border)->GetType() );
pFrameworkElementFactoryBorder->Name = "Outline";
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::CornerRadiusProperty, gcnew System::Windows::CornerRadius(7.0));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::HorizontalAlignmentProperty, System::Windows::HorizontalAlignment::Center);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::VerticalAlignmentProperty, System::Windows::VerticalAlignment::Top);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::PaddingProperty, System::Windows::Thickness(5));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderThicknessProperty, gcnew System::Windows::Thickness(3));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BackgroundProperty, pBackground );
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderBrushProperty, pBorderStroke ) ;
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryStackPanal = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::StackPanel)->GetType() );
pFrameworkElementFactoryStackPanal->Name = "StackPanal";
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryTextBlock = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::TextBlock)->GetType() );
pFrameworkElementFactoryStackPanal->Name = "TextBlock";
pFrameworkElementFactoryTextBlock->SetValue( System::Windows::Controls::TextBlock::ForegroundProperty, System::Windows::Media::Brushes::White);
pFrameworkElementFactoryTextBlock->SetValue( System::Windows::Controls::TextBlock::TextProperty, pToolTipText );
pFrameworkElementFactoryStackPanal->AppendChild( pFrameworkElementFactoryTextBlock );
pFrameworkElementFactoryBorder->AppendChild( pFrameworkElementFactoryStackPanal );
pControlTemplate->VisualTree = pFrameworkElementFactoryBorder;
pToopTipStyle = gcnew System::Windows::Style();
pToopTipStyle->TargetType = (gcnew System::Windows::Controls::ToolTip)->GetType();
//OverridesDefaultStyleProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::ToolTip::OverridesDefaultStyleProperty, true);
pToopTipStyle->Setters->Add( pSetter );
//HasDropShadowProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::ToolTip::HasDropShadowProperty, true);
pToopTipStyle->Setters->Add( pSetter );
//Template
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Control::TemplateProperty, pControlTemplate );
pToopTipStyle->Setters->Add( pSetter );
return pToopTipStyle;
}
};
}
I create a static class for set a tooltip style in VC++, using WPF and C++/CLI. Using this class we can set the custom tooltip style for our WPF application.
Eg:
System::Windows::Controls::ToolTip^ pToolTip = gcnew System::Windows::Controls::ToolTip();
pToolTip->Style = mvdata::CToolTipStyle::ToopTipStyle( "Tool tip Text" );
System::Windows::Controls::YourControl->ToolTip = pToolTip;
//pSubMenuItem->ToolTip = pToolTip;
#pragma once
#include "stdafx.h"
//Class for SetToolTip Style
namespace myTooltipStyle
{
public ref class CToolTipStyle
{
private:
static CToolTipStyle()
{
}
public:
static System::Windows::Style^ ToopTipStyle(System::String^ pToolTipText)
{
System::Windows::Style^ pToopTipStyle;
System::Windows::Setter^ pSetter;
System::Windows::Controls::ControlTemplate^ pControlTemplate;
System::Windows::Media::LinearGradientBrush^ pBorderStroke;
System::Windows::Media::LinearGradientBrush^ pBackground;
//-----------------------------------------------------Background-------------------------------------------------------
pBackground = gcnew System::Windows::Media::LinearGradientBrush();
pBackground->StartPoint = System::Windows::Point(0.5, 0);
pBackground->EndPoint = System::Windows::Point(0.5, 1);
pBackground->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#CF181818" )), 0 ));
pBackground->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#BE1C1C1C" )), 1 ));
//-----------------------------------------------------BorderStroke-------------------------------------------------------
pBorderStroke = gcnew System::Windows::Media::LinearGradientBrush();
pBorderStroke->StartPoint = System::Windows::Point(0.5, 0);
pBorderStroke->EndPoint = System::Windows::Point(0.5, 1);
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#80FFFFFF" )), 0 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#7FFFFFFF" )), 1 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFF18D" )), 0.344 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFF4AB" )), 0.647 ));
//--------------------------------------------------------------------------------------------------
pControlTemplate = gcnew System::Windows::Controls::ControlTemplate();
pControlTemplate->TargetType = (gcnew System::Windows::Controls::ToolTip)->GetType();
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBorder = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Border)->GetType() );
pFrameworkElementFactoryBorder->Name = "Outline";
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::CornerRadiusProperty, gcnew System::Windows::CornerRadius(7.0));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::HorizontalAlignmentProperty, System::Windows::HorizontalAlignment::Center);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::VerticalAlignmentProperty, System::Windows::VerticalAlignment::Top);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::PaddingProperty, System::Windows::Thickness(5));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderThicknessProperty, gcnew System::Windows::Thickness(3));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BackgroundProperty, pBackground );
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderBrushProperty, pBorderStroke ) ;
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryStackPanal = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::StackPanel)->GetType() );
pFrameworkElementFactoryStackPanal->Name = "StackPanal";
System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryTextBlock = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::TextBlock)->GetType() );
pFrameworkElementFactoryStackPanal->Name = "TextBlock";
pFrameworkElementFactoryTextBlock->SetValue( System::Windows::Controls::TextBlock::ForegroundProperty, System::Windows::Media::Brushes::White);
pFrameworkElementFactoryTextBlock->SetValue( System::Windows::Controls::TextBlock::TextProperty, pToolTipText );
pFrameworkElementFactoryStackPanal->AppendChild( pFrameworkElementFactoryTextBlock );
pFrameworkElementFactoryBorder->AppendChild( pFrameworkElementFactoryStackPanal );
pControlTemplate->VisualTree = pFrameworkElementFactoryBorder;
pToopTipStyle = gcnew System::Windows::Style();
pToopTipStyle->TargetType = (gcnew System::Windows::Controls::ToolTip)->GetType();
//OverridesDefaultStyleProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::ToolTip::OverridesDefaultStyleProperty, true);
pToopTipStyle->Setters->Add( pSetter );
//HasDropShadowProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::ToolTip::HasDropShadowProperty, true);
pToopTipStyle->Setters->Add( pSetter );
//Template
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Control::TemplateProperty, pControlTemplate );
pToopTipStyle->Setters->Add( pSetter );
return pToopTipStyle;
}
};
}
Thursday, June 26, 2008
Multicolumn treeview in C++/CLI using WPF
This is a sample code. You can modify it and try to create the controls as dynamically. It display a tree like this.
=====================
- Nod1.1 Node1.2
Node1..1 Node 1.2.2
- Node2.1 Node 2.2
Node2.1.1 Node 2.2.2
=====================
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
ref class FirstWindow : Window
{
Canvas^ maincanvas;
TextBox^ textbox_a1;
TextBox^ textbox_a2;
TextBox^ textbox_b1;
TextBox^ textbox_b2;
TextBox^ textbox_c1;
TextBox^ textbox_c2;
TextBox^ textbox_d1;
TextBox^ textbox_d2;
TreeView ^g_TreeView;
TreeViewItem^ g_TreeViewItem;
TreeViewItem^ g_TreeViewItem1;
TreeViewItem^ g_TreeViewItem2;
Grid^ g_Grid_a1;
ColumnDefinition^ g_ColumnDefinition_a1; ColumnDefinition^ g_ColumnDefinition_a2;
RowDefinition^ g_RowDefinitions_a1; RowDefinition^ g_RowDefinitions_a2;
Grid^ g_Grid_b1;
ColumnDefinition^ g_ColumnDefinition_b1; ColumnDefinition^ g_ColumnDefinition_b2;
RowDefinition^ g_RowDefinitions_b1; RowDefinition^ g_RowDefinitions_b2;
Grid^ g_Grid_c1;
ColumnDefinition^ g_ColumnDefinition_c1; ColumnDefinition^ g_ColumnDefinition_c2;
RowDefinition^ g_RowDefinitions_c1; RowDefinition^ g_RowDefinitions_c2;
Grid^ g_Grid_d1;
ColumnDefinition^ g_ColumnDefinition_d1; ColumnDefinition^ g_ColumnDefinition_d2;
RowDefinition^ g_RowDefinitions_d1; RowDefinition^ g_RowDefinitions_d2;
public:
FirstWindow(void)
{
Title = "Multi column TreeView ";
Width = 400;
Height = 400;
ResizeMode = System::Windows::ResizeMode::NoResize;
InitControls();
}
void InitControls(void)
{
textbox_a1 = gcnew TextBox();
textbox_a1->Width = 50;
textbox_a1->Height = 20;
textbox_a1->Text ="Node 1";
textbox_a2 = gcnew TextBox();
textbox_a2->Width = 50;
textbox_a2->Height = 20;
textbox_a2->Text ="god";
textbox_b1 = gcnew TextBox();
textbox_b1->Width = 50;
textbox_b1->Height = 20;
textbox_b1->Text ="Node 1";
textbox_b2 = gcnew TextBox();
textbox_b2->Width = 50;
textbox_b2->Height = 20;
textbox_b2->Text ="god";
textbox_c1 = gcnew TextBox();
textbox_c1->Width = 50;
textbox_c1->Height = 20;
textbox_c1->Text ="Node 1";
textbox_c2 = gcnew TextBox();
textbox_c2->Width = 50;
textbox_c2->Height = 20;
textbox_c2->Text ="god";
textbox_d1 = gcnew TextBox();
textbox_d1->Width = 50;
textbox_d1->Height = 20;
textbox_d1->Text ="Node 1";
textbox_d2 = gcnew TextBox();
textbox_d2->Width = 50;
textbox_d2->Height = 20;
textbox_d2->Text ="god";
g_ColumnDefinition_a1 = gcnew ColumnDefinition();
g_RowDefinitions_a1 = gcnew RowDefinition();
g_ColumnDefinition_a2 = gcnew ColumnDefinition();
g_RowDefinitions_a2 = gcnew RowDefinition();
g_ColumnDefinition_b1 = gcnew ColumnDefinition();
g_RowDefinitions_b1 = gcnew RowDefinition();
g_ColumnDefinition_b2 = gcnew ColumnDefinition();
g_RowDefinitions_b2 = gcnew RowDefinition();
g_ColumnDefinition_c1 = gcnew ColumnDefinition();
g_ColumnDefinition_c2 = gcnew ColumnDefinition();
g_RowDefinitions_c1 = gcnew RowDefinition();
g_RowDefinitions_c2 = gcnew RowDefinition();
g_ColumnDefinition_d1 = gcnew ColumnDefinition();
g_ColumnDefinition_d2 = gcnew ColumnDefinition();
g_RowDefinitions_d1 = gcnew RowDefinition();
g_RowDefinitions_d2 = gcnew RowDefinition();
g_Grid_a1 = gcnew Grid();
g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a1);
g_Grid_a1->ColumnDefinitions->Add(g_ColumnDefinition_a1);
g_Grid_a1->ColumnDefinitions->Add(g_ColumnDefinition_a2);
g_Grid_a1->ShowGridLines=true;
g_Grid_a1->Height=20;
g_Grid_a1->Width =100;
g_Grid_a1->SetColumn(textbox_a1,0);
g_Grid_a1->SetColumn(textbox_a2,2);
g_Grid_a1->Children->Add(textbox_a1);
g_Grid_a1->Children->Add(textbox_a2);
g_Grid_b1 = gcnew Grid();
g_Grid_b1->RowDefinitions->Add(g_RowDefinitions_b1);
g_Grid_b1->ColumnDefinitions->Add(g_ColumnDefinition_b1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_b1->ColumnDefinitions->Add(g_ColumnDefinition_b2);
g_Grid_b1->ShowGridLines=true;
g_Grid_b1->Height=20;
g_Grid_b1->Width =100;
g_Grid_b1->SetColumn(textbox_b1,0);
g_Grid_b1->SetColumn(textbox_b2,2);
g_Grid_b1->Children->Add(textbox_b1);
g_Grid_b1->Children->Add(textbox_b2);
g_Grid_c1 = gcnew Grid();
g_Grid_c1->RowDefinitions->Add(g_RowDefinitions_c1);
g_Grid_c1->ColumnDefinitions->Add(g_ColumnDefinition_c1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_c1->ColumnDefinitions->Add(g_ColumnDefinition_c2);
g_Grid_c1->ShowGridLines=true;
g_Grid_c1->Height=20;
g_Grid_c1->Width =100;
g_Grid_c1->SetColumn(textbox_c1,0);
g_Grid_c1->SetColumn(textbox_c2,2);
g_Grid_c1->Children->Add(textbox_c1);
g_Grid_c1->Children->Add(textbox_c2);
g_Grid_d1 = gcnew Grid();
g_Grid_d1->RowDefinitions->Add(g_RowDefinitions_d1);
g_Grid_d1->ColumnDefinitions->Add(g_ColumnDefinition_d1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_d1->ColumnDefinitions->Add(g_ColumnDefinition_d2);
g_Grid_d1->ShowGridLines=true;
g_Grid_d1->Height=20;
g_Grid_d1->Width =100;
g_Grid_d1->SetColumn(textbox_d1,0);
g_Grid_d1->SetColumn(textbox_d2,2);
g_Grid_d1->Children->Add(textbox_d1);
g_Grid_d1->Children->Add(textbox_d2);
g_TreeView = gcnew TreeView();
g_TreeView->Width= 300;
g_TreeView->Height = 200;
g_TreeViewItem1 = gcnew TreeViewItem();
g_TreeViewItem1->Header = g_Grid_b1;
//g_TreeViewItem1->Items->Add("Sub Sub Node God");
g_TreeViewItem1->Items->Add(g_Grid_c1);
g_TreeViewItem = gcnew TreeViewItem();
//g_TreeViewItem->Header = "root";
g_TreeViewItem->Header = g_Grid_d1;
g_TreeViewItem->Items->Add(g_Grid_a1);
g_TreeViewItem->Items->Add(g_TreeViewItem1);
g_TreeView->Items->Add(g_TreeViewItem);
Canvas::SetTop(g_TreeView,10);
Canvas::SetLeft(g_TreeView,10);
maincanvas = gcnew Canvas();
maincanvas->Children->Add(g_TreeView);
Content = maincanvas;
}
};
=====================
- Nod1.1 Node1.2
Node1..1 Node 1.2.2
- Node2.1 Node 2.2
Node2.1.1 Node 2.2.2
=====================
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;
ref class FirstWindow : Window
{
Canvas^ maincanvas;
TextBox^ textbox_a1;
TextBox^ textbox_a2;
TextBox^ textbox_b1;
TextBox^ textbox_b2;
TextBox^ textbox_c1;
TextBox^ textbox_c2;
TextBox^ textbox_d1;
TextBox^ textbox_d2;
TreeView ^g_TreeView;
TreeViewItem^ g_TreeViewItem;
TreeViewItem^ g_TreeViewItem1;
TreeViewItem^ g_TreeViewItem2;
Grid^ g_Grid_a1;
ColumnDefinition^ g_ColumnDefinition_a1; ColumnDefinition^ g_ColumnDefinition_a2;
RowDefinition^ g_RowDefinitions_a1; RowDefinition^ g_RowDefinitions_a2;
Grid^ g_Grid_b1;
ColumnDefinition^ g_ColumnDefinition_b1; ColumnDefinition^ g_ColumnDefinition_b2;
RowDefinition^ g_RowDefinitions_b1; RowDefinition^ g_RowDefinitions_b2;
Grid^ g_Grid_c1;
ColumnDefinition^ g_ColumnDefinition_c1; ColumnDefinition^ g_ColumnDefinition_c2;
RowDefinition^ g_RowDefinitions_c1; RowDefinition^ g_RowDefinitions_c2;
Grid^ g_Grid_d1;
ColumnDefinition^ g_ColumnDefinition_d1; ColumnDefinition^ g_ColumnDefinition_d2;
RowDefinition^ g_RowDefinitions_d1; RowDefinition^ g_RowDefinitions_d2;
public:
FirstWindow(void)
{
Title = "Multi column TreeView ";
Width = 400;
Height = 400;
ResizeMode = System::Windows::ResizeMode::NoResize;
InitControls();
}
void InitControls(void)
{
textbox_a1 = gcnew TextBox();
textbox_a1->Width = 50;
textbox_a1->Height = 20;
textbox_a1->Text ="Node 1";
textbox_a2 = gcnew TextBox();
textbox_a2->Width = 50;
textbox_a2->Height = 20;
textbox_a2->Text ="god";
textbox_b1 = gcnew TextBox();
textbox_b1->Width = 50;
textbox_b1->Height = 20;
textbox_b1->Text ="Node 1";
textbox_b2 = gcnew TextBox();
textbox_b2->Width = 50;
textbox_b2->Height = 20;
textbox_b2->Text ="god";
textbox_c1 = gcnew TextBox();
textbox_c1->Width = 50;
textbox_c1->Height = 20;
textbox_c1->Text ="Node 1";
textbox_c2 = gcnew TextBox();
textbox_c2->Width = 50;
textbox_c2->Height = 20;
textbox_c2->Text ="god";
textbox_d1 = gcnew TextBox();
textbox_d1->Width = 50;
textbox_d1->Height = 20;
textbox_d1->Text ="Node 1";
textbox_d2 = gcnew TextBox();
textbox_d2->Width = 50;
textbox_d2->Height = 20;
textbox_d2->Text ="god";
g_ColumnDefinition_a1 = gcnew ColumnDefinition();
g_RowDefinitions_a1 = gcnew RowDefinition();
g_ColumnDefinition_a2 = gcnew ColumnDefinition();
g_RowDefinitions_a2 = gcnew RowDefinition();
g_ColumnDefinition_b1 = gcnew ColumnDefinition();
g_RowDefinitions_b1 = gcnew RowDefinition();
g_ColumnDefinition_b2 = gcnew ColumnDefinition();
g_RowDefinitions_b2 = gcnew RowDefinition();
g_ColumnDefinition_c1 = gcnew ColumnDefinition();
g_ColumnDefinition_c2 = gcnew ColumnDefinition();
g_RowDefinitions_c1 = gcnew RowDefinition();
g_RowDefinitions_c2 = gcnew RowDefinition();
g_ColumnDefinition_d1 = gcnew ColumnDefinition();
g_ColumnDefinition_d2 = gcnew ColumnDefinition();
g_RowDefinitions_d1 = gcnew RowDefinition();
g_RowDefinitions_d2 = gcnew RowDefinition();
g_Grid_a1 = gcnew Grid();
g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a1);
g_Grid_a1->ColumnDefinitions->Add(g_ColumnDefinition_a1);
g_Grid_a1->ColumnDefinitions->Add(g_ColumnDefinition_a2);
g_Grid_a1->ShowGridLines=true;
g_Grid_a1->Height=20;
g_Grid_a1->Width =100;
g_Grid_a1->SetColumn(textbox_a1,0);
g_Grid_a1->SetColumn(textbox_a2,2);
g_Grid_a1->Children->Add(textbox_a1);
g_Grid_a1->Children->Add(textbox_a2);
g_Grid_b1 = gcnew Grid();
g_Grid_b1->RowDefinitions->Add(g_RowDefinitions_b1);
g_Grid_b1->ColumnDefinitions->Add(g_ColumnDefinition_b1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_b1->ColumnDefinitions->Add(g_ColumnDefinition_b2);
g_Grid_b1->ShowGridLines=true;
g_Grid_b1->Height=20;
g_Grid_b1->Width =100;
g_Grid_b1->SetColumn(textbox_b1,0);
g_Grid_b1->SetColumn(textbox_b2,2);
g_Grid_b1->Children->Add(textbox_b1);
g_Grid_b1->Children->Add(textbox_b2);
g_Grid_c1 = gcnew Grid();
g_Grid_c1->RowDefinitions->Add(g_RowDefinitions_c1);
g_Grid_c1->ColumnDefinitions->Add(g_ColumnDefinition_c1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_c1->ColumnDefinitions->Add(g_ColumnDefinition_c2);
g_Grid_c1->ShowGridLines=true;
g_Grid_c1->Height=20;
g_Grid_c1->Width =100;
g_Grid_c1->SetColumn(textbox_c1,0);
g_Grid_c1->SetColumn(textbox_c2,2);
g_Grid_c1->Children->Add(textbox_c1);
g_Grid_c1->Children->Add(textbox_c2);
g_Grid_d1 = gcnew Grid();
g_Grid_d1->RowDefinitions->Add(g_RowDefinitions_d1);
g_Grid_d1->ColumnDefinitions->Add(g_ColumnDefinition_d1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_d1->ColumnDefinitions->Add(g_ColumnDefinition_d2);
g_Grid_d1->ShowGridLines=true;
g_Grid_d1->Height=20;
g_Grid_d1->Width =100;
g_Grid_d1->SetColumn(textbox_d1,0);
g_Grid_d1->SetColumn(textbox_d2,2);
g_Grid_d1->Children->Add(textbox_d1);
g_Grid_d1->Children->Add(textbox_d2);
g_TreeView = gcnew TreeView();
g_TreeView->Width= 300;
g_TreeView->Height = 200;
g_TreeViewItem1 = gcnew TreeViewItem();
g_TreeViewItem1->Header = g_Grid_b1;
//g_TreeViewItem1->Items->Add("Sub Sub Node God");
g_TreeViewItem1->Items->Add(g_Grid_c1);
g_TreeViewItem = gcnew TreeViewItem();
//g_TreeViewItem->Header = "root";
g_TreeViewItem->Header = g_Grid_d1;
g_TreeViewItem->Items->Add(g_Grid_a1);
g_TreeViewItem->Items->Add(g_TreeViewItem1);
g_TreeView->Items->Add(g_TreeViewItem);
Canvas::SetTop(g_TreeView,10);
Canvas::SetLeft(g_TreeView,10);
maincanvas = gcnew Canvas();
maincanvas->Children->Add(g_TreeView);
Content = maincanvas;
}
};
Friday, June 22, 2007
Count occurence of character (i.e. line breaks) in a MySQL field
Unfortunately, there is no inbuilt MySQL function to count the occurance of a character in string. By counting the number of characters in the original string, then temporarily 'deleting' the character you want to count and counting the string again, subtracting the first count from the second, you get the number of occurances:
SELECT LENGTH('foobarfoobarfoobar') - LENGTH(REPLACE('foobarfoobarfoobar', 'b', '')) AS `occurances`
--> 3
Obviously, in this example 'b' is the string you want to count the number of occurances of.
You can use the same principle to count the number of lines in a field, by adding one to the difference:
Thursday, June 21, 2007
Connecting C / C++ with MYSQL
This code help you to create a connection with C/C++ and MYSQL
Create a header " .h" file and write these steps in that file
#include "/usr/include/mysql/mysql.h"
#include
using namespace std;
MYSQL mysql;
MYSQL_RES *Result;
MYSQL_ROW FirstTblRow, SecTblRow, ThrdTblRow, TblField ;
int C_Connect_MYSQL( char *hostname, char *username, char *passwd, char *dbname )
{
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, hostname, username, passwd, dbname, 0, NULL, 0))
{
printf("%d: %s \n", mysql_errno(&mysql), mysql_error(&mysql));
printf("Connection Refused by MYSQL\n");
return 1;
}
else{
printf("Connection OK by MYSQL\n");
return 0;
}
}
MYSQL_RES * Exectue_Query ( char *Exec_Qry )
{
if(mysql_query(&mysql, Exec_Qry)) {
cout << Exec_Qry << endl;
cout << "Error in Query!"< }
else {
Result = mysql_store_result(&mysql);
}
return Result;
}
MYSQL_RES * MySqlListField ( char *TableName )
{
Result = mysql_list_fields ( &mysql, TableName, NULL );
return Result;
}
After this create the ".c" file and write the following steps in main() function
char *Qry[] = "select * from YOUR_TABLE_NAME;";
int ret = C_Connect_MYSQL( MY_HOSTNAME, MY_USERNAME, MY_PASSWORD, MY_DATABASE);
if ( ret == 1 ) exit(0);
FirstTableName = Exectue_Query( Qry );
if ( mysql_num_rows(FirstTableName) > 0 ) {
while( (FirstTblRow = mysql_fetch_row( FirstTableName )))
{
cout << TblField[1] ? TblField[1] : "NULL" << endl;
}
}
Create a header " .h" file and write these steps in that file
#include "/usr/include/mysql/mysql.h"
#include
using namespace std;
MYSQL mysql;
MYSQL_RES *Result;
MYSQL_ROW FirstTblRow, SecTblRow, ThrdTblRow, TblField ;
int C_Connect_MYSQL( char *hostname, char *username, char *passwd, char *dbname )
{
mysql_init(&mysql);
if(!mysql_real_connect(&mysql, hostname, username, passwd, dbname, 0, NULL, 0))
{
printf("%d: %s \n", mysql_errno(&mysql), mysql_error(&mysql));
printf("Connection Refused by MYSQL\n");
return 1;
}
else{
printf("Connection OK by MYSQL\n");
return 0;
}
}
MYSQL_RES * Exectue_Query ( char *Exec_Qry )
{
if(mysql_query(&mysql, Exec_Qry)) {
cout << Exec_Qry << endl;
cout << "Error in Query!"<
else {
Result = mysql_store_result(&mysql);
}
return Result;
}
MYSQL_RES * MySqlListField ( char *TableName )
{
Result = mysql_list_fields ( &mysql, TableName, NULL );
return Result;
}
After this create the ".c" file and write the following steps in main() function
char *Qry[] = "select * from YOUR_TABLE_NAME;";
int ret = C_Connect_MYSQL( MY_HOSTNAME, MY_USERNAME, MY_PASSWORD, MY_DATABASE);
if ( ret == 1 ) exit(0);
FirstTableName = Exectue_Query( Qry );
if ( mysql_num_rows(FirstTableName) > 0 ) {
while( (FirstTblRow = mysql_fetch_row( FirstTableName )))
{
cout << TblField[1] ? TblField[1] : "NULL" << endl;
}
}
Subscribe to:
Posts (Atom)