C#中怎么截获ListView中滚动条事件? 我在ListView中想在滚动条滚动时做一些事情,但是该控件没有滚动事件,所以我想通过截获滚动消息来处理,但是不会截获这个消息,有谁可以帮帮我么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 啊,,,,没有人帮帮我么?难道C#就不能像VC一样可以对这些事件进行底层的处理么? 你可以在WndProc中试着拦截WM_HSCROLL和WM_VSCROLL 那通过MouseOver,Up,Down这些来仿滚动条事件 刚刚测试了一下你在Form的WndProc里面是捕捉不到ListView消息的你必须封装一下可以借助NativeWindow/* //调用方法 ListViewExtender extender = new ListViewExtender(); extender.BeginHookProc(this.listView1); //在窗体关闭的时候调用EndHookProc*/using System;using System.Collections;using System.Runtime.InteropServices;using System.Windows.Forms;namespace testApplication1{ public class ListViewExtender { /// <summary> /// native windows /// </summary> private Hashtable nativeWindows = new Hashtable(); /// <summary> /// Creates a new <see cref="ListViewExtender"/> instance. /// </summary> public ListViewExtender() { } /// <summary> /// Begins the hook proc. /// </summary> /// <param name="control">Control.</param> public void BeginHookProc(Control control) { if(nativeWindows.Contains(control)) { return; } nativeWindows.Add(control, new ListViewNativeWindow(this, control)); } /// <summary> /// Ends the hook proc. /// </summary> /// <param name="control">Control.</param> public void EndHookProc(Control control) { if(!nativeWindows.Contains(control)) { return; } ListViewNativeWindow window = nativeWindows[control] as ListViewNativeWindow; nativeWindows.Remove(control); window.ReleaseHandle(); window = null; } /// <summary> /// WNDs the proc. /// </summary> /// <param name="m">M.</param> protected void WndProc(ref Message m) { if (m.Msg == 0x114 || m.Msg == 0x115) { //滚动条 } } /// <summary> /// ListViewNativeWindow /// </summary> protected class ListViewNativeWindow : NativeWindow { private ListViewExtender listViewExtender; /// <summary> /// Creates a new <see cref="ListViewNativeWindow"/> instance. /// </summary> /// <param name="listViewExtender">ListView extender.</param> /// <param name="control">Control.</param> public ListViewNativeWindow(ListViewExtender listViewExtender, Control control) { this.listViewExtender = listViewExtender; if(control.IsHandleCreated) { AssignHandle(control.Handle); } else { control.HandleCreated += new EventHandler(OnControlHandleCreated); } control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed); } /// <summary> /// Ons the control handle created. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">E.</param> private void OnControlHandleCreated(object sender, EventArgs e) { // Control is now created, assign handle to NativeWindow. AssignHandle(((Control)sender).Handle); } /// <summary> /// Ons the control handle destroyed. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">E.</param> private void OnControlHandleDestroyed(object sender, EventArgs e) { // Control was destroyed, release hook. ReleaseHandle(); } /// <summary> /// WNDs the proc. /// </summary> /// <param name="m">M.</param> [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override void WndProc(ref Message m) { listViewExtender.WndProc(ref m); base.WndProc(ref m); } } }} VS2010打包,然而在别的电脑运行不了,只有自己能用。 C#验证码获取问题 帮我把这段JS代码翻译成C#的 在没有装vs的电脑上运行Winform 有哪些像www.codeproject.com一样的开源好站? 请问在C#下多线程的问题 ASCII 字符串转换为 double 值 我是名新手。。如何学好C#请各位前辈指教一下。。。 高分求大素数算法 帮忙解释下这段代码。谢谢。。今天结帖! 做的第一个程序:淘宝网销售分析,大家PP 我有一端关于DOM解析xml的代码,,老是出错.写出来大家给我看看..谢谢!
你在Form的WndProc里面是捕捉不到ListView消息的
你必须封装一下
可以借助NativeWindow/*
//调用方法
ListViewExtender extender = new ListViewExtender();
extender.BeginHookProc(this.listView1);
//在窗体关闭的时候调用EndHookProc
*/
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace testApplication1
{
public class ListViewExtender
{
/// <summary>
/// native windows
/// </summary>
private Hashtable nativeWindows = new Hashtable(); /// <summary>
/// Creates a new <see cref="ListViewExtender"/> instance.
/// </summary>
public ListViewExtender()
{
} /// <summary>
/// Begins the hook proc.
/// </summary>
/// <param name="control">Control.</param>
public void BeginHookProc(Control control)
{
if(nativeWindows.Contains(control)) {
return;
} nativeWindows.Add(control, new ListViewNativeWindow(this, control));
} /// <summary>
/// Ends the hook proc.
/// </summary>
/// <param name="control">Control.</param>
public void EndHookProc(Control control)
{
if(!nativeWindows.Contains(control)) {
return;
} ListViewNativeWindow window = nativeWindows[control] as ListViewNativeWindow;
nativeWindows.Remove(control);
window.ReleaseHandle();
window = null;
} /// <summary>
/// WNDs the proc.
/// </summary>
/// <param name="m">M.</param>
protected void WndProc(ref Message m)
{
if (m.Msg == 0x114 || m.Msg == 0x115)
{
//滚动条
}
} /// <summary>
/// ListViewNativeWindow
/// </summary>
protected class ListViewNativeWindow : NativeWindow
{
private ListViewExtender listViewExtender; /// <summary>
/// Creates a new <see cref="ListViewNativeWindow"/> instance.
/// </summary>
/// <param name="listViewExtender">ListView extender.</param>
/// <param name="control">Control.</param>
public ListViewNativeWindow(ListViewExtender listViewExtender, Control control)
{
this.listViewExtender = listViewExtender; if(control.IsHandleCreated) {
AssignHandle(control.Handle);
} else {
control.HandleCreated += new EventHandler(OnControlHandleCreated);
}
control.HandleDestroyed += new EventHandler(OnControlHandleDestroyed);
} /// <summary>
/// Ons the control handle created.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
private void OnControlHandleCreated(object sender, EventArgs e)
{
// Control is now created, assign handle to NativeWindow.
AssignHandle(((Control)sender).Handle);
} /// <summary>
/// Ons the control handle destroyed.
/// </summary>
/// <param name="sender">Sender.</param>
/// <param name="e">E.</param>
private void OnControlHandleDestroyed(object sender, EventArgs e)
{
// Control was destroyed, release hook.
ReleaseHandle();
} /// <summary>
/// WNDs the proc.
/// </summary>
/// <param name="m">M.</param>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m)
{
listViewExtender.WndProc(ref m);
base.WndProc(ref m);
}
}
}
}