Let VB applications support mouse wheel

zhaozj2021-02-08  251

Xu Feng (Anhui Trading School)

---- First, put forward the problem

---- Since Microsoft launched an Intellimouse mouse in 1996, the mouse with rollers began to have a large line, and the application software that supported the mouse roller was more and more. But I am strange, why VB to 6.0 itself does not support the mouse wheel, VF provides a MouseWheel event from 5.0.

---- How to make the VB application support the mouse wheel? There is an article on the MSDN to resolve the application of Intellimouse mouse, which solves this problem is achieved by a 30-k third-party control, but unfortunately the control has no source code. Moreover, in order to support the use of a third-party control in the mouse wheel, it seems to be a bit no loss. This article gives a method of achieving this function in pure VB.

---- Second, solve the problem

---- We know that the message from Windows responded to the VB application needs to be interpreted by VB. But unfortunate, although VB explains all the news, only allows the user program to handle some messages in the event, VB handles other messages, or ignore these messages.

---- In VB5.0, the application cannot pass the VB direct processing message, Microsoft provides the Addressof operator from VB5.0, which allows the user program to pass the function or process to an API function. This way we can write your own window handle in the VB application, pass the window address defined in the VB to the window processing function via the AddressOf operator, which bypasses the VB's interpreter, handles the message yourself. In fact, the method can be used to handle any messages in VB.

---- The key to implementing the application supports the mouse wheel is to capture the message of the mouse wheel MSH_Mousewheel, WM_MOUSEWHEEL. Where MSH_MouseWheel is prepared for 95, the Intellimouse driver is required, while WM_MOUSEWHEEL is currently built in each version of Windows (98 / NT40 / 2000). This article mainly processes the WM_MOUSEWHEEL message. Here is the syntax of WM_MOUSEWHEEL.

WM_MOUSEWHEEL

fwkeys = loword (wparam); / * key flags * /

ZDELTA = (Short) HiWord (WPARAM);

/ * WHEEL ROTATION * /

XPOS = (Short) Loword (LPARAM);

/ * Horizontal Position of Pointer * /

YPOS = (Short) HiWord (LPARAM);

/ * Vertical Position of Pointer * /

---- Among them: fwKeys pointed out whether there is Ctrl, Shift, mouse button (left, middle, right, additional) press, allowing composite. The ZDELTA transmitting roller scrolles is slow, the value is less than zero indicates that the roller scrolls (toward the user direction), which is greater than zero means the roller forward scrolling (facing the display direction). LPARAM points out the X, Y axis coordinate on the left left of the mouse pointer.

---- Roller button is equivalent to the midbond key of the normal three-way mouse, according to the action of the roller button, WM_MBUTTONUP, WM_MBUTTONDOWN, WM_MBUTTONDBLCLK message, respectively, these messages VB have been supported in the mouse event.

---- Third, actual application

---- According to the above principles, a typical example of a database application is given.

---- 1. User interface class and student one-to-many inquiry Press and hold the mouse roller key and scroll the mouse, and the student's schedule is moved.

---- 2. ADO DATA Controls on ADO DATA Controls CONNECTIONSTRING for "Provider = MSDataShape; Data Provider = Msdasql; DSN = SCHOOL; UID =; PWD =;", the SQL command text of the RecordSelectors property is "Shape {SELECT * "AS PARENTCMD APPEND ({SELECT * FROM student} as childcmd Relate class name TO class name) as childcmd". ---- 3.TextBox's DataSource is Datprimaryrs, DataFile, as shown.

---- 4. The grid under the window is the DataGrid control, named GrddataGrid.

---- 5. The list of form from1.frm is as follows:

Private sub flow_load ()

Set grddataGrid.dataSource = _

Datprimaryrs.Recordset ("Childcmd"). UnderlyingValue

Hook me.hwnd

End Sub

Private Sub Form_Unload (Cancel AS Integer)

UnHook me.hwnd

End Sub

---- 6. Standard module Module1.bas list is as follows:

Option expedition

Public Type Pointl

X as long

Y as long

End Type

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

Declare function setWindowlong_

LIB "user32" alias "setwindowlonga" _

(Byval Hwnd As Long, _

Byval Nindex as long, _

BYVAL DWNEWLONG AS Long AS Long

Declare function systemparametersInfo _

LIB "user32" alias "systemparametersinfoa" _

(Byval uAction as long, _

Byval uParam as long, _

LPVParam as any, _

Byval Fuwinini as long) As long

Declare function screenToClient lib "user32" _

(Byval HWND As Long, Xypoint As Pointl) As Long

Public const GWL_WndProc = -4

Public const spi_getwheelscrolllines = 104

Public const wm_mousewheel = & h20a

Public WHEEL_SCROLL_LINES As Long

Global LPPREVWNDPROC AS Long

Public Sub Hook (Byval Hwnd As Long)

LPPREVWNDPROC = SETWINDOWLONG (HWND, GWL_WNDPROC, _ADDRESSOF WINDOWPROC)

'Get the scroll line value in "Control Panel"

Call SystemParametersInfo (SPI_Getwheelscroll Co., _

0, WHEEL_SCROLL1_LINES, 0)

If Wheel_Scroll_Lines> form1.grddataGrid.visibleRows Then

Wheel_scroll_lines = form1.grddataGrid.visibleROWS

END IF

End Sub

Public Sub UnHook (Byval Hwnd As Long)

Dim LngreturnValue As Long

LNGRETURNVALUE = setWindowlong (hwnd,

GWL_WNDPROC, LPPREVWNDPROC)

End Sub

Function WindowProc (Byval HW As Long, _

Byval umsg as long, _

Byval wparam as long, _

BYVAL LPARAM AS Long AS Long

DIM PT As Pointl

SELECT CASE UMSG

Case WM_MOUSEWHEEL

DIM WZDELTA, WKEYS AS INTEGER

WZDELTA = HiWord (WPARAM)

WKEYS = Loword (WPARAM)

Pt.x = loword (lparam)

Pt.y = HiWord (LPARAM)

'Convert screen coordinates to Form1. Window coordinates

ScreenToClient Form1.hWnd, Pt

With form1.grddataGrid

'Judging whether the coordinates are in the Form1.GrddataGrid window

IF pt.x> .left / screen.twipsperpixelx and _

Pt.x <(.left .width) / Screen.twipsPixelx and _

Pt.y> .top / screen.twipsperpixely and _

Pt.y <(.top .height) / Screen.twipsperpixely THEN

'Scroll clearing database

IF WKEYS = 16 THEN

'Scrolling key press, horizontally scroll GRDATAGRID

IF SGN (WZDELTA) = 1 THEN

Form1.grddataGrid.scroll -1, 0

Else

Form1.grddataGrid.scroll 1, 0

END IF

Else

'Vertical scrolling GrddataGrid

IF SGN (WZDELTA) = 1 THEN

Form1.grddataGrid.scroll 0, 0 - Wheel_Scroll_Lines

Else

Form1.grddataGrid.scroll 0, Wheel_Scroll_Lines

END IF

END IF

Else

'Mouse is not in the GrddataGrid area, scrolling master database

With form1.datprimaryrs.recordset

IF SGN (WZDELTA) = 1 THEN

If .bof = false

.MOVEPREVIOUS

If .bof = true kil

.MOVEFIRST

END IF

END IF

Else

IF .eof = false

.MOVENEXT

IF .eof = true kilovelast

END IF

END IF

END IF

End with

END IF

End with

Case Else

WindowProc = CallWindowProc (lpprevwndproc, hw, _

UMSG, WPARAM, LPARAM)

End SELECT

END FUNCTION

Public Function HiWord (Longin As Long) AS INTEGER

'Take the 32-bit value of 16 digits

HiWord = (Longin and & Hffff0000) / & H10000

END FUNCTION

Public Function Loword (Longin As Long) AS Integer

'Remove 32-bit value low 16

Loword = Longin and & HFFFF & HFFF &

END FUNCTION

---- 7. This example does not have any Win2000 / 98 environment where any additional mouse driver is installed, and the Lenovo network mouse / Logitech silver scrape, VB6.0 is passed.

---- It is necessary to further explain that the operation of the user interface mouse wheel should also follow the public user interface operation habits. Don't define some weird operations. If you prepare the application supports the mouse wheel, please see if you meet the following These standards.

---- Vertical scrolling: When the user rolled the wheel (in the user direction), the scroll bar moves down; forward the wheel (in the display direction), the scroll strip moves upward. The current choice of documents should not be affected, the current record pointer to the database is unchanged.

---- Horizontal scrolling: If there is a vertical scroll bar, the mouse wheel should first control the up and down scrolling; when the document is only horizontally rolling the bumper, the user rolled the wheel, scrolling the strip to move, forward scrolling the wheel, scroll the strip Left movement. The current choice of documents should be unaffected, and the database field selection is not affected.

---- Rolling speed: The mouse wheel rolls every score, the number of rows moving to the long document, should comply with the definition of the mouse in the control panel (the default mobile three lines), and the short document is rolled every time, in any case Never exceed the number of rows displayed by the window.

---- Translation: Translational facts is the continuous operation of the scroll bar. Translation is generally the drag of the roller button, preferably provides the direction indication cursor.

---- Auto Scroll: Auto Scrolling usually starts at the mouse wheel button clicks, and then any keystroke, mouse button, or scroll to terminate the mouse wheel. The rolling direction and speed depends on the direction and distance of the original position when the mouse offset roller button is clicked. The farther from the original position mark, the faster the automatic scroll, and the distance is slow. The application needs to provide an initial location bitmap and a direction indication icon.

---- Zoom: Rolling the roller before and after holding down the CTRL key. Rolling the wheel (in the direction of the user), reduce the ratio; the forward rolling wheel (in the direction of the display), increase the ratio.

---- Fourth, conclude

---- Through the previous introduction, you will find that "direct" supports the mouse wheel software, which needs to increase many workloads. However, there is a significant increase in the ease of software, and the author thinks this is worth it. Of course, you can also implement partial mouse roll functions by using special mouse function enhancements. Finally, it is important to note that each user has a roller mouse, the software's mouse roller is functional, so that the user can be implemented by other operations.

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

New Post(0)