Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //How to use :
- //warningCheck.exe arg1 arg2 arg3
- //arg1 window's title
- //arg2 message
- //arg3 marker's path
- //Exemple :
- //warningCheck.exe "WSL feature was installed and needs reboot to complete" "Restart now?" "C:\Windows\Temp\warningCheckMarker"
- #include <windows.h>
- #include <string>
- #include "Shlwapi.h"
- #include "resource.h"
- #pragma comment(lib, "Shlwapi.lib")
- #define ID_CAPTIONSTATICBLANK 1
- #define ID_CAPTIONSTATIC 2
- #define ID_YESBUTTON 11
- #define ID_NOBUTTON 12
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- HANDLE hwndYesButton;
- HANDLE hwndNoButton;
- HANDLE hwndCaptionStaticBlank;
- HANDLE hwndCaptionStatic;
- HWND mainWindow;
- std::wstring windowsTitle;
- std::wstring captionText;
- std::wstring markerPath;
- int xSize, ySize;
- std::wstring yesText;
- std::wstring noText;
- LPWSTR stringToLPWSTR(const std::string& instr);
- int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
- {
- LANGID language = GetUserDefaultUILanguage();
- if (language == 1036)
- {
- yesText = L"Oui";
- noText = L"Non";
- }
- else
- {
- yesText = L"Yes";
- noText = L"No";
- }
- RECT desktop;
- const HWND hDesktop = GetDesktopWindow();
- GetWindowRect(hDesktop, &desktop);
- xSize = desktop.right;
- ySize = desktop.bottom;
- LPWSTR *szArgList;
- int argCount;
- szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
- if (szArgList == NULL)
- {
- MessageBox(NULL, L"Unable to parse command line", L"Error", MB_OK | MB_ICONEXCLAMATION);
- return 10;
- }
- if (szArgList[1] != NULL)
- {
- windowsTitle = szArgList[1];
- }
- else
- {
- windowsTitle = L"windowsTitle";
- }
- if (szArgList[2] != NULL && argCount >= 3)
- {
- captionText = szArgList[2];
- }
- else
- {
- captionText = L"captionText";
- }
- if (szArgList[3] != NULL && argCount >= 4)
- {
- markerPath = szArgList[3];
- }
- else
- {
- markerPath = stringToLPWSTR(getenv("TEMP"));
- markerPath.append(L"\\warningCheckMarker");
- }
- LocalFree(szArgList);
- MSG msg;
- WNDCLASSW wc = { 0 };
- wc.lpszClassName = L"cdsMsg";
- wc.hInstance = hInstance;
- wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- wc.lpfnWndProc = WndProc;
- wc.hCursor = LoadCursor(0, IDC_ARROW);
- wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
- RegisterClassW(&wc);
- mainWindow = CreateWindowW(wc.lpszClassName, windowsTitle.c_str(), WS_CAPTION | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPED | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, xSize - 400, ySize - 200, 400, 126, 0, 0, hInstance, 0);
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg)
- {
- case WM_CREATE:
- hwndCaptionStatic = CreateWindowEx(0, L"STATIC", captionText.c_str(), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, 8, 12, 400, 32, hwnd, (HMENU)ID_CAPTIONSTATIC, GetModuleHandle(NULL), 0);
- hwndCaptionStaticBlank = CreateWindowEx(0, L"STATIC", L"", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,0, 0, 400, 42, hwnd, (HMENU)ID_CAPTIONSTATICBLANK, GetModuleHandle(NULL), 0);
- hwndYesButton = CreateWindowW(L"Button", yesText.c_str(), WS_VISIBLE | WS_CHILD, 205, 55, 61, 21, hwnd, (HMENU)ID_YESBUTTON, NULL, NULL);
- hwndNoButton = CreateWindowW(L"Button", noText.c_str(), WS_VISIBLE | WS_CHILD, 300, 55, 61, 21, hwnd, (HMENU)ID_NOBUTTON, NULL, NULL);
- break;
- case WM_COMMAND:
- switch(LOWORD(wParam))
- {
- case ID_YESBUTTON:
- {
- DWORD fileAttr = GetFileAttributes(markerPath.c_str());
- if (0xFFFFFFFF == fileAttr)
- {
- HANDLE h = CreateFile(markerPath.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (!h)
- {
- MessageBox(NULL, L"Couldn't create marker", L"Error", MB_OK | MB_ICONEXCLAMATION);
- }
- }
- else if (PathFileExistsW(markerPath.c_str()))
- {
- MessageBox(NULL, L"Marker already exists", L"", MB_OK | MB_ICONEXCLAMATION);
- }
- else
- {
- MessageBox(NULL, L"Couldn't create marker", L"Error", MB_OK | MB_ICONEXCLAMATION);
- }
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- break;
- }
- case ID_NOBUTTON:
- SendMessage(hwnd, WM_CLOSE, 0, 0);
- break;
- default:
- break;
- }
- break;
- case WM_CTLCOLORSTATIC:
- SetBkColor((HDC)wParam, RGB(255, 255, 255));
- return (INT_PTR)CreateSolidBrush(RGB(255, 255, 255));
- break;
- case WM_SYSCOMMAND:
- {
- int command = wParam & 0xfff0;
- if (command == SC_MOVE)
- return NULL;
- break;
- }
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- LPWSTR stringToLPWSTR(const std::string& instr)
- {
- int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
- LPWSTR widestr = new WCHAR[bufferlen + 1];
- ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
- widestr[bufferlen] = 0;
- return widestr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement