如何彈出一個視窗氣泡(使用定時器向上移動)

來源:互聯網
上載者:User

標籤:

原文連結:http://blog.csdn.net/tangaowen/article/details/5108980  

如何彈出一個視窗氣泡

      最近在工作中遇到這樣一個需求,就是需要將一個視窗從右下角工作列下面緩緩的上升到工作列的上面,現在有很多的軟體都有這樣的氣泡,比如:搜狗IME的詞條更新視窗,還比如CSDN的廣告視窗等等。

      1.首先 將要彈出的視窗移動到工作列(當前螢幕)以下

      2.然後,獲得工作列(本質是個視窗)的高度,這樣就可以知道視窗最終的位置了

      3.然後,計算獲得視窗最終停止的位置:計算公式:dwMaxHeight=當前螢幕高度-工作列視窗的頂部高度+/-適度位移值

      4.設定一個定時器 

      5.在定時器中響應視窗,調用MoveWindow或者SetWindowPos等函數來每次固定減少 一個固定的Y座標值dwIncreatmentValue

 

      關於定時器響應的幾種情況:

      1.如果上升視窗的top<=dwMaxHeight說明視窗已經浮出到預定的位置了,這個時候

       KillTimer

      2.如果上升視窗的top>dwMaxHeight,說明還沒有達到預定的位置,這個時候

         根據   上升視窗的top+dwIncreatmentValue的值來處理

         (1)上升視窗的top+dwIncreatmentValue<=dwMaxHeight,說明這次不要增加

                  dwIncreatmentValue這麼多值就可以上升到預定位置了,這個時候

                 讓 上升視窗的top=dwMaxHeight,然後SetWindowPos到預定位置,然後

                  KillTimer

          (2)上升視窗的top+dwIncreatmentValue>dwMaxHeight,說明這一次還不能

                  上升到預定的位置,所以就直接:上升視窗Rect+dwIncreatmentValue

                   然後SetWindowPos將視窗向上移動dwIncreatementValue的位置。

 

 

 

下面是相關代碼:

      1.在類中聲明兩個成員變數:

     

[cpp] view plaincopy  
  1. // 視窗浮上來所用的時間間隔  
  2. DWORD m_nUpTimeSpan;  
  3. // 每次上升的高度  
  4. DWORD m_nIncrementHeight;  
  5. // 上升的極限值  
  6. DWORD m_nMinTop;  

 

      2. 在類的建構函式中,初始化這些值:

    

[cpp] view plaincopy  
  1. m_nIncrementHeight=2;  
  2. m_nUpTimeSpan=1*10;  

 

 

    3.將整個升窗的函數寫為StartMove

    

[cpp] view plaincopy  
  1. // 開始向上移動  
  2. void    StartMove(void)  
  3. {  
  4.     //獲得當前視窗的地區範圍  
  5.     CRect WindowRect;  
  6.     ::GetWindowRect(GetSafeHwnd(),&WindowRect);  
  7.   
  8.     //獲得螢幕的寬高  
  9.     int screenwidth=GetSystemMetrics(SM_CXSCREEN);  
  10.     int screenheith=GetSystemMetrics(SM_CYSCREEN);  
  11.   
  12.     //將當前視窗移動到螢幕以下的地方  
  13.     MoveWindow(screenwidth-WindowRect.Width()-2,screenheith,WindowRect.Width(),WindowRect.Height());  
  14.     //測試  
  15.     Invalidate(FALSE);  
  16.     ShowWindow(SW_SHOWNORMAL);  
  17.     //置頂  
  18.     ::SetWindowPos(this->GetSafeHwnd(),HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);  
  19.       
  20.     //獲得 工作列 視窗  
  21.     CWnd* cwd = NULL;   
  22.     RECT rt;    
  23.     HWND hwnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);     
  24.     if(hwnd)     
  25.     {     
  26.         cwd = CWnd::FromHandle(hwnd);     
  27.     }     
  28.     cwd->GetWindowRect(&rt);  
  29.   
  30.     m_nMinTop=rt.top-WindowRect.Height()-2;  
  31.   
  32.     //上升定時器的設定  
  33.     SetTimer(TM_STARTUP,m_nUpTimeSpan,NULL);  
  34. }  

 

 

     4.OnTimer函數的實現

    

[cpp] view plaincopy  
  1. void OnTimer(UINT_PTR nIDEvent)  
  2. {  
  3.     //如果已經在極限值以上,那麼說明已經完全浮上來了  
  4.     CRect WindowRect;  
  5.     ::GetWindowRect(GetSafeHwnd(),&WindowRect);  
  6.     if (WindowRect.top<=m_nMinTop)  
  7.     {  
  8.         Invalidate(FALSE);  
  9.         ShowWindow(SW_SHOWNORMAL);  
  10.         UpdateWindow();  
  11.         //置頂  
  12.         this->SetForegroundWindow();  
  13.         ::SetWindowPos(this->GetSafeHwnd(),HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);  
  14.         KillTimer(TM_STARTUP);  
  15.     }  
  16.   
  17.     //否則  
  18.     else  
  19.     {  
  20.         int iHeight=WindowRect.Height();  
  21.         WindowRect.top-=m_nIncrementHeight;  
  22.   
  23.         if (WindowRect.top<m_nMinTop)  
  24.         {  
  25.             WindowRect.top=m_nMinTop;  
  26.             WindowRect.bottom=WindowRect.top+iHeight;     
  27.             MoveWindow(&WindowRect);  
  28.             //置頂  
  29.             this->SetForegroundWindow();  
  30.             ::SetWindowPos(this->GetSafeHwnd(),HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);  
  31.   
  32.             KillTimer(TM_STARTUP);  
  33.         }  
  34.         else  
  35.         {  
  36.             WindowRect.bottom-=m_nIncrementHeight;  
  37.             MoveWindow(&WindowRect);  
  38.         }     
  39.     }  
  40.   
  41.     CDialog::OnTimer(nIDEvent);  
  42. }  

 

http://blog.csdn.net/huasonl88/article/details/8705558

如何彈出一個視窗氣泡(使用定時器向上移動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.