scrolling a panel

B

Bob Dankert

I am having trouble scrolling a panel horizontally to match the horizontal
scroll from a listbox. Currently, I catch the listbox WM_HSCROLL in the
listbox WndPrc, and have this call a method in my Panel to scroll the panel.
The method is:

public void ScrollMeHorizontal(System.IntPtr wParam)
{
System.Windows.Forms.Message ScrollMessage = new Message();
ScrollMessage.HWnd = this.Handle;
ScrollMessage.Msg = 0x0114;
ScrollMessage.WParam = wParam;
this.DefWndProc(ref ScrollMessage);
MessageBox.Show("Test");
}

The message box is displaying 'test', so I know the method is getting
called, however the panel is not being scrolled at all. For reference, the
panel contains two labels side by side, and right-most label is half cut-off
and should scroll.

(As you can probably guess, I am implementing my own column headers for a
list box - and if your wondering, netiehr a listview nor datagrid will work
for my application.)

Thanks!

Bob Dankert
 
B

Bob Dankert

I made some progress with this issue.
1. Need to use the SendMessage API, can not send a message through
DefWndPrc
2. Need to set AutoScroll = true for the Panel.

However, I am still having trouble getting the Panel to match the listbox
when a SB_THUMBPOSITION or SB_THUMBTRACK call is made. In fact, the panel
does not scroll at all for either of these. The Panel does seem to respond
to the rest of the scroll messages, however.

I would greatly appreciate any help with this...

Bob Dankert
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Based on my understanding, you want to make the panel horizontally scroll
with SB_THUMBPOSITION or SB_THUMBTRACK.

I have writen a sample, you may use the code snippet to this done:
const int WM_HSCROLL=0x114;
const int SB_THUMBPOSITION=0x4;
const int SB_THUMBTRACK = 0x5;

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam,
IntPtr lParam);
private void button4_Click(object sender, System.EventArgs e)
{
SendMessage(this.panel1.Handle, WM_HSCROLL,
(IntPtr)(0x100000000&SB_THUMBPOSITION), IntPtr.Zero);
}
For more information of wParam and lParam, please refer to the document of
WM_HSCROLL Notification in MSDN.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Dankert

Jeffrey,

I found the problem with my code after doing a bit of investigating. It
seems that the internal method SetDisplayRectLocation needs to be called
after a SB_THUMBPOSITION or SB_THUMBTRACK scroll event. Here is my modified
code for scrolling which works:

public void ScrollMeHorizontal(System.IntPtr wParam)
{
int lo = LOWORD(wParam);
int hi = HIWORD(wParam);
SendMessage(this.Handle, (uint)0x0114, (UIntPtr)wParam.ToInt32(),
(System.IntPtr)0);
if (lo == 4 || lo == 5)
SetDisplayRectLocation(-hi, -DisplayRectangle.Y);
this.Invalidate();
foreach (Control ctrl in this.Controls)
ctrl.Refresh();
}

Unfortunately, I am unable to find any information regarding
SetDisplayRectLocation and DisplayRectangle as these are supposed to be
internal methods. Is there somewhere I can get more information on the
method and property which have fixed my code?

Thanks,

Bob Dankert
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Thanks very much for your feedback!

I think I am not fully understand your problem. I think you just want to
programmatically control the scroll bar, yes? Does my code snippet of
P/inovke like this work for you?
const int WM_HSCROLL=0x114;
const int SB_THUMBPOSITION=0x4;
const int SB_THUMBTRACK = 0x5;

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam,
IntPtr lParam);
private void button4_Click(object sender, System.EventArgs e)
{
SendMessage(this.panel1.Handle, WM_HSCROLL,
(IntPtr)(0x100000000&SB_THUMBPOSITION), IntPtr.Zero);
}
At my side, this code may will scroll the panel horizontally.

Also, we may use Panel's AutoScrollPosition property to control the scroll
position, like this:
private void Form1_Load(object sender, System.EventArgs e)
{
pt=new Point(this.panel1.AutoScrollPosition.X,
this.panel1.AutoScrollPosition.Y);
}

private Point pt;
private void button5_Click(object sender, System.EventArgs e)
{
pt=new Point(pt.X +5, pt.Y+5);
this.panel1.AutoScrollPosition=new Point(pt.X, pt.Y);
} //scroll the horizontally 5 pt and vertically 5 pt.

If I misunderstand you, please feel free to tell me, thanks.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Dankert

Jeffrey,

Sending a SB_THUMBPOSITION and SB_THUMBTRACK message does not work for me
unless I call the ScrollableControl.SetDisplayRectLocation method after the
message is sent. I found other people that were experiencing the same
problem. Everything is working great with this method, so I feel pretty
confident that it should work well for me.

Thanks for following up!

Bob Dankert
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Ok, I am glad it works well for your. Anyway, if you need further help,
please feel free to tell me, I will work with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top