Advertisement
Alan468

Untitled

Oct 18th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include <Windows.h> // Podstawa do WinAPI
  2. #include <string>
  3. using namespace std;
  4. HWND hwnd, Res ,Pos;
  5. WNDCLASSEX window; // Zmienna klasy
  6. MSG msg; // Zmienna wydarzenia
  7. TCHAR Class_Name[] = TEXT("OKNO_TEST"), Title[] = TEXT("Tytul"); // Tytuly TCHAR
  8. int NumberOfWindows = 0;
  9. int PositionX = 0, PositionY = 0;
  10. int Width = 0, Height = 0;
  11. RECT rect;
  12.  
  13. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {//Funkcja eventów
  14.     switch (msg) {
  15.     case WM_CREATE:
  16.         NumberOfWindows++;
  17.         UpdateWindow(hwnd);
  18.         break;
  19.     case WM_COMMAND:    break;// Eventy uzytkownika
  20.     case WM_CLOSE:// Event zamkniecia
  21.         if (MessageBox(0, TEXT("Zamknac to okno?"), TEXT("?"), MB_YESNO | MB_ICONQUESTION) == IDYES) {
  22.             NumberOfWindows--;
  23.             DestroyWindow(hwnd);
  24.         }
  25.         break;
  26.     case WM_DESTROY:// Event zamkniecia
  27.         if (NumberOfWindows == 0) {
  28.             MessageBox(0, TEXT("KONIEC!"), TEXT("KONIEC!"), MB_OK);
  29.             PostQuitMessage(0);
  30.         }
  31.         break;
  32.     case WM_LBUTTONDOWN:
  33.         hwnd = CreateWindowEx(0, Class_Name, TEXT("Nowe Okno"), WS_OVERLAPPEDWINDOW, (100+(NumberOfWindows*10)), (100 + (NumberOfWindows * 10)), 500, 300, 0, 0, GetModuleHandle(0), 0);
  34.         ShowWindow(hwnd, SW_SHOW);
  35.         UpdateWindow(hwnd);
  36.         break;
  37.     case WM_SIZE:
  38.         if (GetWindowRect(hwnd, &rect)) {
  39.             Width = rect.right - rect.left;
  40.             Height = rect.bottom - rect.top;
  41.             SetWindowText(Res, TEXT(("Rozdzielczość: " + to_string(Width) + "x" + to_string(Height)).c_str()));
  42.         }
  43.         break;
  44.     case WM_MOVE:
  45.         InvalidateRect(hwnd, &rect, 1);
  46.         PositionX = LOWORD(lParam);
  47.         PositionY = HIWORD(lParam);
  48.         SetWindowText(Pos, TEXT(("Pozycja: " + to_string(PositionY) + "x" + to_string(PositionX)).c_str()));
  49.         break;
  50.     default:// Nieznany event
  51.         return DefWindowProc(hwnd, msg, wParam, lParam);
  52.     }
  53.     return 0;
  54. }
  55.  
  56. bool Register_Class(HINSTANCE &hInstance) {
  57.     window.cbClsExtra = NULL;// Wypelnianie klasy okna //
  58.     window.cbSize = sizeof(WNDCLASSEX);
  59.     window.cbWndExtra = NULL;
  60.     window.hbrBackground = (HBRUSH)(COLOR_WINDOW);
  61.     window.hCursor = LoadCursor(NULL, IDC_ARROW);
  62.     window.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  63.     window.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  64.     window.hInstance = hInstance;
  65.     window.lpfnWndProc = WndProc;
  66.     window.lpszClassName = Class_Name;
  67.     window.lpszMenuName = 0;
  68.     window.style = CS_VREDRAW | CS_HREDRAW;
  69.     return RegisterClassEx(&window);
  70. }
  71.  
  72. void Unregister_Window(HINSTANCE &hInstance) {
  73.     if (!UnregisterClass(Class_Name, hInstance)) { // Niszczenie okna ....
  74.         MessageBox(NULL, TEXT("Błąd niszczenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  75.     }
  76. }
  77.  
  78. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR ilCmdLine, int nCmdShow) { // main
  79.     Register_Class(hInstance);
  80.  
  81.     if (!Register_Class) { // Rejestracja klasy okna .... jesli blad STOP
  82.         MessageBox(NULL, TEXT("Błąd rejestracji okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  83.         return 0;
  84.     }
  85.  
  86.     hwnd = CreateWindowEx(WS_EX_WINDOWEDGE, Class_Name, Title, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 200-8, 200-31, 800, 600, NULL, NULL, hInstance, NULL); //Tworzenie okna
  87.  
  88.     if (hwnd == NULL) {//Sprawdzanie czy okno zostalo stworzeona
  89.         MessageBox(NULL, TEXT("Błąd tworzenia okna"), TEXT("Błąd"), MB_ICONEXCLAMATION);
  90.         return 0;
  91.     }
  92.     string s = to_string(Width) + to_string(Height);
  93.     Res = CreateWindowEx(NULL, "STATIC", TEXT(("Rozdzielczość: " + to_string(Width) + "x" + to_string(Height)).c_str()), WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 10, 200, 50, hwnd, NULL, hInstance, NULL);
  94.     Pos = CreateWindowEx(NULL, "STATIC", TEXT(("Pozycja: " + to_string(PositionY) + "x" + to_string(PositionX)).c_str()), WS_VISIBLE | WS_CHILD | SS_LEFT, 10, 60, 200, 50, hwnd, NULL, hInstance, NULL);
  95.  
  96.     ShowWindow(hwnd, nCmdShow); // Wyswietlenie okna
  97.     UpdateWindow(hwnd); // Aktualizacja okna
  98.  
  99.     while (GetMessage(&msg, NULL, 0, 0)) { // Petla eventow
  100.         TranslateMessage(&msg); // tlumacznie eventu
  101.         DispatchMessage(&msg); // Wykonywanie eventu
  102.     }
  103.  
  104.  
  105.     Unregister_Window(hInstance);
  106.  
  107.     return msg.wParam;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement