1,可以支持带前台界面,也可以不带前台界面,启动后台程序
2,支持全局单程序检测,任何时候当前机器只有一个进程示例
3,支持注册后台程序服务,开机自动启动后台进程,有待实现
0, winIcontest\Release\winIcontest.exe
1, winIcontest\winIcontest.sln
2, winIcontest\winIcontest\winIcontest.vcxproj
3, winIcontest\winIcontest\ winiconset.ico
4, winIcontest\ winIcontest\winiconset.rc
#include "resource.h" // ID value smaller at first for app icon IDI_SETICON ICON "winiconset.ico" IDI_SMALL ICON "small.ico"
5, winIcontest\winIcontest\resource.h
#define IDI_SETICON 107 #define IDI_SMALL 108
6, winIcontest\winIcontest\winiconset.cpp
#include <Windows.h> #if 1 #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define WINDOW_TITLE L"winIconTestTitle" LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ switch (message) { case WM_PAINT: //ValidateRect(hwnd, NULL); break; case WM_KEYDOWN: if (wParam==VK_ESCAPE){ DestroyWindow(hwnd); } break; //case WM_CREATE: // SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_CAPTION); // SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_DRAWFRAME); // break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ WNDCLASSEX wndClass = { 0 }; wndClass.cbSize = sizeof(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); // LTGRAY_BRUSH wndClass.lpszMenuName = NULL; wndClass.lpszClassName = WINDOW_TITLE; HANDLE mutex = CreateMutexA(NULL, TRUE, "Global/winIconTestTitle"); if ((mutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS)){ MessageBoxA(NULL, "Another is running.\n" ,"Other detected", MB_OK);//MB_OKCANCEL return 0; } if (!RegisterClassEx(&wndClass)) return -1; // WS_OVERLAPPEDWINDOW WS_POPUPWINDOW with black outline ,donot need WM_CREATE case set HWND hwnd = CreateWindow(WINDOW_TITLE, WINDOW_TITLE, WS_POPUPWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); MoveWindow(hwnd, 100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, true); ShowWindow(hwnd, nShowCmd); UpdateWindow(hwnd); MSG msg = { 0 }; while (msg.message != WM_QUIT) { if (PeekMessage( &msg,0,0,0,PM_REMOVE )) { TranslateMessage(&msg); DispatchMessage(&msg); } } UnregisterClass(WINDOW_TITLE, wndClass.hInstance); return 0; } #else //int main(int argc, char* argv[]) int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { //system("python.exe"); WinExec("python.exe", 1);//0 not show 1 show win return 0; } #endif
7,命令行编译
devenv "D:\winIcontest\winIcontest.sln" /Clean "Release|Win32" devenv "D:\winIcontest\winIcontest.sln" /Rebuild "Debug|Win32" devenv "D:\winIcontest\winIcontest.sln" /Rebuild "Release|Win32"