[Win32] The method of automatically closing dialog boxes through multiple threads

tags: Win32  c++  windows

Zero, demand

  • I hope that a dialog box will pop up automatically after Windows is turned on, and it will automatically close after 8 seconds.

One, solve

1. Build the project and source files

  • Create a new Win32 project in VC6.0
     Win32 1
     Win32 2

  • New C++ source file
     C++

2. Write the code

  • basic structure
#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
{ 
    return 0; 
}
  • Add popup
#include <windows.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
{ 
    if(IDCANCEL == MessageBox(NULL, TEXT ("Pop-up message"), TEXT ("Pop 1"), MB_OKCANCEL))
    {
          //If you click cancel
          MessageBox (NULL, TEXT ("Pop-up message"), TEXT ("Pop 2"), MB_OK);
    }
    return 0; 
}
  • Add multithreading
#include <windows.h>

// thread running function
DWORD WINAPI CloseDialog(LPVOID param)
{
    ExitThread(0); 
    return 0;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
{ 
    DWORD parameter = 0;
    DWORD id;
    //Create thread
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CloseDialog, &parameter, 0, &id);

    if(IDCANCEL == MessageBox(NULL, TEXT ("Pop-up message"), TEXT ("Pop 1"), MB_OKCANCEL))
    {
          //If you click cancel
          MessageBox (NULL, TEXT ("Pop-up message"), TEXT ("Pop 2"), MB_OK);
    }
    return 0; 
}
  • Perfect close function
#include <windows.h>

static TCHAR title[] = TEXT ("Pop 1");

// thread running function
DWORD WINAPI CloseDialog(LPVOID param)
{
    //Sleep for 8000ms
    Sleep(8000);
    HANDLE hWnd = ::FindWindowEx(NULL, NULL, NULL,title);
    if (hWnd)
    {
          //Close the dialog
          ::EndDialog((HWND)hWnd, IDNO);
    }
    ExitThread(0); 
    return 0;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 
{ 
    DWORD parameter = 0;
    DWORD id;
    //Create thread
    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CloseDialog, &parameter, 0, &id);

    if(IDCANCEL == MessageBox(NULL, TEXT ("Close in 8 seconds"), title, MB_OKCANCEL))
    {
          //If you click cancel
          MessageBox (NULL, TEXT ("Won't close"), TEXT ("Pop 2"), MB_OK);
    }
    return 0; 
}

3. Effect

  • window

    will automatically close after 8 seconds of doing nothing, click confirm and close immediately, click cancel and close immediately and pop up window 2, which will not close automatically.

Two, summary

The following content is collected from the Internet.

1.CreateThread

To spawn a thread (and thus achieve a multi-threaded program), with CreateThread () as the beginning of all actions.

prototype

HANDLE CreateThread(
 LPSECURITY_ATTRIBUTES lpThreadAttributes, 
 SIZE_T dwStackSize, 
 LPTHREAD_START_ROUTINE lpStartAddress, 
 LPVOID lpParameter, 
 DWORD dwCreationFlags, 
 LPDWORD lpThreadId 
);

parameter

lpThreadAttributes: Describe the security attributes applied to this new thread. NULL means use the default value.

dwStatckSize: The new thread has its own stack, 0 means to use the default size.

lpStartAddress: The starting address where the new thread will start. This is a function pointer (in the C language, the function name represents the function pointer).

lpParameter: This value will be passed to the new thread function specified above as a parameter (the only parameter).

dwCreationFlags: Allows you to spawn a temporarily suspended thread, the default is "start execution immediately".

lpThreadId: The ID of the new thread will be passed back here.

return value

If CreateThread() succeeds, it returns a handle representing the new thread, otherwise it returns FALSE. If it fails, you can call GetLastError() to find out the reason.

2.MessageBox

A dialog box pops up.

prototype

int MessageBox(          
    HWND hWnd,
    LPCTSTR lpText,
    LPCTSTR lpCaption,
    UINT uType
);

parameter

hWnd
handles the owner window of the message box to be created. If this parameter is empty, the message box has no owner window.
lpText
Pointer to a null-terminated string containing the message to be displayed
lpCaption
A pointer to a null-terminated string containing the title of the dialog box. If this parameter is empty, the default title Error is used.
uType
Specify the content and behavior of the dialog box. This parameter can be a combination of the following sets of flags. Point out the button displayed in the message box.

Button

MB_OK
The default value. There is a confirmation button inside.
MB_YESNO
has yes and no in it.
MB_ABORTRETRYIGNORE
There are Abort, Retry and Ignore
MB_YESNOCANCEL
The message box contains three buttons: Yes, No and Cancel
MB_RETRYCANCEL
Retry (retry) and Cancel (cancel)
MB_OKCANCEL
The message box contains two buttons: OK and Cancel

icon

X error MB_ICONHAND, MB_ICONSTOP, MB_ICONERROR
? Ask MB_ICONQUESTION
! Warning MB_ICONEXCLAMATION, MB_ICONWARNING
i information MB_ICONASTERISK, MB_ICONINFORMATION
The above things can be seen in msdn.

return value

If a message box has a cancel button, then if the ESC key is pressed or the cancel button is cancelled, the function will return the IDCANCEL value.
If the message box does not have a cancel button, pressing the ESC key has no effect.
If the function fails, the return value is 0.
If the function succeeds, the return value is one of the values ​​of the following menu items.
The IDABORT Abort button is selected.
IDCANCEL Cancel button is selected.
IDCONTINUE Continue button is selected.
The IDIGNOR EIgnore button is selected.
IDNO No button is selected.
IDOK OK button is selected.
The IDRETRY Retry button is selected.
IDYES Yes button is selected.

3.FindWindowEx

Find the first child window that matches the specified conditions in the window list.
This function obtains the handle of a window whose class name and window name match the given string. This function finds the child window, starting from the next child window behind the given child window. The search is not case sensitive.

prototype

HWND FindWindowEx(
HWND hwndParent, 
HWND hwndChildAfter,
LPCTSTR lpszClass,
LPCTSTR lpszWindow
);

parameter

hwndParent: The handle of the parent window where the child window to be found is located (if hwndParent is set, it means that the child window is searched from the parent window pointed to by this hwndParent).
If hwndParent is 0, the function uses the desktop window as the parent window to find all child windows of the desktop window.
Windows NT5.0 and later: If hwndParent is HWND_MESSAGE, the function only finds all message windows.

hwndChildAfter: The handle of the child window. The search starts from the next child window in the Z sequence. The child window must be a direct child window of the hwndParent window and not a descendant window. If HwndChildAfter is NULL, the search starts from the first child window of hwndParent. If hwndParent and hwndChildAfter are both NULL, the function finds all top-level windows and message windows.

lpszClass: Pointer to a null-terminated string specifying the class name, or a pointer to a member of the class name string. If the parameter is a member, it must be the global member generated by the previous call to theGlobaIAddAtom function. This member is 16 bits and must be located in the low 16 bits of lpClassName, and the high bit must be 0.

lpszWindow: points to a null-terminated string that specifies the window name (window title). If this parameter is NULL, all windows are matched.

return value

Long, the handle of the window found. If no matching window is found, zero is returned. Will set GetLastError
If the function succeeds, the return value is the window handle with the specified class name and window name. If the function fails, the return value is NULL.
If you want to get more error information, please call GetLastError function.

4.EndDialog

This function clears a modal dialog box and causes the system to suspend any processing of the dialog box.

prototype

BOOL EndDialog(HWND hDlg,int nResult);

parameter

hDlg: Indicates the dialog window to be cleared.

NResult: Specify the value returned to the application from the dialog box creation function.

return value

If the function call succeeds, the return value is non-zero; if the function call fails, the return value is zero. If you want to get error information, please call GetLastError function.

Intelligent Recommendation

The third method is to create multiple threads

Callable interface and the use FutureTask class is created, and the difference between using the Thread class and Runnable interface before is that this method returns the return value of the thread e...

UpdateData method using multiple threads

1, define the message 2, an announcement message   4, adding the message map 3, implement the message response function 4, sends a message in the thread   Reproduced in: https: //www.cnblogs...

How to ensure the same size of multiple dialog boxes when adding dialog resources in MFC

Recently, I received a job, imitating an interface to make a calculation book function. The fake interface is as follows The left is the tree control, and the right is the sub-window interface. When t...

Solve the multiple nesting of el-dialog bullet boxes and realize the best el-dialog

Solve the problem of multiple nesting of el-dialog bullet boxes. Realize the best el-dialog With the widespread use of vueJs, Element components are gradually gaining popularity. But when using elemen...

More Recommendation

Jquery automatically selects multiple check boxes when loading the page

Use jquery to automatically select the multi-select box on the page when the page loads When entering the page, the check box is automatically selected Code demo: The important one is the code: $(&quo...

Embedded system development notes 51: Solve the problem of "you must close all dialog boxes before closing computer management"

Article directory foreword 1. Causes 2. Experiment foreword When closing the "Computer Management" application, the problem "You must close all dialog boxes before closing Computer Mana...

Dialog boxes, and custom dialog box

Dialog with custom dialog box Dialog Ordinary dialog Radio dialog Checkbox Customize dialog box Horizontal progress bar dialog Round fuzzy progress dialog Date selection dialog Time selection dialog D...

Dialog eight basic dialog boxes

1. Simple introductory dialog activity_main.xml MainActivity.java Two, list dialog box and single selection dialog box activity_main.xml MainActivity.java Three, multiple selection dialog box and wait...

Dialog eight kinds of dialog boxes

Dialog dialog 1. Common dialog 1. Common dialog 2. Single selection dialog 3. Multiple selection dialog 4. Customize the dialog 5. Horizontal progress bar dialog 6. Circular progress bar dialog 7. Dat...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top