我试图在按钮点击时添加一个面板.我的代码在下面,我做到了.但现在我正试图在我的面板上放置其他按钮等,当你点击第一个按钮和面板幻灯片时,我没有任何新按钮.
//Constants const int AW_SLIDE = 0X40000; const int AW_HOR_POSITIVE = 0X1; const int AW_HOR_NEGATIVE = 0X2; const int AW_BLEND = 0X80000; [DllImport("user32")] static extern bool AnimateWindow(IntPtr hwnd,int time,int flags); photosflag=0; private void photosbutton_Click(object sender,EventArgs e) { if (photosflag == 0) { object O = Controller.Properties.Resources.ResourceManager.Getobject("photospressed"); photosbutton.Image = (System.Drawing.Image)O; photosflag = 1; int ylocation = photosbutton.Location.Y; //Set the Location photospanel.Location = new Point(101,ylocation); //Animate form AnimateWindow(photospanel.Handle,500,AW_SLIDE | AW_HOR_POSITIVE); } else { object O = Controller.Properties.Resources.ResourceManager.Getobject("photos"); photosbutton.Image = (System.Drawing.Image)O; photosflag = 0; photospanel.Visible = false; } }
解决方法
好的 – 这是一个非常简单的例子,它不依赖于AnimateWindow API:
向表单添加计时器控件.在我的,我将间隔设置为10(毫秒).您可以使用此值来根据需要平滑动画
我在表单上有按钮和面板(不可见)
我在表单上声明了以下私有成员 – 它们是面板的起始X位置,结束位置和每个增量移动的像素数 – 再次,调整以影响速度/平滑度等
private int _startLeft = -200; // start position of the panel private int _endLeft = 10; // end position of the panel private int _stepSize = 10; // pixels to move
然后在按钮上单击,我启用计时器:
animationTimer.Enabled = true;
最后,计时器tick事件中的代码使面板可见,将其移动到位,并在完成时禁用自身:
private void animationTimer_Tick(object sender,EventArgs e) { // if just starting,move to start location and make visible if (!photosPanel.Visible) { photosPanel.Left = _startLeft; photosPanel.Visible = true; } // incrementally move photosPanel.Left += _stepSize; // make sure we didn't over shoot if (photosPanel.Left > _endLeft) photosPanel.Left = _endLeft; // have we arrived? if (photosPanel.Left == _endLeft) { animationTimer.Enabled = false; } }