3

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?

Dan Ling
  • 2,965
  • 2
  • 29
  • 43
  • I'm not sure what you ended up doing, but anyone else struggling with this may try experimenting with setting the control.Capture property while beginning the drag. – gusmally supports Monica Feb 22 '23 at 17:40

1 Answers1

1

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.

Franchesca
  • 1,453
  • 17
  • 32