Friday, December 11, 2009

Custom Command Button in WPF

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 );

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;

}
};

}