Dynamic loading record in VB's ListView

zhaozj2021-02-16  66

When accessing the database, it is often encountered with too much amount, and it takes a lot of time when the data list is initialized, and the user is always too long when entering the interface. In order to solve this problem, we can implement it using dynamically loaded data.

That is, when the initialization is performed, the number of data is loaded, so that the control is quickly completed, so that the user gets control over a short period of time, when the load data can be set according to the actual situation; then, when the user scrolls the view When we detect the position of its scrolling, we determine whether to continue loading the data. If the number of data that has not been displayed is less than the reference value, continue to load a quantity of data, otherwise it will not do any action. In this way, the loading of all data can be implemented, and it does not affect the user's operation.

In fact, this approach is easy to implement in VC, as long as we do the corresponding list of WM_VScroll, but in VB, the list control does not directly support this message, which requires us to do some additional work to achieve this process . We seem to have regularly monitor the list status using the timer control, but this method not only affects the speed of the system, but also the effect is not very good, so we still use the method of responding to WM_VSCROLL messages.

In order to achieve this, the message is first processed in VB. Since the message we need is not directly handled in VB, here we have to use an API function to set the list of messages of the list control to our own defined functions. This API is setwindowlong, its prototype is

Public Declare Function SetWindowlong Lib "User32" Alias ​​"SetWindowlonga" (Byval Nindex As Long, Byval Dwnewlong As Long) As long

It has three parameters:

HWnd is the form handle to set

NINDEX is the index to set the project, in which we will take it as GWL_WndProc

DWNewlong is the value to set the item, in which the program is the address of our custom message processing function.

Its return value is a long integer, which calls the value of the corresponding item before calling this API. If the setting fails, it returns 0. For specific error messages, getlasTerror can be called, where it will return the original list control message processing. The address of the function.

Specific practices are as follows:

1. Define message processing functions in existing modules or newly established modules

Message handlers should be defined as the following form:

Public function xxxlistproc (Byval Hwnd As Long, Byval Imsg As Long, _

BYVAL WPARAM As Long, LParam As Long) AS Long

IF (IMSG = WM_VSCROLL) THEN

IF SB_ENDSCROLL = VAL ("& H" & Right (HEX (WPARAM), 4)) THEN

IF frmxxx.xxxlistView.getfirstvisible.index 100> frmxxx. Xxxlistview.listItems.count dam

LoadDatafunc frmxxx. Xxxlistview.listItems.count, 100

END IF

END IF

END IF

XXXListProc = CallWindowProc (g_oldproc, hwnd, IMSG, WPARAM, LPARAM)

The loadDatafunc is loaded in the endDatafunc in this function. It is a custom function. The first parameter is the starting position of the data load (Nth record), the second parameter is to load data (record) quantity. This function can be defined according to its own actual situation, here is just the form I use. In addition, it is important to note that this message processing function must be defined in the module without defining in the form.

2. Set the message processing function of the list control to a custom message processing function when the form is initialized.

Add the following code in the initialization function of the form where the list control is located:

g_oldproc = setwindowlong (xxxlistView.hwnd, gwl_wndproc, addressof xxxlistproc)

Where xxxlistView.hwnd is a list of window controls. This property is a hidden property, although it can't be seen, it can be used normally; we use Addressof to get our custom message processing function. g_oldproc is a global variable for saving the original message processing function address.

3. Set the message processing function of the list control to the original function when the form exits

Add the following code to the unload event in the form where the list control is located:

Setwindowlong xxxlistView.hwnd, GWL_WNDPROC, G_ OLDPROC

In this processing method, the most critical is to implement a custom processing for the window message, so we use the pointers that are not advocated in VB, although not advocated to use pointers in VB, but reasonable use of pointers will be Realization has a lot of help. Here, only one way to dynamically load data using the custom message processing function can also be applied to other aspects, thereby enhancing the functionality of the program and improving the efficiency of the program.

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

New Post(0)