SetWindowLong 怎么就是用不上去。

wlllll 2007-12-30 01:37:17
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System.Drawing.Point(24, 24)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth
Me.RichTextBox1.Size = New System.Drawing.Size(240, 224)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = "aaa" & Microsoft.VisualBasic.ChrW(10) & Microsoft.VisualBasic.ChrW(10) & "b" & Microsoft.VisualBasic.ChrW(10) & "b" & Microsoft.VisualBasic.ChrW(10) & "b" & Microsoft.VisualBasic.ChrW(10) & "b" & Microsoft.VisualBasic.ChrW(10) & "f" & Microsoft.VisualBasic.ChrW(10) & "sd" & Microsoft.VisualBasic.ChrW(10) & "fs" & Microsoft.VisualBasic.ChrW(10) & "gd" & Microsoft.VisualBasic.ChrW(10) & "g" & Microsoft.VisualBasic.ChrW(10) & "d" & Microsoft.VisualBasic.ChrW(10) & "fs" & Microsoft.VisualBasic.ChrW(10) & "r" & Microsoft.VisualBasic.ChrW(10) & "weg" & Microsoft.VisualBasic.ChrW(10) & "er" & Microsoft.VisualBasic.ChrW(10) & "5" & Microsoft.VisualBasic.ChrW(10) & "43r" & Microsoft.VisualBasic.ChrW(10) & "ge" & Microsoft.VisualBasic.ChrW(10) & "r5" & Microsoft.VisualBasic.ChrW(10) & "4" & Microsoft.VisualBasic.ChrW(10) & "tgr" & Microsoft.VisualBasic.ChrW(10) & "6y" & Microsoft.VisualBasic.ChrW(10) & "tr" & Microsoft.VisualBasic.ChrW(10) & "t" & Microsoft.VisualBasic.ChrW(10) & "34y" & Microsoft.VisualBasic.ChrW(10) & Microsoft.VisualBasic.ChrW(10) & "y" & Microsoft.VisualBasic.ChrW(10) & "4r" & Microsoft.VisualBasic.ChrW(10) & "5" & Microsoft.VisualBasic.ChrW(10) & "g" & Microsoft.VisualBasic.ChrW(10) & "y" & _
"" & Microsoft.VisualBasic.ChrW(10) & "rt" & Microsoft.VisualBasic.ChrW(10) & "y34" & Microsoft.VisualBasic.ChrW(10) & "56t" & Microsoft.VisualBasic.ChrW(10) & "tr" & Microsoft.VisualBasic.ChrW(10) & "gr" & Microsoft.VisualBasic.ChrW(10) & "y" & Microsoft.VisualBasic.ChrW(10) & "rt" & Microsoft.VisualBasic.ChrW(10) & "63" & Microsoft.VisualBasic.ChrW(10) & "6y" & Microsoft.VisualBasic.ChrW(10) & "thgv"
Me.RichTextBox1.WordWrap = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(56, 280)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(296, 326)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.RichTextBox1)
Me.Name = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
MsgBox("开始勾住")
PrevWndProc = SetWindowLong(RichTextBox1.Handle, -4, AddressOf SubWndProc)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
SetWindowLong(RichTextBox1.Handle, GWL_WNDPROC, PrevWndProc)
End Sub

Public Declare Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As Long, ByVal dwNewLong As Type) As Type
Public Declare Function CallWindowProc Lib "USER32" Alias "CallWindowProcA" (ByVal OldwndProc As Type, ByVal hWnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public PrevWndProc As Type
Public Delegate Function Type(ByVal hWnd As IntPtr, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Function SubWndProc(ByVal hWnd As IntPtr, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Call Dada(CStr(MSG))
SubWndProc = CallWindowProc(PrevWndProc, hWnd, MSG, wParam, lParam)
End Function
Public Function Dada(ByVal DataText As String)
Button1.Text = DataText
End Function


End Class
...全文
299 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlllll 2007-12-30
  • 打赏
  • 举报
回复
无论我是钩什么,都会错误。
但是就是不明白…………
wlllll 2007-12-30
  • 打赏
  • 举报
回复
搜索了许多列子,终于成功了。
但是却还有一个疑问,为什么这个没办法在测试中正常使用,
而在生成文件中,却能正常使用呢?

代码如下:

Private WindowId As Int32
Private Const GWL_WNDPROC = (-4)
Private Const WM_MOUSEWHEEL = &H20A
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As WindowType) As Int32
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Int32, ByVal hWnd As Int32, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Private Delegate Function WindowType(ByVal hWnd As Int32, ByVal uMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32

Private Function FunRoll(ByVal hWnd As Int32, ByVal uMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
If uMsg = WM_MOUSEWHEEL Then
If wParam > 0 Then
Button1.Text = "上 " ' '如果频繁显示这些信息,就会卡掉 & uMsg & " & " & wParam & " & " & lParam
ElseIf wParam < 0 Then
Button1.Text = "下 "
End If
'如果频繁显示这些信息,就会卡掉
'Button1.Text = Button1.Text & uMsg & " & " & wParam & " & " & lParam
'禁止滚动
'Return 0
End If
'执行当前的消息
Return CallWindowProc(WindowId, hWnd, uMsg, wParam, lParam)
End Function


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'如果在测试中,没有MsgBox,马上就会卡住
'MsgBox(1)
If WindowId = 0 Then
WindowId = SetWindowLong(RichTextBox1.Handle.ToInt32, GWL_WNDPROC, AddressOf FunRoll)
End If
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If WindowId <> 0 Then
SetWindowLong(RichTextBox1.Handle.ToInt32, GWL_WNDPROC, AddressOf FunRoll)
End If
End Sub
wlllll 2007-12-30
  • 打赏
  • 举报
回复
楼上的,你的是VB2005么。
我的是2003
pslh 2007-12-30
  • 打赏
  • 举报
回复
正好才写过这样的,供参考:

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As IntPtr, ByVal dwNewLong As myWndProc) As IntPtr
Public Delegate Function myWndProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Public Declare Function SetWindowLong2 Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As IntPtr, ByVal nIndex As IntPtr, ByVal dwNewLong As Integer) As IntPtr
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As IntPtr, ByVal hWnd As IntPtr, ByVal Msg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr



Dim PrevWndProc&

'子类化入口
Public Sub InitClass(ByVal hWnd As IntPtr)
PrevWndProc = SetWindowLong(hWnd, -4, AddressOf SubWndProc)
End Sub

'新的窗口消息处理过程,将被插入到默认处理过程之前
Public Function SubWndProc(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
'
'
'调用默认的窗口处理过程
Return CallWindowProc(PrevWndProc, hWnd, msg, wParam, lParam)
End Function

'子类化出口
Public Sub TerminateClass(ByVal hWnd As IntPtr)
SetWindowLong2(hWnd, -4, PrevWndProc)
End Sub
gyc 2007-12-30
  • 打赏
  • 举报
回复
你显现试试更改一下你那个API声明

系统中的LONG类型,在VB.NET是integer,
integer 对应,。NET里的 Short
原来的类型在VB.NET里都缩小了一级


另一个是,在.NET 2.0 里推荐使用 ImportDLL attributable 来引用
gyc 2007-12-30
  • 打赏
  • 举报
回复
API 啊~

没有用过,不过要和COM交互,你那个类型大小要统一, 原来 系统里的LONG是4 Byte的, .NET 里的LONG 是8 Byte 的, 差了一倍哦~

另外,个人理解一下,那个API是对于窗口的吧?那个FORM ,指定一个控件可以吗?

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧