Use Windows API to obtain advanced system functions

Source: Internet
Author: User
Use Windows API to obtain advanced system functions

Xiaogang

VB is undoubtedly one of the most advanced programming tools, but it still seems powerless in terms of core programming involving windows 32-bit systems-for example, the implementation of some advanced functions, this requires us to make full use of VB's powerful Windows API function calling capabilities to compensate. The following code shows you how to use the Windows API to control the system tray (icon) area, Start Menu, taskbar, and advanced system functions such as disabling/starting Ctrl-alt-Del, logging out of the network, shutting down immediately, and restarting.

Start VB6, create a standard EXE project, and add 14 command buttons, the caption attributes are "Hide Start Menu", "Show Start Menu", "Hide System Tray", "Show System Tray", "Disable Ctrl-alt-del", and "Start CTRL ". -alt-Del, hide the taskbar, display the taskbar, restart now, shut down, log out of the network, and display the shutdown dialog box ", adjust the above control to the appropriate position, double-click the form, and write the following code:

Option explicit

Private declare function findwindow lib "USER32" alias "find0000wa" (byval lpclassname as string, byval lpwindowname as string) as long

'Look for the first top-level window in the window list that meets the specified conditions
'Lpclassname refers to the pointer to the null (c) string containing the window class name; or set to zero, 'indicates receiving any class
'Lpwindowname refers to the pointer to the air (c) string containing the window text (or tag); 'or set to zero, it indicates receiving any window title

Private declare function showwindow lib "USER32" (byval hwnd as long, byval ncmdshow as long) as long

'Control window visibility
'Hwnd window handle. You need to apply the command specified by ncmdshow to this window.
'Ncmdshow is a command for specifying the visibility of a window

Private declare function find1_wex lib "USER32" alias "find1_wexa" (byval hwnd1 as long, byval hwnd2 as long, byval lpsz1 as string, byval lpsz2 as string) as long

'Look for the first subwindow that matches the specified condition in the window list.
'Hwnd1: Find the parent window of the child.
'Hwnd2 start searching in this window. In this way, you can find all the child windows that meet the conditions by calling find1_wex multiple times. If it is set to zero, the search starts from the first subwindow.

Private declare function exitwindowsex lib "USER32" (byval uflags as long, byval dwreserved as long)

'Exit windows and restart with specific options

Private declare function systemparametersinfo lib "USER32" alias "systemparametersinfoa" (byval uaction as long, byval uparam as long, byref lpvparam as any, byval fuwinini as long) as long

'Allow obtaining and setting a large number of Windows System Parameters
'Uaction specifies the parameter to be set

Private const ewx_logoff = 0' stop the process and then log out
Private const ewx_shutdown = 1' turn off the system power
Private const ewx_reboot = 2' reboot the system
Private const ewx_force = 4' force stop processes with No Response
Private const spi_screensaverrunning = 97

Private sub commandemediclick () 'hide the Start Menu
Dim handle as long, findclass as long
Findclass = findwindow ("shell_traywnd ","")
Handle = findwindowex (findclass, 0, "button", vbnullstring)
Showwindow handle, 0
End sub

Private sub command11_click () ': Ctrl-alt-del
Dim RET as integer
Dim pold as Boolean
Ret = systemparametersinfo (spi_screensaverrunning, true, pold, 0)
End sub

Private sub command12_click () 'Disable Ctrl-alt-del
Dim RET as integer
Dim pold as Boolean
Ret = systemparametersinfo (spi_screensaverrunning, false, pold, 0)
End sub

Private sub command13_click () 'restart now
Exitwindowsex ewx_reboot, 0
End sub

Private sub command14_click () 'shutdown
Exitwindowsex ewx_shutdown, 0
End sub

Private sub command15_click () 'log out of the Network
Exitwindowsex ewx_logoff, 0
End sub

Private sub command16_click () ': Display shutdown dialog box
Exitwindowsex ewx_force, 0
End sub

Private sub command2_click () 'show Start Menu
Dim handle as long, findclass as long
Findclass = findwindow ("shell_traywnd ","")
Handle = findwindowex (findclass, 0, "button", vbnullstring)
Showwindow handle, 1
End sub

Private sub command5_click () 'hide the system tray
Dim findclass as long, handle as long
Findclass = findwindow ("shell_traywnd ","")
Handle = findwindowex (findclass, 0, "traypolicywnd", vbnullstring)
Showwindow handle, 0
End sub

Private sub command6_click () 'displays the system tray
Dim findclass as long, handle as long
Findclass = findwindow ("shell_traywnd ","")
Handle = findwindowex (findclass, 0, "traypolicywnd", vbnullstring)
Showwindow handle, 1
End sub

Private sub command7_click () ': display the taskbar
Dim findclass as long, findclass2 as long, parent as long, handle as long
Findclass = findwindow ("shell_traywnd ","")
Findclass2 = findjavaswex (findclass, 0, "rebarwindow32", vbnullstring)
Parent = findjavaswex (findclass2, 0, "mstaskswwclass", vbnullstring)
Handle = find1_wex (parent, 0, "systabcontrol32", vbnullstring)
Showwindow handle, 0
End sub

Private sub command8_click () 'hide the taskbar
Dim findclass as long, findclass2 as long, parent as long, handle as long
Findclass = findwindow ("shell_traywnd ","")
Findclass2 = findjavaswex (findclass, 0, "rebarwindow32", vbnullstring)
Parent = findjavaswex (findclass2, 0, "mstaskswwclass", vbnullstring)
Handle = find1_wex (parent, 0, "systabcontrol32", vbnullstring)
Showwindow handle, 1
End sub

Private sub form_queryunload (cancel as integer, unloadmode as integer)
End
End sub

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.