[英]How can I create a Unicode string using variables in Visual Studio C# WinForms?
[英]How can I change PictureBox size incrementally and smoothly ? ( in winforms , c# , visual studio 2013 )
我的表單中有一個Button和一個寬度為290,高度為145的PictureBox,最初它是隱藏的。 我想在按鈕的MouseEnter事件發生時一點一點地顯示PictureBox。 所以我嘗試了這段代碼:
private void button1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Size = new Size(0, 145);
pictureBox1.Show();
for (int i = 0; i < 290; i++)
pictureBox1.Size = new Size(i, 145);
}
但是它會立即顯示主要尺寸的PictureBox。
我在這個網站上找到了一個類似的問題( PictureBox並沒有改變它的大小 ),但是實際上它的答案也幫不了我。
您的代碼可以一次全部執行,因此您將看到的只是突然的更改。
使用計時器,並在計時器滴答時逐漸增加大小。
timer = new Timer(16); //~60 FPS
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
...
private void button1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Size = new Size(0, 145);
pictureBox1.Show();
timer.Enabled = true; // Enable it
}
...
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (pictureBox1.Width < 290)
pictureBox1.Width++; //Increment
else
timer.Enabled = false; //Disable
}
您必須使用pictureBox
的Update
方法才能重繪它。 另外,稍稍延遲也會在Faster計算機上更平滑地更改大小。
我這樣更改了您的代碼:
private void button1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Size = new Size(145, 145);
for (int i = 145; i < 290; i++)
{
pictureBox1.Size = new Size(i, i);
pictureBox1.Update();
System.Threading.Thread.Sleep(1);
}
}
1)定義一個新的int為public:
public partial class Form1 : Form
{
int counter = 0;
.
.
.
2)使用計時器:
private void timer1_Tick(object sender, EventArgs e)
{
counter = counter + 10;
timer1.Interval = 10;
pictureBox1.Show();
if (counter <= 290)
{ pictureBox1.Width += 1; }
}
3)在鼠標事件中啟用計時器:
private void button1_MouseEnter(object sender, EventArgs e)
{
counter = 0;
pictureBox1.Width = 0;
timer1.Enabled = true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.