2014-04-04 30 views
0

我正在開發一個依靠webbrowser winform組件的瀏覽器。如何增加標籤寬度到鼠標位置

我使用xtratabcontrol爲了與在其選項卡中具有關閉按鈕的選項卡(DevExpress組件)衝浪。

我正試圖在關閉標籤時實現什麼是Internet Explorer(它的最後一個版本)。其他標籤延伸(其寬度增加)到達鼠標poisiton(以允許卡之後,關閉選項卡不移動鼠標)

這是我添加的代碼(在關閉按鈕點擊事件)

 Point cursor = MousePosition; 
     int x = cursor.X; 
     int count = browserTabControl.TabPages.Count -1;// I don't want to include the last tab (which opens another tabs) 

     int width = x/count; 


     for (int i = 0; i < browserTabControl.TabPages.Count -1 ; i++) 
       browserTabControl.TabPages[i].TabPageWidth = width; 

我也試着刪除最後一個標籤之前得到的標籤整個寬度,然後把它劃分在新的選項卡數,並將結果設置爲每個標籤:

int current_width = (browserTabControl.TabPages.Count - 1) * browserTabControl.TabPages[0].TabPageWidth; 
     //..............some code removing the last tab (actually the tab before the last tab) 
     //after removing the last tab 
     int count = browserTabControl.TabPages.Count - 1; 

     int width = current_width/count; 


     for (int i = 0; i < browserTabControl.TabPages.Count - 1; i++) 
      browserTabControl.TabPages[i].TabPageWidth = width; 

,第一碼結果 the result is not accurate


第二碼結果:

the result is not accurate

也許問題是,當我把INT/INT我失去了分裂的其餘,我可以得到雙重的結果,但TabPageWidth是int。

回答

1

請嘗試以下解決方案:

// Initialization 
    foreach(XtraTabPage page in xtraTabControl.TabPages) { 
     if(page == addNewTabPage) continue; 
     page.TabPageWidth = 100; // turn off headers auto-size 
    } 
} 
void xtraTabControl_CloseButtonClick(object sender, EventArgs e) { 
    ClosePageButtonEventArgs ea = e as ClosePageButtonEventArgs; 
    if(ea.Page != addNewTabPage) { 
     xtraTabControl.BeginUpdate(); 
     ((XtraTabPage)ea.Page).Dispose(); 

     int totalWidth = 0; 
     var visiblePages =((IXtraTab)xtraTabControl).ViewInfo.HeaderInfo.VisiblePages; 
     int totalHeadersGrow = 0; 
     for(int i = 0; i < visiblePages.Count; i++) { 
      var pageInfo = visiblePages[i]; 
      if(pageInfo.Page == addNewTabPage) continue; 
      totalWidth += pageInfo.Bounds.Width; 
      totalHeadersGrow += (pageInfo.Bounds.Width - pageInfo.Page.TabPageWidth); 
     } 
     int count = xtraTabControl.TabPages.Count - 1; 
     int width = totalWidth/count - totalHeadersGrow/(count + 1); 
     foreach(XtraTabPage page in xtraTabControl.TabPages) { 
      if(page == addNewTabPage) continue; 
      page.TabPageWidth = width; 
     } 
     xtraTabControl.EndUpdate(); 
    } 
} 

附:您可以直接聯繫DevExpress support(我相信這是在使用產品時遇到問題的最佳方式)在這方面得到正式答覆。

0

您可以使用HeaderAutoFill功能。這將自動填充標籤到客戶區域;所以沒有必要爲用戶移動鼠標關閉多個

this.xtraTabControl1.HeaderAutoFill = DevExpress.Utils.DefaultBoolean.True; 
+0

它不適合我的目的,我也有一個標籤,我不想擴展它(上圖中的最後一個標籤),另外,它不會將標籤擴展到鼠標位置;我需要移動鼠標關閉... –

+0

IMO最後一個不應該是一個選項卡,它應該是一個'CustomHeaderButton'來添加一個新選項卡。 PS:我的答案是解決你的問題的另一種方式,即開箱即用的東西不完全是你問的。 –

+0

如果我刪除最後一個選項卡並將其轉換爲CustomHeaderButton它會工作嗎?我可以把按鈕放在同一個地方嗎? –