Unique instances of Win32 applications are implemented via the SDK function.

zhaozj2021-02-17  52

//FormMain.cpp

#include #include "exemutex.h"

Static lpcstr szappname = text ("hellowin");

Lresult Callback WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {if {return FALSE (OperateMutex (szAppName, szAppName, szAppName, OPEN)!);} HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); wndclass .hbrbackground = (hbrush) getStockObject (White_brush); WNDCLASS.LPSZMENUNAME = NULL; WNDCLASS.LPSZCLASSNAME = Szappname;

RegisterClass (& wndclass); hwnd = CreateWindow (szAppName, // window class name szAppName, // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT , // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); while (GetMessage ( & msg, NULL, 0, 0) {TranslateMessage (& MSG); DispatchMessage (& msg);} return 0;}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_DESTROY: OperateMutex (szAppName, szAppName, szAppName, CLOSE); PostQuitMessage (0); return 0;} return DefWindowProc (hwnd, Message, wparam, lparam;} // header file

/ * Define constants for judging the type of operation * / const bool open = 1; const bool close = 0;

// Function declares BOOL OperateMuteX (LPCSTR, LPCTSTR, LPCTSTSTR, BOOL); Bool ActiveOLDAPP (LPCSTR, LPCSTR); / / Determine whether or not the current application running instance is the function body BOOL OPERATEMUTEX (LPCSTANCENAME, / / ​​Custom) LPCSTR hWndClassName unique identifying name of the application, class name registration // CreateWindow function of the form specified LPCSTR hWndWinName, // name of the form specified in the function CreateWindow BOOL mOperate) // operator whether the variable {HANDLE hMutex; hMutex = OpenMutex (MUTEX_ALL_ACCESS , False, InstanceName); Switch (MOPERATE) {copy open: // open = launchler if (hmutex == null) {hmutex = cretemutex (null, false, instancename); if ((hmutex == null) | GetLastError () == ERROR_ALREADY_EXISTS)) {ActiveOldApp (hWndClassName, hWndWinName); return FALSE; // procedure has been started or an error has occurred} else {return TRUE;} // first start program} else {ActiveOldApp (hWndClassName, hWndWinName ); Return false; // The program has been started} Break; Case Close: // Close = Exit Procedure ReleaseMutex (HMutex); CloseHandle (hmutex); Break;} Return True;}

// Activate started program BOOL ActiveOldApp (LPCSTR hWndClassName, registered class name // CreateWindow function of the form specified LPCSTR hWndWinName) // name of the form specified in the function CreateWindow {HWND hWndApp, hWndPopup; if (hWndApp = FindWindow (hWndClassName , hWndWinName)) {hWndPopup = GetLastActivePopup (hWndApp); BringWindowToTop (hWndApp); if (IsIconic (hWndPopup)) {ShowWindow (hWndPopup, SW_RESTORE);} else {SetForegroundWindow (hWndPopup);}} else {return FALSE;} return TRUE }

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

New Post(0)