謝謝你的幫忙。 在你的幫助下,我可以解決我的問題。 該解決方案是這樣的,
//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;
};
//函子.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);
}
現在,當我們點擊按鈕1時,應用程序調用值爲10,20的函數「MyFunction」,當我們點擊按鈕2時,同樣的函數「MyFunction」的值爲100,200。
謝謝你的幫助。
Sabeesh
謝謝您的回答。 – sabeesh 2010-09-19 05:40:23
你不能*繼續*。 C++ 0x添加lambdas,爲C++首次提供了匿名方法的語法。就個人而言,我很高興微軟等待標準委員會就語法達成一致意見,而不是添加一些不可移植的擴展,並以受管vs本地代碼中的lambda表達式的不同語法結束。 – 2010-09-20 17:08:58
我需要從click事件中找到添加的委託並需要刪除。我怎樣才能做到這一點? – sabeesh 2010-09-22 12:01:27