Advertisement
Combreal

RandomNumberPicker

Oct 9th, 2017
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <windows.h>
  2. #include <ctime>
  3. #include <stdio.h>
  4. #include "resource.h"
  5.  
  6. #define ID_BUTTON 1
  7. #define ID_EDIT 2
  8.  
  9. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  10. static HWND hwndEdit;
  11. static HWND hwndLabel;
  12. void CenterWindow(HWND);
  13. int GetRandomNumber(int passedNumber);
  14.  
  15. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
  16.         srand(time(NULL));
  17.         MSG  msg;    
  18.         WNDCLASSW wc = {0};
  19.         wc.lpszClassName = L"RNP";
  20.         wc.hInstance     = hInstance;
  21.         wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
  22.         wc.lpfnWndProc   = WndProc;
  23.         wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
  24.         wc.hCursor       = LoadCursor(0, IDC_ARROW);
  25.         RegisterClassW(&wc);
  26.         CreateWindowW(wc.lpszClassName, L"RNP", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 260, 134, 265, 134, 0, 0, hInstance, 0);  
  27.         while (GetMessage(&msg, NULL, 0, 0)) {
  28.             TranslateMessage(&msg);
  29.             DispatchMessage(&msg);
  30.         }
  31.         return (int) msg.wParam;
  32. }
  33.  
  34. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  35.     static wchar_t *max =  L"Entrez la borne maximale :";
  36.     static wchar_t *responce =  L"1";
  37.     HWND hwndButton;
  38.     switch(msg) {
  39.     case WM_KEYDOWN:
  40.         if (wParam == VK_ESCAPE) {
  41.             int ret = MessageBoxW(hwnd, L"Etes vous sur de vouloir quitter?",
  42.                 L"Message", MB_OKCANCEL);                            
  43.             if (ret == IDOK) {
  44.                 SendMessage(hwnd, WM_CLOSE, 0, 0);
  45.             }
  46.         }    
  47.         break;
  48.     case WM_CREATE:
  49.         hwndButton=CreateWindowW(L"Button", L"Generer", WS_VISIBLE|WS_CHILD, 98, 37, 62, 21, hwnd, (HMENU)ID_BUTTON, NULL, NULL);
  50.         CenterWindow(hwnd);
  51.         CreateWindowW(L"Static", max, WS_CHILD|WS_VISIBLE|SS_LEFT, 10, 12, 197, 30, hwnd, (HMENU)1, NULL, NULL);
  52.         hwndLabel=CreateWindowW(L"Static", L"", WS_CHILD|WS_VISIBLE|SS_LEFT, 116, 68, 50, 30, hwnd, (HMENU)1, NULL, NULL);
  53.         hwndEdit=CreateWindowW(L"Edit", NULL, WS_CHILD|WS_VISIBLE|WS_BORDER, 195, 9, 40, 23, hwnd, (HMENU)ID_EDIT, NULL, NULL);
  54.         break;  
  55.     case WM_COMMAND:
  56.         if (LOWORD(wParam)==ID_BUTTON) {
  57.             int number = 0;
  58.             TCHAR buff[1024];
  59.             char str[256];
  60.             GetWindowText(hwndEdit, buff, 1024);
  61.             number = GetRandomNumber(atoi(buff));
  62.             sprintf_s(str, "%d", number);
  63.             SetWindowText(hwndLabel, str);
  64.             RedrawWindow(hwndLabel, NULL, NULL, RDW_ERASE);
  65.         }
  66.         break;
  67.     case WM_DESTROY:  
  68.         PostQuitMessage(0);
  69.         break;
  70.     }
  71.     return DefWindowProcW(hwnd, msg, wParam, lParam);
  72. }
  73.  
  74. void CenterWindow(HWND hwnd) {
  75.     RECT rc = {0};
  76.     GetWindowRect(hwnd, &rc);
  77.     int win_w = rc.right - rc.left;
  78.     int win_h = rc.bottom - rc.top;
  79.     int screen_w = GetSystemMetrics(SM_CXSCREEN);
  80.     int screen_h = GetSystemMetrics(SM_CYSCREEN);
  81.     SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2, (screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
  82. }
  83.  
  84. int GetRandomNumber(int passedNumber)
  85. {
  86.     int min, max, number = 0;
  87.     min=1;
  88.     if(passedNumber!=0)
  89.     {
  90.         max=passedNumber;
  91.     }
  92.     else
  93.     {
  94.         max=10;
  95.     }
  96.     number = rand()%(max-min)+min;
  97.     /*char str[256];
  98.     sprintf_s(str, "%d", number);
  99.     OutputDebugString(buff);*/
  100.     return number;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement