Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble with resizable winform control in Excel VSTO application

I am adding a Winforms control to an Excel worksheet. The control inherits from ListView and simply adds the ability for a user to resize it by "grabbing" the bottom right corner, like so:

//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);
    }
}

This control gets added to my worksheet:

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");

With this, I am able to make the control smaller by grabbing the bottom right corner and dragging it left/upwards.

The problem is that I am not able to make it larger because it won't allow dragging the cursor past the right/bottom borders of MyListView. After doing some investigation, I believe that this is occurring because all controls that get added to the worksheet via VSTO are parented by a control called VSTOContainerControl, who's size is always set to be the same as it's child control. This fact is confirmed on an MSDN blog here. I discovered that if I programatically increase the size of the parent VSTOContainerControl, the child MyListView automatically increases as well. However, I need the ability for the user to manually increase the size at will. How can I achieve this?

like image 954
Dan Ling Avatar asked Nov 24 '25 02:11

Dan Ling


1 Answers

I would suggest putting a small margin between the listview and the container control, enough so that you can drag the resize handle a few pixels, and programatically fix the size of the container to the new size + margin. The container resize can be done in an event handler for whatever event gets fired when the user attempts to do the resize.

like image 152
Franchesca Avatar answered Nov 26 '25 15:11

Franchesca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!