Advertisement
Combreal

loadingBar.Cpp

Jun 20th, 2020
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. #include <windows.h>
  2. #include <commctrl.h>
  3. #include <string>
  4. #include "resource.h"
  5.  
  6. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  7. HANDLE hwndButton;
  8. HWND hWndProgressBar;
  9.  
  10. void CenterWindow(HWND);
  11. LPWSTR stringToLPWSTR(const std::string& instr);
  12.  
  13. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  14. {
  15.     MSG msg;
  16.     WNDCLASSW wc = { 0 };
  17.     wc.lpszClassName = L"Akakyuri";
  18.     wc.hInstance = hInstance;
  19.     wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  20.     wc.lpfnWndProc = WndProc;
  21.     wc.hCursor = LoadCursor(0, IDC_ARROW);
  22.     RegisterClassW(&wc);
  23.     CreateWindowW(wc.lpszClassName, L"Akakyuri", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 260, 134, 288, 200, 0, 0, hInstance, 0);
  24.     while (GetMessage(&msg, NULL, 0, 0))
  25.     {
  26.         TranslateMessage(&msg);
  27.         DispatchMessage(&msg);
  28.     }
  29.     return (int)msg.wParam;
  30. }
  31.  
  32. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  33. {
  34.     switch (msg)
  35.     {
  36.     case WM_KEYDOWN:
  37.         if (wParam == VK_ESCAPE)
  38.         {
  39.             int ret = MessageBoxW(hwnd, L"Woot?",
  40.                 L"Message", MB_OKCANCEL);
  41.             if (ret == IDOK)
  42.             {
  43.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  44.             }
  45.         }
  46.         break;
  47.     case WM_CREATE:
  48.         CenterWindow(hwnd);
  49.         hwndButton = CreateWindowW(L"Button", L"Click", WS_VISIBLE | WS_CHILD, 50, 9, 61, 21, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  50.         //hWndProgressBar = CreateWindowW(NULL, PROGRESS_CLASS, WS_VISIBLE | WS_CHILD, 50, 9, 61, 21, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  51.         hWndProgressBar = CreateWindowEx(0, PROGRESS_CLASS, (LPSTR)NULL, WS_VISIBLE | WS_CHILD, 10, 50, 200, 20, hwnd, (HMENU)IDPB_PROGRESS_BAR, (HINSTANCE)SetWindowLongPtr(hwnd, GWLP_HINSTANCE, DWLP_DLGPROC), NULL);
  52.         //hWndProgressBar = CreateWindowW(PROGRESS_CLASS, (LPSTR)NULL, WS_VISIBLE | WS_CHILD, 10, 50, 200, 20, hwnd, (HMENU)IDPB_PROGRESS_BAR, (HINSTANCE)SetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  53.         if (!hWndProgressBar)
  54.         {
  55.             MessageBox(NULL, "Progress Bar Failed.", "Error", MB_OK | MB_ICONERROR);
  56.             SendMessage(hwnd, PBM_SETRANGE, 0, MAKELPARAM(0, 9));
  57.             SendMessage(hwnd, PBM_SETSTEP, (WPARAM)1, 0);
  58.         }
  59.         break;
  60.     case WM_TIMER:
  61.     {
  62.         switch ((UINT)wParam)
  63.         {
  64.         case IDT_TIMER:
  65.         {
  66.             KillTimer(hwnd, IDT_TIMER);
  67.             KillTimer(hwnd, IDT_PROGRESS_TIMER);
  68.             MessageBox(NULL, "Timer Activated", "Success", MB_OK | MB_ICONINFORMATION);
  69.         }
  70.         break;
  71.         case IDT_PROGRESS_TIMER:
  72.             SendMessage(hWndProgressBar, PBM_STEPIT, 0, 0);
  73.             break;
  74.         }
  75.     }
  76.     break;
  77.     case WM_COMMAND:
  78.         if (LOWORD(wParam) == ID_BUTTON)
  79.         {
  80.             if (SetTimer(hwnd, IDT_TIMER, 5000, (TIMERPROC)NULL))
  81.             {
  82.                 SetTimer(hwnd, IDT_PROGRESS_TIMER, 500, (TIMERPROC)NULL);
  83.                 MessageBox(NULL, "Timer set for 5 seconds", "Success", MB_OK | MB_ICONINFORMATION);
  84.             }
  85.             else
  86.             {
  87.                 MessageBox(NULL, "Timer failed", "Error", MB_OK | MB_ICONERROR);
  88.             }
  89.         }
  90.         break;
  91.     case WM_DESTROY:
  92.         PostQuitMessage(0);
  93.         break;
  94.     }
  95.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  96. }
  97.  
  98. void CenterWindow(HWND hwnd)
  99. {
  100.     RECT rc = { 0 };
  101.     GetWindowRect(hwnd, &rc);
  102.     int win_w = rc.right - rc.left;
  103.     int win_h = rc.bottom - rc.top;
  104.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  105.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  106.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w) / 2, (screen_h - win_h) / 2, 0, 0, SWP_NOSIZE);
  107. }
  108.  
  109. LPWSTR stringToLPWSTR(const std::string& instr)
  110. {
  111.     int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
  112.     LPWSTR widestr = new WCHAR[bufferlen + 1];
  113.     ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
  114.     widestr[bufferlen] = 0;
  115.     return widestr;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement