📄 singleinstance.cpp
字号:
#include <windows.h>
#include "CMcl.h"
#define APPLICATION_WINDOW_NAME "Single Instance Application"
#define APPLICATION_CLASS_NAME "SingleInstanceAppWindowClass"
#define APPLICATION_MUTEX_NAME "SingleInstanceAppMutex"
LRESULT CALLBACK WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc( hwnd, msg, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) {
///////////////////////////////////////////
// single instance synchronization stuff...
///////////////////////////////////////////
CMclMutex cmSingleInstance( TRUE, APPLICATION_MUTEX_NAME);
if (cmSingleInstance.Status() == ERROR_ALREADY_EXISTS) {
// we are not the first running instance...
// wait for the first instance to have finished
// creating it's window...
cmSingleInstance.Wait(INFINITE);
// we can release the mutex now...
cmSingleInstance.Release();
// bring the window to the top...
HWND hwnd = FindWindow( APPLICATION_CLASS_NAME, APPLICATION_WINDOW_NAME);
if (hwnd) {
SetForegroundWindow(hwnd);
}
return 0;
}
//////////////////////////////////
// bare-bones windows app stuff...
//////////////////////////////////
// create a window class..
WNDCLASS wcl;
wcl.hInstance = hThisInst;
wcl.lpszClassName = APPLICATION_CLASS_NAME;
wcl.lpfnWndProc = WindowProc;
wcl.style = 0;
wcl.hIcon = LoadIcon( NULL, IDI_APPLICATION);
wcl.hCursor = LoadCursor( NULL, IDC_ARROW);
wcl.lpszMenuName = NULL;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hbrBackground = GetStockObject(GRAY_BRUSH);
// register the window class..
if (RegisterClass(&wcl) == FALSE) {
// fatal error, unable to register window class...
return 0;
}
// create the main window...
HWND hwnd = CreateWindow( APPLICATION_CLASS_NAME,
APPLICATION_WINDOW_NAME,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_DESKTOP,
NULL,
hThisInst,
NULL);
// show the window...
ShowWindow(hwnd, nWinMode);
UpdateWindow(hwnd);
// we are the first running instance, release the mutex...
cmSingleInstance.Release();
// message loop...
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// exit..
return msg.wParam;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -