vb.net子类化 vb·net

vb.net子类怎样重写父类的构造函数

你可以直接写Public Sub New(ByVal name As String, ByVal price As Double,byval haskdisk as boolean)

成都创新互联公司成立于2013年,我们提供高端成都网站建设公司成都网站制作成都网站设计公司、网站定制、成都营销网站建设微信小程序开发、微信公众号开发、成都网站推广服务,提供专业营销思路、内容策划、视觉设计、程序开发来完成项目落地,为纱窗企业提供源源不断的流量和订单咨询。

,但在写这句之前要先引用父类的构造函数, 就是加上一句怕你不明白,完整的写法就是这样:

mybase(name , price);

Public Sub New(ByVal name As String, ByVal price As Double,byval haskdisk as boolean) ;

这样就行了,希望我的回答可以帮助你!

VB怎么实现滚动条对鼠标中轴的响应啊?

VB可以使用子类化处理鼠标滚轮消息。

新建工程

在窗体中添加滚动条VScroll1

'窗体代码

Private Sub Form_Load()

'取得控件的句柄

hwndVS = VScroll1.hwnd

'保存smMap控件的默认窗口消息处理函数地址

OldWindowProc = GetWindowLong(VScroll1.hwnd, GWL_WNDPROC)

'将smMap控件的消息处理函数指定为自定义函数NewWindowProc

Call SetWindowLong(VScroll1.hwnd, GWL_WNDPROC, AddressOf NewWindowProc)

End Sub

添加一模块:

Option Explicit

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = -4

Public Const WM_MOUSEWHEEL = H20A

Public OldWindowProc As Long '用来保存系统默认的窗口消息处理函数的地址

Public hwndVS As Long '用来保存控件的句柄

'自定义的消息处理函数

Public Function NewWindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

On Error Resume Next

If msg = WM_MOUSEWHEEL Then

'则对鼠标滚轮事件进行处理

If wParam = -7864320 Then '向下滚动

Form1.VScroll1.Value = Form1.VScroll1.Value + 1

ElseIf wParam = 7864320 Then '向上滚动

Form1.VScroll1.Value = Form1.VScroll1.Value - 1

End If

Else

'调用默认窗口消息处理函数

NewWindowProc = CallWindowProc(OldWindowProc, hwnd, msg, wParam, lParam)

End If

End Function

什么是子类化,有VB或者.NET的具体实例吗

1. 何谓子类化(subclassing)众所周知,Windows是一个基于消息的系统,消息在Windows的对象之间进行着传递。子类化和Windows的钩子机制存在于消息系统之中,我们可以利用这些机制来操纵、修改甚至丢弃那些在操作系统或是进程中传递的消息,以求改变系统的一些行为。子类化技术用来截取窗口或控件之间的消息,当然是消息在到达目的窗口之前完成的操作。这些被截获的消息既可以保留也可以修改它们的状态,之后就继续发送到目的地。子类化技术实现了一些正常情况下无法实现的功能,试想鼠标右键单击TextBox,系统默认弹出Undo、Cut、Copy、Paste等菜单,我们就可以利用子类化技术来改变这个系统菜单。简单的说,子类化就是创建一个新的窗口消息处理过程,并将其插入到原先的默认窗口消息处理过程之前。子类化分为三类:实例子类化(instance subclassing)—从窗口或控件的单一实例截获消息,这种子类化技术最普遍;全局子类化(global subclassing)—能够截获从相同的窗口类创建出来的多个窗口或控件的消息;超类化(superclassing)—和全局子类化很类似,区别在于可以应用在新的窗口类上面。 2. Visual Basic 6子类化的实现在Visual Basic 6子类化的实现中我将通过一段代码的实例来介绍这一技术在VB6中的应用。下面的例子将演示如何将About加入窗口的系统菜单。①创建工程启动Visual Basic 6同时创建一个标准EXE工程。 ②在窗体中录入代码Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As LongPrivate Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long Private Const MF_BYCOMMAND = H0Private Const MF_BYPOSITION = H400Private Const MF_STRING = H0Private Const MF_SEPARATOR = H800 Private Sub Form_Load()InsertMenu GetSystemMenu(Me.hWnd, False), 0, MF_BYPOSITION Or MF_SEPARATOR, 2001, ""InsertMenu GetSystemMenu(Me.hWnd, False), 0, MF_BYPOSITION Or MF_STRING, 2002, "About Me(A)"'安装子类化入口Call Init(Me.hWnd)End Sub Private Sub Form_Unload(Cancel As Integer)‘卸载子类化Call Terminate(Me.hWnd)End Sub ③加入一个模块并录入代码Option Explicit Private Declare Function SetWindowLong Lib "user32" Alias _ "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex _ As Long, ByVal dwNewLong As Long) As Long Private Declare Function CallWindowProc Lib "user32" Alias _ "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _ hWnd As Long, ByVal Msg As Long, ByVal wParam As _ Long, ByVal lParam As Long) As Long Const GWL_WNDPROC = (-4) Dim PrevWndProc Private Const WM_SYSCOMMAND = H112Const WM_DESTROY = H2‘子类化入口Public Sub Init(hWnd As Long) PrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubWndProc)End Sub‘子类化出口Public Sub Terminate(hWnd As Long) Call SetWindowLong(hWnd, GWL_WNDPROC, PrevWndProc)End Sub‘新的窗口消息处理过程,将被插入到默认处理过程之前Private Function SubWndProc(ByVal hWnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) _ As LongIf Msg = WM_DESTROY Then Terminate (Form1.hWnd) If wParam = 2002 Then MsgBox "我是40Star", vbInformation, "hia..hia..."End If‘调用默认的窗口处理过程SubWndProc = CallWindowProc(PrevWndProc, hWnd, Msg, wParam, lParam)End Function ' -- 模块结束 -- ' 但是,需要指出的是不正确的子类化是非常危险的,将导致一个General Protection Fault(GPF)错误,致使VB应用立即崩溃。 3. Visual Basic .NET子类化的实现在.NET中使用子类化技术要比VB6中简单,因为微软在.NET中已经提供了接口,不需要我们再自己SetWindowLong了,我们做的是Overrides(覆盖) WndProc过程即可。Overrides Protected Sub WndProc( ByRef m As Message )参数m实现了Windows的消息类型。下面的例子将同样演示如何将About加入窗口的系统菜单。①创建工程创建一个VB.NET的Windows Application工程。 ②录入代码Public Class Form1 Inherits System.Windows.Forms.Form ‘中间隐去了.NET自动生成的代码‘ – 引用Win32Api – ‘Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As Int32Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Int32, ByVal nPosition As Int32, ByVal wFlags As Int32, ByVal wIDNewItem As Int32, ByVal lpNewItem As String) As Int32 Private Const MF_BYCOMMAND = H0Private Const MF_BYPOSITION = H400Private Const MF_STRING = H0Private Const MF_SEPARATOR = H800Private Const WM_SYSCOMMAND = H112 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load InsertMenu(GetSystemMenu(Me.Handle, False), 0, MF_BYPOSITION Or MF_SEPARATOR, 2001, "") '加入一条分割线‘GetSystemMenu(Me.Handle, False)是得到系统菜单的句柄,第二个参数为True的话不能改变系统菜单,所以要设为False InsertMenu(GetSystemMenu(Me.Handle, False), 0, MF_BYPOSITION Or MF_STRING, 2002, "About Me(A)") '加入About me菜单在系统菜单中 End Sub‘子类化窗口--覆盖WndProc过程 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_SYSCOMMAND Then If m.WParam.ToInt32 = 2002 Then MsgBox("我是40Star", vbInformation, "hia..hia...") End If End If ‘调用窗口默认的处理过程 MyBase.WndProc(m) End SubEnd Class

VB怎么实现象QQ左下边那样的主菜单(其实是个没有标题栏的窗体),

Option Explicit

Const MAX_TOOLTIP As Integer = 64

Const NIF_ICON = H2 '删除图标

Const NIF_MESSAGE = H1

Const NIF_TIP = H4

Const NIM_ADD = H0 '添加图标到任务栏提示区

Const NIM_DELETE = H2

Const WM_MOUSEMOVE = H200

Const WM_LBUTTONDOWN = H201

Const WM_LBUTTONUP = H202

Const WM_LBUTTONDBLCLK = H203

Const WM_RBUTTONDOWN = H204

Const WM_RBUTTONUP = H205

Const WM_RBUTTONDBLCLK = H206

Const SW_RESTORE = 9

Const SW_HIDE = 0

Private Type NOTIFYICONDATA

cbSize As Long

hwnd As Long

uID As Long

uFlags As Long

uCallbackMessage As Long

hIcon As Long

szTip As String * MAX_TOOLTIP

End Type

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _

ByVal nCmdShow As Long) As Long

Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" _

(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

Private nfIconData As NOTIFYICONDATA

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)

Dim lMsg As Single

lMsg = X / Screen.TwipsPerPixelX

If lMsg = WM_RBUTTONUP Or lMsg = WM_LBUTTONUP Then Me.PopupMenu sys

End Sub

Private Sub command1_Click()

nfIconData.hwnd = Me.hwnd

nfIconData.uID = Me.Icon

nfIconData.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP

nfIconData.uCallbackMessage = WM_MOUSEMOVE

nfIconData.hIcon = Me.Icon.Handle

nfIconData.szTip = "System Tray Example" vbNullChar

nfIconData.cbSize = Len(nfIconData)

Call Shell_NotifyIcon(NIM_ADD, nfIconData)

End Sub

Private Sub command2_Click()

Call Shell_NotifyIcon(NIM_DELETE, nfIconData)

End Sub

Private Sub command3_Click()

ShowWindow Me.hwnd, SW_HIDE

End Sub

Private Sub command4_Click()

Unload Me

End Sub

Private Sub Command5_Click()

Me.Visible = False

End Sub

Private Sub move_Click()

Call Shell_NotifyIcon(NIM_DELETE, nfIconData)

End Sub

Private Sub show_Click()

Me.Visible = True

End Sub

Private Sub exit_Click()

Unload Me

End Sub

上面是托盘

你说的那 可以把 FORM 的 外观属性设置下 就是没有标题的 窗口了

HIDE 隐藏 写在失去焦点的 事件中


当前名称:vb.net子类化 vb·net
网页路径:http://ybzwz.com/article/dooggog.html