簡體   English   中英

c# - 如何在鼠標單擊事件上自動隱藏面板

[英]how to autohide the panel on mouseclick event in c#

如何在c# windows 應用程序中的mouseclick事件上滑動面板我試過這個

panel1.Location = new Point(panel1.Location.X - i, panel1.Location.Y);
System.Threading.Thread.Sleep(10);

如果我理解正確,您想將面板向左移動。 在這種情況下,您可以編寫如下內容:

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
    //declare step in pixel which will be used to move panel
    int xMoveStep = 3;
    //repeat this block of code until panel is hidden under form's left border, until 
    //panels top right is less than 0
    while (this.panel1.Right > 0)
    {
        //move it to left
        this.panel1.Left = this.panel1.Left - xMoveStep;
        //pause for 10 milliseconds
        Thread.Sleep(10); 

    }
}

您應該使用Timer並且在每個刻度上只對對象進行一些操作。

Timer m_Timer = null; // placeholder for your timer

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
    if(m_Timer != null) return; 
    // if timer is not null means you're animating your panel so do not want to perform any other animations

    m_Timer = new Timer(); //  instantiate timer
    m_Timer.Interval = 1000 / 30; // 30 frames per second;
    m_Timer.Tick += OnTimerTick; // set method handler
    m_Timer.Start(); // start the timer
}

int m_CurrentFrame = 0; // current frame 

void OnTimerTick(object sender, EventArgs e)
{
    const int LAST_FRAME_INDEX = 150; // maximum frame you can reach

    if(m_CurrenFrame > LAST_FRAME_INDEX) // if max reached
    {
        m_Timer.Stop();  // stop timer
        m_Timer.Dispose(); // dispose it for the GC
        m_Timer = null; // set it's reference to null
        m_CurrentFrame = 0; // reset current frame index
        return; // return from the event
    }

    this.Invoke(new MethodDelegate( () => { // invoke this on the UI thread
        panel1.Location = new Point(panel1.Location.X - m_CurrenFrame, panel1.Location.Y); 
    });
    m_CurrentFrame++; // increase current frame index
}

這應該有效,請嘗試此代碼

private void frmTest_MouseMove(object sender, MouseEventArgs e)
{
  if (e.Location.X >= panel1.Bounds.X && e.Location.X <= (panel1.Bounds.X + panel1.Bounds.Width) && e.Location.Y >= panel1.Bounds.Y && e.Location.Y <= (panel1.Bounds.Y + panel1.Bounds.Width))
  {
    panel1.Visible = false;
  }
  else
  {
    panel1.Visible = true;
  }
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
  panel1.Visible = false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM