我正在将我的 C# Windows 应用程序转换为 C++\CLI 应用程序。
我坚持“转换”这段代码:
void LoadWindow(Mode mode) {
if (timer != null && timer.Enabled) return;
int currentIndex = Windows.IndexOf(panel.Controls[0]);
int loadInex = 0, exitLeft = 0, entranceLeft = 0, stopLeft = 0;
if (mode == Mode.Next)
{
loadInex = currentIndex == Windows.Count - 1 ? 0 : ++currentIndex;
exitLeft = -((panel.Width / 2 - Windows[currentIndex].Width / 2) + Windows[currentIndex].Width);
entranceLeft = panel.Width;
}
else
{
loadInex = currentIndex == 0 ? Windows.Count - 1 : --currentIndex;
exitLeft = panel.Width;
entranceLeft = -panel.Width;
}
stopLeft = panel.Width / 2 - Windows[loadInex].Width / 2;
Windows[loadInex].Left = entranceLeft;
panel.Controls.Add(Windows[loadInex]);
timer = new System.Windows.Forms.Timer();
timer.Interval = 10;
timer.Tick += new EventHandler(delegate(object sender, EventArgs e) {
if (mode == Mode.Next)
{
if (exitLeft <= panel.Controls[0].Left)
panel.Controls[0].Left -= 50;
if (stopLeft <= panel.Controls[1].Left)
panel.Controls[1].Left = panel.Controls[1].Left - 50 < stopLeft ? stopLeft : panel.Controls[1].Left - 50;
if (exitLeft >= panel.Controls[0].Left && stopLeft >= panel.Controls[1].Left)
{
panel.Controls.RemoveAt(0);
timer.Enabled = false;
}
}
if (mode == Mode.Previous)
{
if (exitLeft >= panel.Controls[0].Left)
panel.Controls[0].Left += 50;
if (stopLeft >= panel.Controls[1].Left)
panel.Controls[1].Left = panel.Controls[1].Left + 50 > stopLeft ? stopLeft : panel.Controls[1].Left + 50;
if (exitLeft <= panel.Controls[0].Left && stopLeft <= panel.Controls[1].Left)
{
panel.Controls.RemoveAt(0);
timer.Enabled = false;
}
}
});
timer.Enabled = true;
}
AFAIK C++\CLI 不支持匿名方法(我什至不能利用 lambdas)。
如果 EventHandler 委托没有在 LoadWindow 函数中使用局部变量,这不会成为问题。
我该如何实施?我考虑过使用一个简单的仿函数类,但有没有更“优雅”的方式?
谢谢,亚历克斯