ScrollWindow has Absolutely No Effect

1.4k views Asked by At

I'm creating a User Control for WinForms and have the need to scroll a section of the control window.

Inexplicably, it appears no ScrollWindow() method is available in WinForms. So I'm trying to use InteropServices to use the Win32 API ScrollWindow() function using variations of the following:

[StructLayout(LayoutKind.Sequential)] 
public struct RECT
{
    public int left; 
    public int top; 
    public int right; 
    public int bottom; 

    public RECT(Rectangle rect)
    {
        this.bottom = rect.Bottom;
        this.left = rect.Left;
        this.right = rect.Right;
        this.top = rect.Top;
    }
}

[DllImport("user32")]
public static extern int ScrollWindow(IntPtr hWnd, int nXAmount, int nYAmount,
    ref RECT rectScrollRegion, ref RECT rectClip);

void MyScrollFunc(int yAmount)
{
    RECT r = new RECT(ClientRectangle);
    ScrollWindow(Handle, 0, yAmount, ref r, ref r);
}

The result is that this code does absolutely nothing. I've tried all sorts of variations of this code, including calling Update() after the scroll (which shouldn't be necessary).

ScrollWindow() is returning 1, which signifies success but it simply has no effect on the content of the control window no matter what I try.

Does anyone know if there's something about a user control that prevents modifying the display this way? I'm testing this on C# Express Edition 2008 on Windows XP.

2

There are 2 answers

0
Jonathan Wood On BEST ANSWER

So, it turns out ScrollWindow() works just fine. I had copied some code from the Web that had the members of the RECT structure out of order. Thanks to MusiGenesis for the tip that got me looking in the right place (I just assumed the code I copied was correct--my mistake.)

Yes, I do like to write efficient code sometimes and it usually means I butt heads in places like SO by people who think I should just use whatever's on the shelf. Different people like to approach software development in different ways, and I think that's okay.

If anyone's curious, I was writing a multi-line status control. I wanted it to be fast because my app can send many messages to the control in a short period of time. An article about the control, along with the final source code has been posted at http://www.blackbeltcoder.com/Articles/controls/a-scrolling-status-control.

1
MusiGenesis On

As Will pointed out in a comment, you can easily make your user control scrollable by setting its AutoScroll property - there is no need tap the Win32 API to achieve this functionality.

If you really want to use the API, at least use ScrollWindowEx instead of ScrollWindow.

Update: Since I randomly guessed this, the answer is:

Pass Null for the two RECT parameters.