Use the API to close the window in VB

zhaozj2021-02-17  59

During the VB programming process, sometimes you need to terminate some programs through program control. You can use some methods in practice:

1. Use SendMessage

Private Declare Function SendMessage LIB "User32" Alias ​​"SendMessagea" (Byval Hwnd As Long, Byval WParam As Long, LParam As Any) As long

HWnd is a window handle of a window program that needs to be closed;

The message sent can be used in two options 1, WMSG is WM_CLOSE, WPARAM, LPARAM is 0; 2, WMSG is WM_SYSCOMMAND, WPARAM is CS_CLOSE, LPARAM is 0.

2. Use TerminateProcess

Private Declare Function TerminateProcess LIB "kernel32" Alias ​​"TerminateProcess" (Byval UExitcode As long) AS Long

HProcess is a handle of a process to be interrupted;

A exit code of the UEXITCODE process

3. Use Closewindow

Private Declare Function CloseWindow Lib "User32" Alias ​​"Closewindow" (Byval Hwnd As Long) AS Long

HWnd is a handle that wants to close the window.

4. If you want to close all windows, you can use BroadcastsystemMessage.

Private Declare Function BroadcastsystemMessage Lib "User32" Alias ​​"BroadcastsystemMessage" (Byval DW As Long, Byval WParam As Long, BYVAL LPARAM As Long)

DW is one or more of the following constants; BSF_FLUSHDisk After each message is processed, the disk is refreshed with the disk (the data of the unmarked disk, BSF_FORCEIFHUGG Hung, if the target is suspended, then expires after the set timeout Return to BSF_IGNORECURRENTSK Send Task Does Not Access Message BSF_LParambuffer

PDW is one or more of the following constants; the BSF_allcomponents message enters each system component BSF_Applications message to receive messages to the application BSF_INSTALLABLERS message reaches the installed driver BSF_NETDRIVERS message reaches the network driver BSF_VXDS message to the system device driver

UN is the message number;

The message is the same as using SendMessage.

5. Close all programs using EXITWINDOWSEX

Private Declare Function ExitWindowsex lib "user32" Alias ​​"EXITWINDOWSEX" (Byval dwreserved as long) AS Long

Set UFlags to EWX_FORCE forced to have no response processes.

dwreserved is 0

note! The program cannot be closed due to the following reasons.

1. In the NT / 2000 / XP system, the process handle needs to be accessed by Process_Terminate to be killed. 2. If the process to be closed is in the kernal-mode, the TerminateProcess function does not work.

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

New Post(0)