VB and Windows API lecture (four)
VB 无解 problem with the API solution
Wang Guorong
Introduction to Windows's message system, which may be more difficult (or more boring), which is the author who wants to avoid things that cannot be avoided, after all, the concept of the message is not available in the process of Windows API. Or lack of 15 questions raised in this issue, there is a relationship between 2 solutions and messages. In addition to the application of the message, since the book is scheduled to be included in the Run! PC 2 months, the author wants to come to a relaxed and practical thing, so the special selection of readers have asked and VB unable to solve the problem, with Windows API To solve it, in addition to the fourth and 15th, these solutions are not too difficult, and you can apply them in your VB.
Question 1: I just want to throw the file to the "resource recovery" instead of deleting it from the hard drive.
Question 2: How to copy the entire directory (including subdirectories and all files)?
Question 3: How to quickly change the directory of the file?
Question 4: How to make TextBox do not display the fast display function table when the right button is pressed?
Question 5: How do I read the directory of Windows?
Question 6: How to build a shortcut to the "startup" folder?
Question 7: How to start the Windows preset executive to open a file?
Question 8: How to wait after a certain program to wait for this one to continue the execution.
Question 9: How to calculate the number of lines in multi-line TextBox?
Question 10: How do I determine if a DRIVE is a disc machine?
Question 11: How to read the setup time and access time of the file?
Question 12: How to control multi-line TEXTBOX rollover in a program?
Question 13: How to fill a color color like a general drawing software?
Question 14: How to read the space and free space of the disk?
Question 15: When the table is subjected to a table, it is desirable to display its illustration to the lower right corner of the work column.
Read this article Previous:
During the call of the Windows API, we must declare the API function of the call and its related constant, self-order, but these declarations are usually smelling and long, so that the author puts them in the final Appendix, of course, in order to facilitate you to quote, these declarations are also included in the author's website, please download it yourself. In addition, all the problems discussed in this article also accompany the model, which is included in the downloaded file.
Question 1: I just want to throw the file to the "resource recovery" instead of deleting it from the hard drive.
This definitely cannot call the Kill Narration provided by VB. To the resource recovery cartridge, the description of the call is as follows:
DIM SHFILEOP As SHFileOpstruct
SHFILEOP.WFUNC = fo_delete
SHFILEOP.PFROM = "c: /test.txt" chr (0)
SHFILEOP.fflags = fof_allowundo fof_noconfirmation
Call ShfileOperation (SHFileop) has several things worth noting in the above description:
FOF_ALLOWUNDO indicates that the deleted file can be restored in the future, and this set value is absolutely necessary. FOF_NOCONFIRMATION indicates that the traffic window is not displayed asked the user. If the file is thrown to the resource recovery tube, if you want to ask the user, you should cancel this set value. Please pay attention to "C: / Test.txt" must be added to CHR (0).
With the above method, multiple files can also be deleted once. At this time, simply string multiple file names and separated by chr (0), suppose we want to delete C: /Test1.txt ,c: /test2.txt , And C: / Test3.txt, etc., the program is as follows:
DIM SHFILEOP As SHFILEOPSTRUCTDIM FILES AS STRING
Files = "c: /test1.txt" chr (0) "c: /test2.txt" chr (0) "C: /TEST3.TXT" CHR (0)
SHFILEOP.WFUNC = fo_delete
SHFILEOP.PFROM = files
SHFILEOP.fflags = fof_allowundo fof_noconfirmation
Call ShfileOperation (SHFILEOP)
Question 2: How to copy the entire directory (including subdirectories and all files)?
If the features provided by VB must be used, there must be several DIR, MKDIR, and FILECOPY, and the program you need is not simple. If you don't introduce it, if you call the SHFileOperation API, It is only a few lines, suppose we want to copy all files of the C: / TEMP directory (including its subdirectory) to the C: / TEMP2 directory, the program is as follows:
DIM SHFILEOP As SHFileOpstruct
SHFILEOP.WFUNC = FO_COPY
SHFILEOP.PFROM = "c: / temp /*.*"
SHFILEOP.pto = "c: / temp2 /*.*"
SHFILEOP.fflags = fof_allowundo fof_noconfirmmkdir
Call ShfileOperation (SHFILEOP)
Please note that one thing in the above description: fof_noconfirmmkdir means that the traffic window is not displayed asked the user "whether to create a directory", if this set value is canceled, and when the directory specified by SHFILEOP.PTO does not exist, Windows will ask Whether the user establishes a directory. (Shfileop.pto = "C: / TEMP2 /*" "Write SHFileop.pto =" C: / TEMP2 can also be)
Question 3: How to quickly change the directory of the file?
When we want to change the directory of a file, if you use the functions provided by VB, you must first execute the file to copy the file to another, and then delete the original file, for example:
FileCopy Path1 & FileName, Path2 & FileName
Kill Path1 & FileName
This approach is for a relatively large file (assuming is 100MB), it is ware for time. If you use the SHFileOperation API function, you can move the file directly to another directory, the method is as follows: (Suppose will be C: / Test4.txt is moved to a C: / TEMP directory) DIM SHFILEOP As ShfileOpstruct
SHFILEOP.WFUNC = FO_MOVE
SHFILEOP.PFROM = "c: /test4.txt" chr (0)
SHFILEOP.pto = "C: / TEMP"
SHFILEOP.fflags = fof_allowundo fof_noconfirmation
Call ShfileOperation (SHFILEOP)
There are several things worth noting in the above narrative:
The parameters specified by SHFileop.PTO must be an existing directory. The file can be moved to another disk, but its role is equivalent to copying the file, then deleting the original file, only moving in the same disk with rapid movement. If you want to move multiple files at a time, please refer to the description of "Question 1" this article.
Question 4: How to make TextBox do not display the fast display function table when the right button is pressed?
When we press the right button on the TEXTBOX, TextBox always displays the flash function table containing "recovery, cut, copy, post ...", how to call TextBox Don't do this?
This problem is a bit difficult, and we must take advantage of our "Interfaces of Windows" in the previous period. In order not to let TextBox display the preset functional table, we must use the interpolation window to eat WM_RBUTTON DOWN message, on the details of the production, including the following:
1. Set the window program of the jacket: Suppose the name of the TextBox is Text1, and the name of the window we wrote is WndProc, as follows:
DIM RET As Long
PrevwndProc = getWindowlong (Text1.hWnd, GWL_WNDPROC)
Ret = setWindowlong (Text1.hWnd, GWL_WndProc, Addressof WNDPROC)
2. Writing of the window program:
Function WndProc (Byval Hwnd As Long, Byval WParam As Long, Byval LParam As long) As long
IF msg = WM_RBUTTONDOWN THEN
'Eat this message
Else
WndProc = CallWindowProc (PrevWndProc, HWND, MSG, WPARAM, LPARAM)
END IF
END FUNCTION
When the above window program receives the WM_RBUTTONDOWN message, no longer call CallWindowProc, so TextBox will not receive a message that "pressing the right button", and does not display the preset flash function.
3. Cancel the team:
'prevwndproc is the location of the previous window programs stored in the intercom
Ret = setWindowlong (Text1.hWnd, GWL_WNDPROC, PrevWndProc)
Please pay special attention to using the above solution. Since our program eats WM_RBUTTONDOWN message, when the user presses the "right button", TextBox does not happen, which will make the program in the Text1_MouseDown event program only The user will be executed when the user presses the "left button". The solution is called the text1_mousedown event program when the WndProc window is received, as follows: Function WndProc (Byval Hwnd As Long, Byval Msg As Long , Byval WParam As Long, Byval LPARAM As Long AS Long
IF msg = WM_RBUTTONDOWN THEN
Call text1_mousedown (parameter ...)
Else
WndProc = CallWindowProc (PrevWndProc, HWND, MSG, WPARAM, LPARAM)
END IF
END FUNCTION
Question 5: How do I read the directory of Windows?
Although the preset directory installed by Windows is "C: / Windows", because the user can freely set the directory installed by the Windows installation, "c: / windows" cannot be assumed to be a directory where Windows is located, to read the directory of Windows You need to use the getWindowsDirectory API function, the following is an example of a call:
DIM S AS STRING * 80, Length As Long
Dim WinPath As String
Length = getWindowsDirectory (S, Len (s))
WinPath = Left (s, length)
Then the WinPath will be equal to the directory of Windows after execution.
Question 6: How to build a shortcut to the "startup" folder?
If you want to create a shortcut, use the Windows API to be more troublesome, so the author wants to use a DLL file attached to VB - VB5stkit.dll (if it is a VB 4.0 32-bit version, it is STKIT432.DLL), which is in the machine installed with VB. This file will appear under Windows's System directory, in addition, this file is also available under VB's setupkit / kitfil32 directory. There is a function called Fcreates Helllink in VB5stkit.dll, which can be used to build "shortcut", which contains 4 parameters, the meaning is as follows:
. "The folder," .. "indicates the" start "tool column," ../ .. "indicates the directory where Windows is located.
2. Shortcutname: shortcut name.
3. EXEPATH: The full path to the program or file.
4. Params: The program parameters of the parameter exepath.
Suppose we want to set the "C: /Windows/notePad.exe" execution file to the "startup" folder name to "Notepad" shortcut, the call is as follows:
RET = FcReateshellLink ("/ Start", "Notepad", "C: /Windows/NotePad.exe", "") The parameter in the above form is the most worthless of the Folder, because the "startup" data clip is "program set The sub-data clip, so write this parameter "/ start", and then give an example, suppose we want to build the same shortcut on the "desktop", this parameter should be set to "../ .. / Desktop "(NT Chinese version is" ../../ desktop "), because" ../ .. "represents Windows's directory, and the so-called" desktop "is actually the Desktop subfold under Windows. The clip is written, so this parameter is written "../../desktop"("../../ desktop").
In addition to the four parameters, the return value of Fcreates Helllink indicates that "whether it is successfully established, if equals 1, it is successful, equal to 0, indicates failure.
Using Fcreateshelllink in VB5, the API declaration must be written as follows:
Declare function fcreateshelllink lib "vb5stkit.dll" (ByVal shortcutname as string, byval exech AS String, Byval params as string) AS Long
However, if you are using VB4 32-bit version, you must change the above VB5stkit.dll to STKIT432.DLL. Finally, please note that vb5stkit.dll (STKIT432.DLL) is not the API function provided by Windows. Before the call, you must copy this file to Windows, Windows's system directory, or the application where the application is located, but if you use VB "installation Elf installed application, "Installing the Elf" automatically copys this file to the system directory of Windows.
In order to let you further experience the use of the Fcreateshelllink function, the author is particularly prepared for the form of Figure -1 (placed in the example-of-style FORM3):
Figure -1 of the FcreateshellLink test program written by the author
You can use this form to set different parameters (the correspondence of the corresponding column on each field on the form), then detect the case where the shortcut is established, the author must explain that after the call is established, the call is established. When the "Start" tool is pressed, the shortcut to be established does not necessarily appear immediately, because the "Start" tool column is not updated, but you can use the file-based tube function table "View / Reorganization Let the "Start" tool column update now.
Incident: Chinese Windows "Start" The folder name is "start", but the English Windows is "startup", and the Windows in different languages may be different, "desktop" is also, so if you are in " Set shortcuts in the data sheet or "desktop" must be considered different languages.
Question 7: How to start the Windows preset executive to open a file?
For example, .txt's file hopes to open with "Notepad", .DOC file is turned on with Word, .bmp's file is turned on with "small painter" ..., it seems to use the "Archive General Management" to open the file. When we want to perform a certain program in the VB program, the easiest way is to call the shell's narrative, such as "shell" notepadc: /test.txt "", but shell's narrative must specify the execution file, so do not apply to this A problem. Want to open a file like a file, you need to call the Shellexecute API function, first give a simple example, assume that you want to turn on the geneR.txt file of the C: / Windows directory, the method is as follows:
Call Shellexecute (Me.hwnd, "Open", "C: /Windows/general.txt", "", ", sw_show)
The above describes the parameters four and parameter five, where the parameter fourth indicates the parameters passing to the executive file, but because this shellexcute is already used to turn on the file, this parameter is usually set to "", Fifth represent a working directory, if set to "", the directory of the file is the work directory. In addition, the parameter six indicates that the file is displayed after the file is turned on, and SW_SHOW represents the normal size. If set to sw_showminimized, the minimized window is displayed, if set to sw_showmaximized, the maximized window is displayed.
Question 8: How to wait after a certain program to wait for this one to continue the execution.
When we call the shell, it will return a value. This value is called the Process ID. Using this Process ID, we can call the openprocess API to get Process Handle, then use the Process Handle call WaitForsingleObject, you can wait for the shell. After the program is executed, it will continue to be executed. The program is as follows: (to perform the NOTEPAD program)
DIM PID AS long 'declared Process ID variable
DIM Phndn as long 'declared Process Handle Variables
PID = shell ("NOTEPAD", VBNORMALFOCUS) 'Shell Remove Process ID
PHND = OpenProcess (Synchronize, 0, PID) 'gets Process Handle
IF phnd <> 0 THEN
Call waitforsingleObject (phnd, infinite) 'Unlimited waiting, until the end
Call CloseHandle (PHND)
END IF
As for the working principle of the program, because the story is very long, forgive the author does not have further explanation. When using this method, please pay special attention to the time when waiting, the original program is completely unconnected, so the author recommends to hide the original window before calling WaitForsingleObject, until waiting for the end (that is, WaitForsingleObject) Re-displays the window.
Question 9: How to calculate the number of lines in multi-line TextBox?
If this problem does not use the Windows API, use VB, the method is as follows: DIM S AS STRING, N AS INTEGER, POS AS INTEGER
S = text1.text
POS = INSTR (S, VBCR VBLF) 'VBCR VBLF is the breakfast word of TextBox
While Pos> 0
N = n 1
S = MID (S, POS 2)
POS = INSTR (S, VBCR VBLF)
Wend
N = n 1
'N is equal to the number of lines of TEXT1
However, the above-mentioned programs have a large number of walked lines, and the performance performance will be a bit, so it can be considered using the following API methods:
DIM N As Long
N = sendMessage (text1.hwnd, em_getlinecount, 0, byval 0 &)
'N is equal to the number of lines of TEXT1
Question 10: How do I determine if a DRIVE is a disc machine?
The GetDriveType function for the Windows API is required, assuming that we want to determine if the "D:" disc is a disc machine, the method is as follows:
DriveType = getDriveType ("d: /")
If DriveType = Drive_cdrom Then 'represents the disc
Note that getDriveType's parameters cannot be written as "D" or "D:", must be written into "d: /". In addition to judging the disc, GetDriveType, the following is the meaning of various transmission values:
Biographic significance 0 No DRIVE_REMOVABLE (= 2) mobility disk, such as a flop drive_fixed (= 3) Hard dish drive_remote (= 4) remote (network) storage device Drive_CDROM (= 5 ) Disc drive_ramdisk (= 6) RAM DISK
If we want to list the type of disk machine, you can first place a DriveListBox on the form (assuming its name is a drive1) control element, and then use the following programs:
DIM DTYPESTR (0 to 6) AS String, DTYPE AS LONG
DTYPESTR (0) = "No Judgment": DtypeStr (1) = "The root directory does not exist"
DTYPESTR (2) = "Soft Disc": DtypeStr (3) = "Hard Disc"
DTYPESTR (4) = "Remote (Network) Storage Device
DTYPESTR (5) = "CDM": DTYPESTR (6) = "Ram Disk"
For i = 0 to drive1.listcount - 1
DRV = Left (drive1.list (i), 2) & "/"
DTYPE = GetDriveType (DRV)
Debug.Print Drv & "Is" & DtypeStr (DTYPE)
NEXT
Question 11: How to read the setup time and access time of the file?
If we use the FileDateTime provided by VB to read the file time, the result is the last time that the file is finally modified, but when we use the archive total management to view a file, in addition to the file "modification time", But you can also see the "Establishment Time" and "Access Time" of the file, as shown in Figure -2. Figure-2 The file content displayed by the Archive Tube except "modification time", and "Establishment Time" and "Access Time".
To further read the relevant information, you must first call the OpenFile of the API function to get the file, and then use the Handle Call GetFileinformationByHandle to read the relevant information of the archive, and in the read file related information, you contain files to establish, modify And the access time, the program execution process is as follows: (Assume that the file you want to read is "c: /autoexec.bat")
DIM FILEHANDLE AS Long
DIM fileInfo as by_handle_file_information
Dim lpreopenbuff as offstruct, ft as systemtime
DIM TZONE AS TIME_ZONE_INFORMATION
DIM DTCREATE AS DATE 'Establishing Time
DIM DTACCESS AS DATE 'Access Date
DIM DTWRITE AS DATE 'Modification Time
DIM BIAS AS Long
'Gets the File Handle for Autoeexec.Bat first
FileHandle = OpenFile ("c: /autoexec.bat", lpreopenbuff, of_read)
'Read file information using File Handle
Call getfileinformationbyHandle (FileHandle, FileInfo)
Call CloseHandle (FileHandle)
'Read Time Zone information, because the file time of the previous step is "Greenwich" time
Call getTimezoneInformation (Tzone)
Bias = Tzone.bias' time difference, in units of "分"
Call filetimetosystemtime (fileInfo.ftcreationTIME, FT) 'conversion time data structure
DTCREATE = DateSerial (ft.wyear, ft.wmonth, ft.wday) TimeSerial (ft.wHour, ft.wminute - bias, ft.wsecond)
Call filetimetosystemtime (fileinfo.ftlastaccesstime, ft)
DTACCESS = DateSerial (ft.wyear, ft.wmonth, ft.wday) TimeSerial (ft.whour, ft.wminute - bias, ft.wsecond)
Call filetimetosystemtime (fileInfo.ftlastwritime (FT)
DTWRITE = DateSerial (ft.wyear, ft.wmonth, ft.wday) TimeSerial (ft.wHour, ft.wminute - bias, ft.wsecond)
The DTCREATE, DTWRITE, and DTACCESS variables obtained by the above process are performed, ie, respectively, modify, and access time.
Question 12: How to control multi-line TEXTBOX rollover in a program?
First, please review the program -9 program. In the program -9 program, we use SendMessage to transfer the em_getlinecount message to TextBox, and when TextBox receives the message, the number of the message will be determined, then calculate the number of rows and return, this work Mode, we can send the message sent to the TextBox as the instruction to the TextBox, and for the scroll control of TextBox, the transmitted message (downward instruction) is EM_LINESCROLL, the program is as follows: DIM N As Long Call SendMessage (Text1.hWnd, EM_LINESCROLL, 0 &, BYVAL N)
Call sendMessage (Text1.hWnd, EM_LINESCROLL, 0 &, BYVAL-N) 'on Volume N
Call sendMessage (Text1.hWnd, EM_LINESCROLL, N, BYVAL 0 &) Right Volume N
Call sendMessage (Text1.hWnd, EM_LINESCROLL, -N, BYVAL 0 &) left volume N columns
For example, you want to volume 5 rows on the right volume of 3 columns, the description of the call is as follows:
Call sendMessage (Text1.hwnd, EM_LINESCROLL, 3 &, BYVAL-5 &)
Question 13: How to fill a color color like a general drawing software?
If you want to fill a zone into a color, you can call the floodfill API function, this function contains the following four parameters:
HDC: Handle Of DC, see the meaning of DC (Device Context) See the last test, for VB's object, Form and Picturebox have an attribute named HDC, which can be called a floodfill function. X, Y: The coordinate position, but please note that the unit is Pixel (pixel). CRCOLOR: The border color of the closed area.
Let the author explain to the setting method of the above parameters 2, 3, 4, refer to Figure -3, suppose we want to fill a square area, then (x, y) can set any point in the region, and CRColor Then set the color of the border, assuming that the frame color is black, set the value equal to RGB (0, 0, 0) (equal to 0), assuming that the box color is red, set the value equal to RGB (255, 0, 0).
Figure -3 FLOODFILL parameters
Although FLOODFILL is a function of filling the region, the number of simply calls this function cannot be filled, because Windows GDI is specified, the brush (picture brush) object must be set to DC before filling the area, and the color of the brush object It is a color of the filled. In order to establish Brush objects, we must do this:
DIM HBRUSH AS Long
Hbrush = createsolidbrush (color setting) 'Establishing Brush object
Call SelectObject (HDC, HBrush) 'Sets the Brush object to DC
'Call FLOODFILL again, for example:
Call Floodfill (HDC, X, Y, RGB (0, 0, 0))
For actual examples, you can refer to the RUNPC49H.FRM form completed by the author, as shown in Figure -4, use this form, as long as the color is selected, you can see the floodfill execution by the selection of the color to fill the color. As a result, in this example, please pay attention to one thing: the author sets the ScaleMode property of the form to "3-pixel" because the FLOODFILL (x, y) parameter is a pixel as a coordinate unit. Figure -4 FLOODFILL model
Question 14: How to read the space and free space of the disk?
Read the space of the disk and the available space to call the GetDiskFreespace API function, this one of these functions contains 5 parameters, the meaning is as follows:
Rootpathname: The root directory of the disk machine, with C: Take "C: /", and cannot be written as "C:" or "C". SectorsperCluster: The number of magnetic tracks per cluster. BYTESPERSECTOR: The number of bit yuan groups for each magnetic track. NumberoffreeClusters: The number of available creads. TotalNumberOfClusters: The total cluster set.
The formula of the calculation of the disk space (number of bits) is equal to = (the number of spots of each magnetic rail × the number of magnetic tracks of each cluster is × coupted), so the program of obtaining the disk space and the available space is as follows. : (Take C: Take Case)
Dim Sectors As Long, Bytes As Long, Free As Long, Total As Long
DIM FREEKB As Long, Totalkb As Long
Call GetDiskFreespace ("C: /", Sectors, Bytes, Free, Total
FreeKb = bytes * Sectors * free / 1024 'available space, in KB-based
Totalkb = bytes * Sectors * Total / 1024 'total space, in KB-based
Question 15: When the table is subjected to a table, it is desirable to display its illustration to the lower right corner of the work column.
Finally, I will have a lot of trouble, but there are many readers in this question.
Figure -5 reduces the program to the lower right corner, may it?
Basically, there is only one home column on the right side of the "Start" function, and don't think. But why can some programs can be reduced to the lower right corner? In fact, the icon in the lower right corner is not a form or program. For Windows, it is just a picture, and if you want to build this picture, the method is to call the shell_notifyicona API function, as follows:
DIM NID AS Notifyicondata
Call shell_notifyicona (nim_add, nid)
Before calling shell_notifyicona, you must fill in the contents of the Notifyicondata data structure (such as the NID variables), and the Members of NOTIFYICONDATA are as follows:
CBSIZE: The length of the NOTIFYICONDATA data is required. HWND: HANDLE OF WINDOW, for example, set to Form1.hwnd. UID: The user is the ID set. UFLAGS: It is used to set the following parameters (ucallbackmessage, hicon, sztip) is valid, usually set (Nif_Message Nif_icon Nif_tip) represents all valid. UcallbackMessage: When the user presses the mouse on the illustration, Windows will notify the window program, and this parameter is the number of the message. Hicon: illustration. SZTIP: Tips message. There are 7 data members, which seem to be scary, don't worry, these information members are easy to set, first let the author to give a simplest example:
DIM NID AS Notifyicondata
Nid.cbsize = len (NID) 'Take the length of the data structure to CBSIZE
Nid.hwnd = me.hwnd 'hWnd set as a form
Nid.uid = 9999 'Take a number, can be kept
Nid.uflags = nif_icon 'nif_icon represents the setting icon
Nid.hicon = me.hicon 'Set into a form
Call shell_notifyicona (nim_add, nid)
After the results are executed, the ME (current form) will appear in the lower right corner of the work column (hereinafter referred to as the work column), you can directly refer to the examples of the author to see the results of the program. In the above settings, the UID must take a unique number, and in the future, if we want to remove this image from the work column, Windows will be compared to this UID and HWnd, so the method is removed as follows:
DIM NID AS Notifyicondata
Nid.cbsize = len (NID)
Nid.hwnd = me.hwnd
Nid.uid = 9999
Call Shell_Notifyicona (Nim_Delete, NID)
The author's habit is to declare NID into a national variable, so after executing shell_notifyicona (nim_add, nid), the NID data structure saves the value of the UID and HWND, so the next no need to set the NID information member, you can call Shell_Notifyicona directly. NIM_DELETE, NID), the author's model is written.
Set "Tip Message"
Figure -6 Motis moves to the illustration above "Tips Information"
Refer to Figure-6, when we move the mouse to the illustration above, some icons also display "prompt message", what is going on? Direct view:
Nid.cbsize = len (NID)
Nid.hwnd = me.hwnd
Nid.uid = 9998
Nid.uflags = nif_icon nif_tip 'increases the setting of "prompt message"
Nid.hicon = me.icon
Nid.sztip = "Learn VB to find Wang Guoyong" CHR (0)
Call shell_notifyicona (nim_add, nid)
First, the UFLAGS data member should add Nif_tip, and the SZTIP data member sets a prompt message. Please note that this prompt message should be added with CHR (0), otherwise it will prompt the message. Many blank fails. Image modification
In addition to the illustrations available and deleted, the shell_notifyicona function can also modify the icon, for example, the following programs can be changed by the prompt message illustrated by "learning VB to find kingdom" to "Learn Visual Basic Looking for Wang Guoyong":
Nid.hwnd = me.hwnd
Nid.uid = 9998
Nid.uflags = NIF_ICON NIF_TIP
Nid.sztip = "Learn Visual Basic Looking for King Guoyong" CHR (0)
Call shell_notifyicona (Nim_Modify, NID)
Receive Windows messages
Think about what we use in the work column? First, suppose we set the graphic illustration of Form1 to the work column, then you can use the following narrative to make Form1 not displayed in a regular work column:
Form1.hide
Because the working columns in the lower right corner do not take advantage of the space, this is indeed a good performance method for some forms of "Background) working, this is indeed a good expression, with this data machine, a printing machine ... Take this The expression, and interests, the program is also changed by using shell_notifyicona (NIM_MODIFY, NID) when the data machine (or printer) state changes, so that the user feels that the program is executed.
Taking the data machine or a printer as an example, when we press the slice above, they will open the window, how is this? The author said earlier, the graphic image of the work column is just a picture, not a window, so it is unable to receive the message of "Pressing the slice", and actually, when the user presses the mouse when the user is in the work column, receive a message. It is Windows's shell program. In order to make the shell program to transfer messages in the mouse to our window, you must do this when establishing the image:
DIM NID AS Notifyicondata
Nid.cbsize = len (NID)
Nid.hwnd = me.hwnd
Nid.uid = 9997
Nid.uflags = NIF_ICON NIF_TIP NIF_MESSAGE
Nid.hicon = me.hicon
Nid.sztip = "Learn VB to find Wang Guoyong" CHR (0)
Nid.UCALLBACKMESSAGE = message number
Call shell_notifyicona (nim_add, nid)
The main changes are two: (1) UFLAGS data member must increase nif_message (2) Set the message number that you want to transfer to the UcallbackMessage data, such as setting it into 99999, will be used in the future user. When the mouse (including single press and double press), the window will receive a message from 99999.
But the story has not ended yet, received the "Window", not "Event Program", so you want to handle the message from the shell, you must write the window, this is to apply to the previous phase The "Windows Message System" introduced.
Then let the author explain how the window programs handle the message sent from the Shell. For the architectural section, the meaning of several parameters such as the previous phase, and the significance of several parameters such as MSG, WPARAM, and LPARAM is as follows: MSG: will An equal value of Nid.UCallbackMessage data. WPARAM: will be equal to the set value of the NID.UID data member. LPARAM: The message that will be equal to the mole, which is most commonly used is WM_LBUTTONDOWN (press the left button) and WM_LBUTTONDBLCLK (double press the left button).
Therefore, the architecture of the window program is approximately as follows:
Function WndProcforicon (Byval Hwnd As Long, Byval WParam As Long, Byval LParam As long) As long
IF msg = message number THEN
IF lparam = wm_lbuttondown then
... Processing "Pressing the Mouse" message
Elseif Lparam = WM_LButtondblclk Then
... Handling "Double Press Hull Mouse" message
END IF
END IF
WndProcForicon = CallWindowProc (PrevwndProcforicon, HWND, MSG, WPARAM, LPARAM)
END FUNCTION
This operation mode is not difficult, the only thing to pay special attention is "message number" (that is, Nid.UCallbackMessage data member and the MSG parameters of the window), because Windows has defined some messages (such as WM_LButtondown), and these The message has a specific meaning, so we choose the "message number" that we choose to conflict with the Windows defined message, to avoid the message conflict, the program can use the number after WM_USER (= & h400 = 1024), because The message number after WM_USER belongs to Windows Unesets.
This problem is particularly difficult. It is recommended that you read the examples of the author carefully. If you don't know the meaning of it, please turn off the previous "Windows Message System" and review it.
Appendix - Call this article API function to declare
Option expedition
'Declaration of Question 1, 2, and 3
Public const fo_move = & h1
Public const fo_copy = & h2
Public const fo_delete = & h3
Public const fof_noconfirmation = & h10
Public const fof_noconfirmmkdir = & h200
Public const fof_allowundo = & h40
Type Shfileopstruct
HWND As Long
WFUNC AS Long
PFROM as String
PTO As String
Fflags as in
Faborted as boolean
Hnamemaps as long
Sprogress as string
End Type
Declare Function SHFILEOPERATION LIB "shell32.dll" Alias "ShfileOperationa" (LPFileOperationa "(LPFileOperationa" (LPFileOperationa "(LPFILEOPERATIONA"
'Declaration of Question 4
Public const gwl_wndproc = (-4) public const wm_rbuttondown = & H204
Public const wm_rbuttonup = & h205
Declare Function CallWindowProc Lib "User32" Alias "CallWindowProca" (Byval HWND As Long, Byval MSG As Long, Byval WParam As Long, Byval LParam As Long
Declare function getWindowlong lib "user32" alias "getwindowlonga" (Byval Nindex as long) As long
Declare function setwindowlong lib "user32" alias "setwindowlonga" (Byval Nindex as long, Byval Dwnewlong As long) AS Long
Public PrevwndProc As Long
'Declaration of Question 5
Declare function getWindowsDirectory lib "kernel32" Alias "getWindowsDirectorya" (Byval nsize as long) AS Long
'Question 6
Declare function fcreateshelllink lib "vb5stkit.dll" (ByVal shortcutname as string, byval exech AS String, Byval params as string) AS Long
'Question 7
Public const sw_showminimized = 2
Public const sw_showmaximized = 3
Public const sw_show = 5
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'Declaration of Question 8
Public const synchronize = & h100000
Public constinite = & hfffffffffff
Declare function openprocess lib "kernel32" (Byval Binheritra AS Long, Byval DWPROCESSID AS Long) As long
Declare Function CloseHandle Lib "Kernel32" (Byval Hobject As Long) As Long
Declaration of Declare Function WaitforsingleObject Lib "Kernel32" (Byval Hhandle As Long, Byval DwmilliseConds As Long) As long 'problem 9,12
Public const em_getlinecount = & hba
Public const em_linescroll = & hb6
Declare function lib "user32" Alias "SendMessagea" (Byval Hwnd As Long, Byval WParam As Long, LParam as an AS Long
'Declaration of problems 10
Public const drive_removable = 2
Public const drive_fixed = 3
Public const drive_remote = 4
Public const drive_cdrom = 5
Public const drive_ramdisk = 6
Declare function getDriveType lib "kernel32" Alias "getDriveTypea" (Byval NDRIVE AS STRING) AS Long
'Question 11
Public const ofs_maxpathname = 128
Public const of_read = & h0
TYPE OFSTRUCT
Cbytes as Byte
FfixedDisk as byte
NERRCODE AS INTEGER
Reserved1 as integer
Reserved2 as integer
Szpathname (OFS_MAXPATHNAME) AS BYTE
End Type
Type SystemTime
Wyear as integer
WMONTH AS INTEGER
WDAYOFWEEK AS INTEGER
WDAY AS INTEGER
WHOR AS INTEGER
WMINUTE AS INTEGER
WSecond As Integer
Wmilliseconds as integer
End Type
Type filetime
DwlowDatetime As Long
DWHighDatetime As Long
End Type
TYPE BY_HANDLE_FILE_INFORMATION
DWFileAttributes as long
FTCREATIONTIME AS FileTime
FTLASTACCESSTIME AS FileTime
FTLASTWRITIME AS FileTime
DWVolumeSerialNumber As Long
NFILESIGHIGH AS long
NFILESZELOW AS Long
NNumberoflinks as long
NfileIndexhigh As Long
NfileIndexlow As Long
End Type
TYPE TIME_ZONE_INMATION
Bias as long
StandardName (32) AS Integer
StandardDate as systemtime
Standardbias as long
DaylightName (32) AS Integer
DaylightDate as systemtime
Daylightbias as long
End Type
Declare Function GetTimeZoneInformation Lib "kernel32" (lpTimeZoneInformation As TIME_ZONE_INFORMATION) As LongDeclare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Declare Function GetFileinformationByHandle Lib "Kernel32" (LPFILEINFORMATION AS BY_HANDLE_FILE_INFORMATION) AS Long
'Declare Function CloseHandle Lib "Kernel32" (Byval Hobject As Long) AS LONG
Declare Function FiletimetosystemTime LIB "Kernel32" (LPSystemTime As SystemTime) AS Long
'Declaration of Questions 13
Declare Function CreateSolidbrush LIB "GDI32" (Byval Crolor As Long) As Long
DECLARE FUNCTION SELECTOBJECT LIB "GDI32" (Byval HOBJECT AS Long) AS Long
Declare function floodfill lib "gdi32" (Byval X as long, byval y as long, byval crcolor as long) AS Long
'Declaration of question 14
Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
'Declaration of Question 15
Public const nim_add = 0
Public const nim_modify = 1
Public const nim_delete = 2
Public const nif_message = 1
Public const nif_icon = 2
PUBLIC const nif_tip = 4
TYPE NOTIFYICONDATA
CBSIZE AS Long
HWND As Long
Uid as long
Uflags as long
UcallbackMessage As Long
Hicon as long
SZTIP As String * 64
End Type
Public const wm_lbuttondown = & h201
Public const wm_lbuttondblclk = & h203
Public const wm_user = & h400
PUBLIC PrevwndProcforicon As LongDeclare Function Shell_notifyicona Lib "Shell32" (LPData as Notifyicondata) AS INTEGER
'Question 4 Window
Function WndProc (Byval Hwnd As Long, Byval WParam As Long, Byval LParam As long) As long
IF msg = WM_RBUTTONDOWN THEN
'Eat this message
Else
WndProc = CallWindowProc (PrevWndProc, HWND, MSG, WPARAM, LPARAM)
END IF
END FUNCTION
'Question 15 Window program
Function WndProcforicon (Byval Hwnd As Long, Byval WParam As Long, Byval LParam As long) As long
IF msg = wm_user dam
IF lparam = wm_lbuttondown then
MsgBox "Pressing the Mouse", Vbinformation VBSystemModal
Elseif Lparam = WM_LButtondblclk Then
Msgbox "Double Press Hull Mouse", Vbinformation VBSystemModal
END IF
END IF
WndProcForicon = CallWindowProc (PrevwndProcforicon, HWND, MSG, WPARAM, LPARAM)
END FUNCTION