Subclass using VB

zhaozj2021-02-16  52

Summary

Here we discuss how to use VB for subclass, and the similarities and differences of subclass of VB6 and VB.NET.

table of Contents

What is Subclassing

2. Implementation of Visual Basic 6 subclass

3. Implementation of Visual Basic .NET subclass

4. Small knot

What is Subclassing

As is well known, Windows is a message-based system, and the message is delivered between Windows objects. Subcort and Windows hook mechanisms are present in the message system, we can use these mechanisms to manipulate, modify even those who are delivered in the operating system or in processes to change the behavior of the system.

Subclassics are used to intercept messages between windows or controls, of course, is the operation that is completed before the window arrived. These intercepted messages can also be reserved or otherwise modified, followed by continuing to send to the destination. Subclassics have realized some of the normal conditions that cannot be implemented. Imagine the right mouse button to pop up UNDO, CUT, COPY, PASTE and other menus, we can use subclassics to change this system menu.

Simply put, subclass is to create a new window message processing process and insert it before the original default window message processing process.

Subclass is divided into three categories: instance subclassing - intercept messages from windows or controls, this kind of subclassic technology is most common; global subclassing - can intercepting the same window A number of windows or controls created by the class; superclassing - and global subcatenation, the difference is that it can be applied to the new window class.

2. Implementation of Visual Basic 6 subclass

In the implementation of the Visual Basic 6 subclass, I will introduce the application of this technology in VB6 by an instance of a code. The following example will demonstrate how to add About to the system menu of the window.

1 creation project

Start Visual Basic 6 At the same time, create a standard EXE project.

2 Enter code in the form

Private Declare Function GetSystemMenu Lib "User 32" (Byval Brevert As long) As long

Private Declare Function InsertMenu Lib "User32" Alias ​​"InsertMenu As Long, Byval Wflags As Long, Byval WidnewItem As String) AS Long

Private const mf_bycommand = & h0 &

Private const mf_beposition = & h400 &

Private const mf_string = & h0 &

Private const mf_separator = & h800 &

Private sub flow_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)"

'Installing Subclass Init Call Init (Me.hwnd)

End Sub

Private Sub Form_Unload (Cancel AS Integer)

'Unloading subclassification

Call Terminate (me.hwnd)

End Sub

3 Add a module and enter the code

Option expedition

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 = & h112

Const WM_DESTROY = & H2

'Subclass of entrance

Public Sub Init (HWND As Long)

PrevwndProc = setWindowlong (HWND, GWL_WNDPROC, Addressof SubwndProc)

End Sub

'Subclass outlet

Public Sub Terminate (HWND As Long)

Call setWindowlong (HWND, GWL_WNDPROC, PrevwndProc)

End Sub

'The new window message processing process will be inserted before the default process

Private function subwndproc (Byval Hwnd As Long, Byval Msg As Long, _

Byval wparam as long, byval lparam as long

As long

IF msg = WM_DESTROY THEN TERMINATE (Form1.hwnd)

IF wparam = 2002 THEN

Msgbox "I am 40star", vbinformation, "hia.... ..."

END IF

'Call the default window processing process

SubWndProc = CallWindowProc (PrevwndProc, HWND, MSG, WPARAM, LPARAM)

END FUNCTION

'- Module end -'

However, it is very dangerous to point out incorrect subclasses, which will cause a General Protection Fault (GPF) error, causing VB applications to crash immediately.

3. Implementation of Visual Basic .NET subclass

The use of subcatetling technology in .NET is simpler than VB6, because Microsoft has provided an interface in .NET, we don't need us to setwindowlong, we do Overrides WndProc processes.

OVERRIDES protected Sub WndProc (byref m as message)

The parameter m implements the Windows message type.

The following example will also demonstrate how to add About to the system menu of the window.

1 creation project

Create a VB.NET Windows Application Engineering. 2 entry code

Public Class Form1

Inherits System.Windows.Forms.form

'The code automatically generated in the middle of the .NET.

'- Quote Win32API -'

Private Declare Function GetSystemMenu Lib "User32" (Byval Brevert As INT32) AS INT32

Private 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 = & h0 &

Private const mf_beposition = & h400 &

Private const mf_string = & h0 &

Private const mf_separator = & h800 &

Private const wm_syscommand = & h112

Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

INSERTMENU (Me.Handle, False), 0, MF_BYPOSITION OR MF_SEPARATOR, 2001, "") 'Add a split line

'GetSystemMenu (me.handle, false) is a handle of the system menu. If the second parameter is true, the system menu cannot be changed, so set to false.

INSERTMENU (Me.Handle, False), 0, MF_BYPOSITION OR MF_STRING, 2002, "About ME (& A)" Add to the About Me menu in the system menu

End Sub

'Subclass window - override the WndProc process

Protected Overrides Sub WndProc (Byref M As System.windows.Forms.Message)

IF m.msg = wm_syscommand the

IF m.wparam.toint32 = 2002 THEN

Msgbox ("I am 40star", vbinformation, "hi.... ...")

END IF

END IF

'Call the window default processing process

Mybase.wndproc (m)

End Sub

END CLASS

4. Small knot

Subclassics allow us to implement some tasks that use VBs that cannot be completed under normal conditions, and through these technologies can learn Windows programming, becoming the master in VB developers.

转载请注明原文地址:https://www.9cbs.com/read-27487.html

New Post(0)