xxar3s

Untitled

Sep 6th, 2020 (edited)
1,817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include "framework.h"
  2. #include "WinnerX.h"
  3. #include <windows.h>
  4. #include <iostream>
  5.  
  6. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  7. {
  8.     switch (message)
  9.     {
  10.     case WM_CLIPBOARDUPDATE:
  11.     {
  12.         std::wcout << std::endl << L"WM_CLIPBOARDUPDATE" << std::endl;
  13.  
  14.         if (!IsClipboardFormatAvailable(CF_TEXT))
  15.         {
  16.             break;
  17.         }
  18.  
  19.         Sleep(1); // necessary for OpenClipboard
  20.         if (!OpenClipboard(hWnd))
  21.         {
  22.             std::wcout << L"Error: " << GetLastError() << std::endl;
  23.             break;
  24.         }
  25.  
  26.         auto clipboardObject = GetClipboardData(CF_TEXT);
  27.         if (clipboardObject == nullptr)
  28.         {
  29.             std::wcout << L"Error: " << GetLastError() << std::endl;
  30.             break;
  31.         }
  32.  
  33.         auto copyiedText = (char*)GlobalLock(clipboardObject);
  34.         if (copyiedText != nullptr)
  35.         {
  36.             std::wcout << L"Copyied: " << copyiedText << std::endl;
  37.             GlobalUnlock(copyiedText);
  38.         }
  39.         CloseClipboard();
  40.     }
  41.     break;
  42.     default:
  43.         return DefWindowProc(hWnd, message, wParam, lParam);
  44.     }
  45.     return 0;
  46. }
  47.  
  48. #define MAX_LOADSTRING 100
  49. WCHAR szTitle[MAX_LOADSTRING];
  50. WCHAR szWindowClass[MAX_LOADSTRING];
  51.  
  52. int main()
  53. {
  54.     auto appModule = GetModuleHandle(nullptr);
  55.     LoadStringW(appModule, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  56.     LoadStringW(appModule, IDC_WINNERX, szWindowClass, MAX_LOADSTRING);
  57.  
  58.     auto wcex = WNDCLASSEXW{};
  59.     wcex.cbSize = sizeof(WNDCLASSEX);
  60.     wcex.lpfnWndProc = WndProc;
  61.     wcex.hInstance = appModule;
  62.     wcex.lpszClassName = szWindowClass;
  63.  
  64.     if (!RegisterClassExW(&wcex))
  65.     {
  66.         std::wcout << L"Error: " << GetLastError() << std::endl;
  67.         return false;
  68.     }
  69.     auto hWnd = CreateWindowEx(0, szWindowClass, szTitle, 0, 0, 0, 0, 0, HWND_MESSAGE, nullptr, nullptr, nullptr);
  70.     if (!hWnd)
  71.     {
  72.         std::wcout << L"Error: " << GetLastError() << std::endl;
  73.         return false;
  74.     }
  75.  
  76.     if (!AddClipboardFormatListener(hWnd))
  77.     {
  78.         std::wcout << L"Error: " << GetLastError() << std::endl;
  79.         return false;
  80.     }
  81.  
  82.     auto msg = MSG();
  83.     while (GetMessage(&msg, nullptr, 0, 0))
  84.     {
  85.         TranslateMessage(&msg);
  86.         DispatchMessage(&msg);
  87.     }
  88.  
  89.     RemoveClipboardFormatListener(hWnd);
  90.  
  91.     return (int)msg.wParam;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment