2012-07-17 113 views
2

我加入一個Wi​​nForms控制到Excel工作表調整大小的WinForm控件。控制從ListView控件繼承和簡單地「抓」的右下角,像這樣增加了用戶調整它的能力:麻煩在Excel VSTO應用

//This solution taken from 
//http://stackoverflow.com/questions/1535826/resize-borderless-window-on-bottom-right-corner/1535943#1535943 
public class MyListView : ListView 
{ 
    protected override void WndProc(ref Message m) 
    { 
     const int wmNcHitTest = 0x84; 
     const int htBottomLeft = 16; 
     const int htBottomRight = 17; 
     if (m.Msg == wmNcHitTest) 
     { 
      int x = (int)(m.LParam.ToInt64() & 0xFFFF); 
      int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16); 
      Point pt = PointToClient(new Point(x, y)); 
      Size clientSize = ClientSize; 
      if (pt.X >= clientSize.Width - 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16) 
      { 
       m.Result = (IntPtr)(IsMirrored ? htBottomLeft : htBottomRight); 
       return; 
      } 
     } 
     base.WndProc(ref m); 
    } 
} 

此控件被添加到我的工作表:

MyListView listView = new MyListView(); 

Microsoft.Office.Tools.Excel.Worksheet worksheet = 
    Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook.Sheets[1]); 

worksheet.Controls.AddControl(listView, 0, 0, 100, 100, "myListView01"); 

有了這個,我可以通過抓住右下角並將其向左/向上拖動來縮小控制。

的問題是,我不能讓它大,因爲它不會允許拖動光標過去MyListView的右邊/底部的邊框。做一些調查,我相信這是發生因爲獲得通過VSTO添加到工作表的所有控制由一個名爲VSTOContainerControl控制,誰的規模一直被設置爲相同的,因爲它的子控件的子。這個事實在MSDN博客here上得到確認。我發現,如果我編程增加母體VSTOContainerControl的大小,孩子MyListView自動增加爲好。但是,我需要用戶能夠隨意手動增加大小。我怎樣才能做到這一點?

回答

0

我建議把ListView和容器控件之間的小幅度,足以讓你可以拖動縮放柄幾個像素,和編程固定容器的大小,以新的大小+保證金。容器大小調整可以在事件處理程序中完成,以便在用戶嘗試執行大小調整時觸發任何事件。