我在XdaII(多普达696英文版)中作应用程序开发,其中涉及到为listbox添加水平滚动条。核心代码如下:
const int GWL_STYLE = (-16);
const int LB_SETHORIZONTALEXTENT = 0x0194;
const long WS_HSCROLL = 0x00100000L;
const int SWP_FRAMECHANGED = 0x0020;
const int SWP_NOMOVE = 0x0002;
const int SWP_NOSIZE = 0x0001;
const int SWP_NOZORDER = 0x0004;
const int WM_VSCROLL = 0x0115;
const int SB_BOTTOM = 7;
[DllImport("coredll.dll")]
static extern IntPtr GetCapture();
[DllImport("coredll.dll")]
static extern uint SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
[DllImport("coredll.dll")]
static extern uint GetWindowLong(IntPtr hwnd, int index);
[DllImport("coredll.dll")]
static extern uint SetWindowLong(IntPtr hwnd, int index,uint dwNewLong);
[DllImport("coredll.dll")]
static extern uint SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter,int x,int y,int cx,int cy,uint uFlags);
private void ModifyStyle(uint addStyle, uint removeStyle)
{ // Get current window style
uint windowStyle = GetWindowLong(GetCapture(), GWL_STYLE);
if (addStyle != 0)
{ // Modify style
SetWindowLong(GetCapture(), GWL_STYLE, windowStyle | addStyle);
}
else
{
// Remove style
SetWindowLong(GetCapture(), GWL_STYLE, windowStyle & ~removeStyle);
}
// Let the window know
SetWindowPos(GetCapture(), IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER | SWP_NOSIZE | SWP_FRAMECHANGED);
}
private void FrmDispatchJob_Load(object sender, System.EventArgs e)
{
this.listBox_Job.Capture=true;
IntPtr handle=GetCapture();
SendMessage(handle, LB_SETHORIZONTALEXTENT, 250,0);
SendMessage(handle, WM_VSCROLL, SB_BOTTOM,0);
ModifyStyle((uint)WS_HSCROLL, 0);
this.listBox_Job.Capture=false;
}
但是莫名的遇见下列行为异常:
Ok, on this screen, it is normal. Without touching anything.
On this screen, I tapped the white part of the scroll bar in GREEN. The text will shift to the right, but the indicator (IN RED) does not move to the right. It stays on the left side.
并且我在.net framework 中也做了相应的测试,行为还是如上异常。
我搜罗,对比了一下,并且发现在vb6.0中用如下所述http://www.freevbcode.com/ShowCode.asp?ID=5362&NoBox=True
实现 add horizontal scroll to listbox 结果一切正常。二者之间没有本质的区别呀?为什么我用 c# 实现出来的 add horizontal scroll to listbox 行为会有问题呢?很是不解,所以拿来跟大家一起探讨,还请各位不吝赐教!