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;

}
};

}

No comments: