Sunday, September 19, 2010

Call a new function from button click with arguments in C++/CLI

//FirstWindow.h
#pragma once

using namespace System; using namespace System::Windows; using namespace System::Windows::Controls;

ref class Functor;

ref class FirstWindow : Window { Canvas^ maincanvas; Button^ addbutton1; Button^ addbutton2; Functor^ pFunctor; public: FirstWindow(void); void InitControls(void); void MyFunction( int x, int y );

};

//FirstWindow.cpp #include "FirstWindow.h" #include "Functor.h"

FirstWindow::FirstWindow(void) { Title = "First Avalon App"; Width = 400; Height = 400; ResizeMode = System::Windows::ResizeMode::NoResize;

InitControls();

}

void FirstWindow::InitControls(void) { addbutton1 = gcnew Button(); addbutton1->Width = 80; addbutton1->Height = 25; addbutton1->Content = "Add"; pFunctor = gcnew Functor(this, 10, 20); addbutton1->Click += gcnew RoutedEventHandler( pFunctor, &Functor::Handler);

    Canvas::SetTop(addbutton1, 45);
Canvas::SetLeft(addbutton1, 200);


pFunctor
= gcnew Functor(this, 100, 200);
addbutton2
= gcnew Button();
addbutton2
->Width = 80;
addbutton2
->Height = 25;
addbutton2
->Content = "Add";
addbutton2
->Click += gcnew RoutedEventHandler(pFunctor, &Functor::Handler);

Canvas::SetTop(addbutton2, 85);
Canvas::SetLeft(addbutton2, 200);


maincanvas
= gcnew Canvas();

maincanvas
->Children->Add(addbutton1);
maincanvas
->Children->Add(addbutton2);
Content = maincanvas;

}

void FirstWindow::MyFunction( int x, int y ) { MessageBox::Show("This function is call by Button Click with values " + x.ToString() + " , " + y.ToString() ); }

//Functor.h #pragma once

using namespace System; using namespace System::Windows; using namespace System::Windows::Controls;

ref class FirstWindow;

private ref class Functor { public: Functor(FirstWindow^ pFirstWindow, int pFirstArg, int pSecArg);

// This is what we will use as the handler method
void Handler(Object ^ sender, RoutedEventArgs ^ e);

private: int m_pFirstArg; int m_pSecArg; FirstWindow^ m_pFirstWindow; };

//Functor.cpp

#include "Functor.h" #include "FirstWindow.h"

Functor::Functor(FirstWindow^ pFirstWindow, int pFirstArg, int pSecArg) : m_pFirstWindow( pFirstWindow ), m_pFirstArg(pFirstArg), m_pSecArg( pSecArg ) {

}

void Functor::Handler(Object ^ sender, RoutedEventArgs ^ e) { if ( m_pFirstWindow ) m_pFirstWindow->MyFunction(m_pFirstArg, m_pSecArg );

}

Now when we click on button one, then the application call the function "MyFunction" with value 10,20 and when we click on button 2 then the same function "MyFunction" with value 100,200.

Saturday, September 11, 2010

Load Controls from WPF window using C++/CLI

void FindChildren(System::Windows::DependencyObject^ source, List^ pList)
{
System::Collections::Generic::List^ pDependencyObject = nullptr;
pDependencyObject = gcnew System::Collections::Generic::List();

if (source != nullptr)
{
GetChildObjects(source, pDependencyObject);

for each ( System::Windows::DependencyObject^ child in pDependencyObject)
{
if ( dynamic_cast(child) )
{
//MessageBox::Show ( "Yes Add" + child->GetType()->ToString());
pList->Add( (System::Windows::Controls::Control^)child );
}
//else
//MessageBox::Show ( "Not need" + child->GetType()->ToString());

}
}

}

void GetChildObjects(System::Windows::DependencyObject^ parent, System::Collections::Generic::List^ pDependencyObject)
{
bool bContentElement = false;
bool bFrameworkElement = false;

if (parent == nullptr)
return;

if ( dynamic_cast(parent) )
bContentElement = true;
else
bContentElement = false;


if ( dynamic_cast(parent) )
bFrameworkElement = true;
else
bFrameworkElement = false;

if ( ( bContentElement == true ) || ( bFrameworkElement == true ) )
{
for each (System::Object^ obj in System::Windows::LogicalTreeHelper::GetChildren(parent))
{
System::Windows::DependencyObject^ depObj = nullptr;
if ( dynamic_cast(obj) )
depObj = (System::Windows::DependencyObject^)obj;

if (depObj != nullptr)
{
pDependencyObject->Add( (System::Windows::DependencyObject^)obj );
//MessageBox::Show( ((System::Windows::DependencyObject^)obj)->GetType()->ToString() );
GetChildObjects( (System::Windows::DependencyObject^)obj, pDependencyObject );
}
}
}
}


call the function

List^ pControlList = nullptr;
pControlList = gcnew List();

FindChildren(pDialog, pControlList); //Load controls from dialog

for each ( System::Windows::Controls::Control^ pControl in pControlList )
{
//your checking
}
My Blog - Windows Live: "- Sent using Google Toolbar"