1. 程式人生 > 其它 >視訊播放器開發日誌-paint event 不響應的解決辦法

視訊播放器開發日誌-paint event 不響應的解決辦法

技術標籤:軟體開發c#paintgdi/gdi+視訊處理

場景:
點選按鈕,隱藏介面上的控制元件,然後在paint 裡面輸出提示資訊。
如題
在這裡插入圖片描述

程式碼如下:

   private void cbxCoverSub_CheckedChanged(object sender, EventArgs e)
        {
            bool bShow = !cbxCoverSub.Checked;
            showSubtitle(bShow);
            showNotice();
        }

兩個函式的程式碼如下:

        private
void showSubtitle(bool bShow) { txtBackSubtitle.Visible = bShow; txtTopSubtitle.Visible = bShow; }

這段程式碼用於更新提示資訊

        private void showNotice()
        {
            string str = "You can move and adjust this bar with mouse to cover the hard subtitle."
; Graphics g = this.CreateGraphics();// Pen p = new Pen(Color.Blue, 2);//定義了一個藍色,寬度為的畫筆 SolidBrush brsh = new SolidBrush(m_mouseMvBkClr); // clear the background g.FillRectangle(brsh, this.ClientRectangle); // measure the length of the string in pixel
StringFormat format = new StringFormat(); format.Alignment = StringAlignment.Center; format.LineAlignment = StringAlignment.Center; Size size = TextRenderer.MeasureText(str, this.Font); Rectangle rect = new Rectangle((this.Width - pnlRight.Width) / 2 - size.Width / 2, (this.Height - size.Height) / 2, size.Width, size.Height); TextRenderer.DrawText(g, str, this.Font, rect, Color.Yellow, m_mouseMvBkClr); //draw a border for this form g.DrawRectangle(p, this.ClientRectangle); }

paint 事件中:

    private void showSubForm_Paint(object sender, PaintEventArgs e)
        {
            //draw a border that the user can see the border of this form
            if (cbxCoverSub.Checked){
                showNotice();
            }
            //g.DrawEllipse(p, 10, 10, 100, 100);//在畫板上畫橢圓,起始座標為(10,10),外接矩形的寬為,高為

        }

一切看似乎完美,然而,期待的畫面沒有出現,必須手動重新整理才得到期待的結果:
在這裡插入圖片描述
嘗試 Refreash,無效;
後來深度懷疑是visible 屬性引發了一個貌似滯後的訊息,即這個隱藏行為發生在paint之後,因此,paint感覺好像沒有執行。弄了個timer測試了一些,發現可以得到預想介面。但是這樣顯然不是正解,忽然想到,控制元件的顯示和隱藏函式應該是阻塞方式,於是將程式碼改了一下,即

   private void showSubtitle(bool bShow)
        {
            //pay attention of the following code
            // it seems that Visible = true will trigger the system to send a  a message and it will take effect after the paint event
            // that makes the codes in paint event run before the control response to this message.
            if (bShow)
            {
                //txtBackSubtitle.Visible = bShow;
                //txtTopSubtitle.Visible = bShow;
                txtBackSubtitle.Show();
                txtTopSubtitle.Show();
            }
            else
            {
                txtBackSubtitle.Hide();
                txtTopSubtitle.Hide();

            }
            this.Refresh();
        }

馬拉孫於2021-01-24
海淀泛五道口地區