Put minimize icons to the lower right corner of the taskbar

xiaoxiao2021-03-06  43

At present, many Win95 applications are minimized, and the small icons representing the program will hide the upper right corner of the screen (ie the right side of the taskbar), such as "Jinshan Word", "Oriental Express", and we use Visual After the program prepared by Basic, the icon always appears in the center of the taskbar and occupies a large space. How can we make our own levels to the right side of the task bar? We can use the Windows API function to realize this function. First, how to add and hide the icon ---- There is a Windows API function named shell_notifyicona in the dynamic connection library shell32.dll in Windows, which is functioning to operate on the right lower corner icon of the taskbar, including add, delete, and modify . The VB sound format of the shell_notifyicona function is as follows: ---- Declare function shell_notifyicona lib "shell32" (Byval DwMessage As Long, LPDATA AS NOTIFYICONDATA) AS INTEGERSHELL_NOTICONA has two parameters DWMessage and LPDATA. DwMessage is a way to operate the icon, the possible values ​​and meanings are as follows: ---- 0 (add icons to the taskbar) ---- 1 (Modify the icon in the taskbar) ---- 2 (Delete the taskbar Icon) ---- LPDATA is the data of custom data type Notifyicondata, which consists of the following members: ---- 1. CBSIZE: You need to fill in the length of the Notifyicondata data structure, which can pass vb during use. The standard function len is calculated, such as the NID of the Notifyicondata type variable, CBSIZE is LEN (NID). ---- 2. HWND: The handle of the minimization of the window. ---- 3. Uid: The use of the ID settings set to the icon, can be customized.

---- 4. UFLAGS: Is it valid if it is valid after UcallbackMessage, Hicon, Sztip is equal to 1, and the user will send a message when the user is pressed on the icon. Formwork; if HiCon is equal to 2, it is valid, indicating that the icon is to be displayed (if the icon is not displayed, the user only cannot see the icon, but can still operate at the location of the taskbar); equal to 4, for SZTIP is effective, When the user places the mouse pointer on the icon, the prompt information is displayed, otherwise it is not displayed. UFLAGS will be set to 7 (ie 1 2 4), which is fully effective. ---- 5. UcallbackMessage: When the user presses the mouse button on the icon, Windows is sent to the message number of the application program. ---- 6. Hicon: icon handle. ---- 7. SZTIP: When the user puts the mouse pointer on the icon on the icon. ---- Before calling the shell_notifyicona function, you must fill in the correct content of the NOTINDATA type variables. ---- This function is returned to 1 if the function is called, and it returns to 0. ---- After the function of the shell_notifyicona function and the method of use, we can easily add icons to the right side of the taskbar, or delete the icon from the task bar. -, such as: Suppose we declare a NOTINICONDATA type variable NID, and all members who have already given it a suitable value, then the following two statements can complete the task of adding and deleting icons: Shell_Notifyicona 0, NID) 'Add Icon Shell_Notifyicona (2, NID)' Delete Icon, How to Respond to Mouse Buttons ---- Follow the above method, we can put the minimized icon on the right side of the taskbar, then when we are What happens when you press the mouse button on this icon? Pointing, don't have a birth, the minimized window does not look like the restoring state of the rest of our hopes. ---- How is this? Very simple, before us, I just added the icon to the task bar, and there is no work to do anything to the mouse event. It is a matter of response to the mouse button, and I have something to do.

---- As mentioned earlier, if you will set the member uflags of the Notifyicondata type variable to 1, Windows sends a message number UCALLBACKMESSAGE to the application when the user presses the mouse button on the icon. Therefore, In order to respond to the mouse event, we must process the message in the program. ---- What to note is that due to the restrictions of Windows itself, we cannot directly intervene in Windows's own message processing, so only use a "curve saving country" similar to intercepted interrupt from the previous DOS environment. To process the message, the process is: ---- After the form is minimized, read the address of the form origin processing process and record it in a global variable, the method is to call the Windows API function getWindowlong, which is as follows: ---- Declare Function GetWindowlong Lib "User32" Alias ​​"getWindowlonga" (Byval Nindex as long) AS long where hwnd is a form handle, NINDEX is a process type, here is -4 (Windows default). ---- (2) Write your own message processing process, pay attention to the number and type of parameters must follow the format below (process names can be free): ---- Sub WndProcforicon (Byval Hwnd As Long, Byval Msg As Long, ByVal WParam As Long, Byval LParam As Long) ---- (3) Set your message processing process as the default message handler, the method is to call the Windows API function setWindowlong, which is as follows: ---- Declare Function setWindowlong Lib "User32" Alias ​​"Setwindowlonga" (Byval Nindex As Long, Byval Dwnewlong As Long) AS Long ---- HWND and NINDEX meaning, dwnewlong is the address of the custom process, you can use VB's request address Operator Addressof is obtained, such as Addressof WndProcforicon to get the address of the custom process WNDPROCFORICON. ---- (4) After the mouse incident, the self-employment process is processed (now in a default message processing process) first proceeds, and the popup menu is returned or popped out for a Popup menu.

---- (5) Let Windows perform the original message processing process (save the address of the process by global variable), the method is to call the Windows API function CallWindowProc, which is as follows: ---- Declare Function CallWindowProc lib "user32" alias "CallWindowProca" (Byval HWND As Long, Byval Hwnd As Long, Byval Msg As Long, Byval WParam As long, Byval LPARM as long) AS long ---- where lpprevwndfunc is the address of the original message handler, other parameters and customizes The parameters during the message processing correspond. ---- Careful readers will definitely find that if the step 5 is not performed after the third step is completed, ie: Use setWindowlong to change the address of the original message handler, but not set the address before the program That program will not end properly, often cause crash, so it must be carefully handled this problem. ---- Another addition, for the correct processing, there is a question to solve, is what is the message is issued by the icon? This can be resolved: Use a global variable to record the value of the member UcallbackMessage (message number) of the Notifyicondata type variable, and then check if the message number (MSGNO) returned by Windows is consistent with the message handler, if consistent, then The message is indeed sent by the icon, should be processed, otherwise it can be ignored. As described in this paper, the full-local variable Iconmsg is used to realize this capability (please refer to the program code). Third, the program example ---- Let's implement the above method through a simple example: ---- 1, Form Layout ---- Create a new project icontest, create a new standard form frMicontest, on the form Place three command buttons, name (name) is Cmdcreateicon, cmdnormalmin, and cmdexit, the title (minimizing to the lower right corner "," ordinary minimization "and" exit ".

---- 2, code ---- Add a module modicontest, write the following code: Option explicit 'Define constant public const gwl_wndproc = (-4) public const wm_lbuttondown = & h201 public const nim_add = 0 public const nim_delete = 2 Public Const NIF_MESSAGE = 1 Public Const NIF_ICON = 2 Public Const NIF_TIP = 4 'define global variables Public IconMsg As Long' 'address of the original message handler' message number Public OldWinProc As Long define a custom data type Public 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 'Windows API function declaration 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 GetWindowLong Lib "user32" Alias ​​"GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex 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 fu nction Shell_NotifyIconA Lib "SHELL32" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Integer 'custom message handler Sub MyProc (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long)' according Msg Judgment, whether the message is emitted, if yes, proceed to process if msg = iconmsg dam = wm_lbuttondtondown the frMicontest.show 'If you press the left button on the icon, display the form end if Endiff' to perform full variables The message handler in the address of the original message handler recorded by OldwinProc CallWindowProc OldwinProc, HWND, MSG, WPARAM, LPAR END SUB are written in the FRMICONTEST form:

Option Explicit Dim HadAdd As Boolean Dim nid As NOTIFYICONDATA Private Sub cmdEnd_Click () If HadAdd Then 'If you had had the lower right corner to add a small icon' is based on pre-recorded OldWinProc restore the original message handler, and delete icon SetWindowLong frmIconTest.hWnd, GWL_WNDPROC, OldWinProc Call Shell_NotifyIconA (NIM_DELETE, nid) 'delete icon End If unload Me' unload the form End Sub Private Sub cmdCreateIcon_Click () If not HadAdd then 'If you do not produce too icon, add an icon to the lower right corner With nid' fill in the following All members of the NID variable.cbsize = len (nid) 'fills in the length of the custom data type. HWND = frMicontest.hwnd' Fill in the form of the form. UID = 9999 'icon ID, can be free to display the icon, prompt and Generate a message .uflags = nif_icon nif_tip nif_Message .hicon = frMicontest.icon.handle 'icon handle.sztip = "Click the left mouse button to recover the form!"' Prompt information. MuCallbackMessage = 2 'icon Message number end with shell_notifyicona NIM_ADD, NID 'Add Icon Iconmsg = Nid.UCallbackMessage' Use the Global Variable Record Message Number, 'Read the address of the FRMICONTEST Normal Message Handler Save OldwinProc = getWindowlong (frMicontest.hwnd, gwl_wndproc) in the full variable OldwinProc Process MyProc address instead of the normal message handler's address setWindowlong frMicontest.hWnd, GWL_WndProc, addressof myproc fmicontest.hide 'hidden form Hadadd = true' set "Already added icon" flag Else frMicontest.hide ' There is already a small icon, then directly hide the form end if end sub private subdnormalmin_click () frmicontest.windowstate = 1 'normal Minimize Form End Sub PRIVATE SUB FORM_LOAD () Hadadd = false' will "add icons to the icon "Set to false 'is the form load icon, the reader should define frMicontest.icon = OAD SUB according to its own VB directory (" c: /myicon/files10.ico ") End Sub ---- Press F5, execute this The program, click the "Minimize to the lower right" button, the form will disappear, and the icon representing the form will appear in the right side of the taskbar (as shown in Figure 2), and the left mouse button is clicked on the icon, the form is constant.

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

New Post(0)