Advertisement
hugol

Untitled

Apr 13th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.29 KB | None | 0 0
  1. // GT_HelloWorldWin32.cpp
  2. // compile with: /D_UNICODE /DUNICODE /DWIN32 /D_WINDOWS /c
  3.  
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <tchar.h>
  8. #include <atlbase.h>
  9.  
  10. // Global variables
  11.  
  12. #define ID_PRZYCISK1 501
  13.  
  14. // The main window class name.
  15. static TCHAR szWindowClass[] = _T("win32app");
  16.  
  17. // The string that appears in the application's title bar.
  18. static TCHAR szTitle[] = _T("Ustawiacz schowków");
  19.  
  20. HINSTANCE hInst;
  21.  
  22. HWND hText;
  23.  
  24. // Forward declarations of functions included in this code module:
  25. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  26.  
  27. int WINAPI WinMain(HINSTANCE hInstance,
  28.     HINSTANCE hPrevInstance,
  29.     LPSTR lpCmdLine,
  30.     int nCmdShow)
  31. {
  32.     WNDCLASSEX wcex;
  33.  
  34.     wcex.cbSize = sizeof(WNDCLASSEX);
  35.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  36.     wcex.lpfnWndProc = WndProc;
  37.     wcex.cbClsExtra = 0;
  38.     wcex.cbWndExtra = 0;
  39.     wcex.hInstance = hInstance;
  40.     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  41.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  42.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  43.     wcex.lpszMenuName = NULL;
  44.     wcex.lpszClassName = szWindowClass;
  45.     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
  46.  
  47.  
  48.     if (!RegisterClassEx(&wcex))
  49.     {
  50.         MessageBox(NULL,
  51.             _T("Call to RegisterClassEx failed!"),
  52.             _T("Win32 Guided Tour"),
  53.             NULL);
  54.  
  55.         return 1;
  56.     }
  57.  
  58.     hInst = hInstance; // Store instance handle in our global variable
  59.  
  60.     // The parameters to CreateWindow explained:
  61.     // szWindowClass: the name of the application
  62.     // szTitle: the text that appears in the title bar
  63.     // WS_OVERLAPPEDWINDOW: the type of window to create
  64.     // CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
  65.     // 500, 100: initial size (width, length)
  66.     // NULL: the parent of this window
  67.     // NULL: this application does not have a menu bar
  68.     // hInstance: the first parameter from WinMain
  69.     // NULL: not used in this application
  70.     HWND hWnd = CreateWindow(
  71.         szWindowClass,
  72.         szTitle,
  73.         WS_OVERLAPPEDWINDOW,
  74.         CW_USEDEFAULT, CW_USEDEFAULT,
  75.         500, 300,
  76.         NULL,
  77.         NULL,
  78.         hInstance,
  79.         NULL
  80.         );
  81.  
  82.     if (!hWnd)
  83.     {
  84.         MessageBox(NULL,
  85.             _T("Call to CreateWindow failed!"),
  86.             _T("Win32 Guided Tour"),
  87.             NULL);
  88.  
  89.         return 1;
  90.     }
  91.  
  92.     HWND g_hPrzycisk;
  93.     g_hPrzycisk = CreateWindowEx(0, "BUTTON", "Ustaw schowek", WS_CHILD | WS_VISIBLE,
  94.         5, 35, 150, 30, hWnd, (HMENU) ID_PRZYCISK1, hInstance, NULL);
  95.  
  96.  
  97.     hText = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 475, 20,
  98.         hWnd, NULL, hInstance, NULL);
  99.  
  100.     AddClipboardFormatListener(hWnd);
  101.  
  102.     // The parameters to ShowWindow explained:
  103.     // hWnd: the value returned from CreateWindow
  104.     // nCmdShow: the fourth parameter from WinMain
  105.     ShowWindow(hWnd,
  106.         nCmdShow);
  107.     UpdateWindow(hWnd);
  108.  
  109.     // Main message loop:
  110.     MSG msg;
  111.     while (GetMessage(&msg, NULL, 0, 0))
  112.     {
  113.         TranslateMessage(&msg);
  114.         DispatchMessage(&msg);
  115.     }
  116.  
  117.     return (int)msg.wParam;
  118. }
  119.  
  120.  
  121. int ustawSchowek(LPWSTR tekst)
  122. {
  123.     LPWSTR cwdBuffer = tekst;
  124.  
  125.     DWORD len = wcslen(cwdBuffer);
  126.     HGLOBAL hdst;
  127.     LPWSTR dst;
  128.  
  129.     // Allocate string for cwd
  130.     hdst = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (len + 1) * sizeof(WCHAR));
  131.     dst = (LPWSTR)GlobalLock(hdst);
  132.     memcpy(dst, cwdBuffer, len * sizeof(WCHAR));
  133.     dst[len] = 0;
  134.     GlobalUnlock(hdst);
  135.  
  136.     // Set clipboard data
  137.     if (!OpenClipboard(NULL)) return GetLastError();
  138.     EmptyClipboard();
  139.     if (!SetClipboardData(CF_UNICODETEXT, hdst)) return GetLastError();
  140.     CloseClipboard();
  141.  
  142.     //free(cwdBuffer);
  143.     return 0;
  144. }
  145.  
  146. //
  147. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  148. //
  149. //  PURPOSE:  Processes messages for the main window.
  150. //
  151. //  WM_PAINT    - Paint the main window
  152. //  WM_DESTROY  - post a quit message and return
  153. //
  154. //
  155. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  156. {
  157.     PAINTSTRUCT ps;
  158.     HDC hdc;
  159.     TCHAR greeting[] = _T("Hello, World!");
  160.  
  161.  
  162.     switch (message)
  163.     {
  164.     case WM_PAINT:
  165.         hdc = BeginPaint(hWnd, &ps);
  166.  
  167.         // Here your application is laid out.
  168.         // For this introduction, we just print out "Hello, World!"
  169.         // in the top left corner.
  170.         //TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
  171.         // End application-specific layout section.
  172.  
  173.         EndPaint(hWnd, &ps);
  174.         break;
  175.     case WM_DESTROY:
  176.         PostQuitMessage(0);
  177.         break;
  178.     case WM_CLIPBOARDUPDATE:
  179.     {
  180.         // Try opening the clipboard
  181.         OpenClipboard(nullptr);
  182.         // Get handle of clipboard object for ANSI text
  183.         HANDLE hData = GetClipboardData(CF_UNICODETEXT);
  184.         // Lock the handle to get the actual text pointer
  185.         LPWSTR pszText = (LPWSTR)(GlobalLock(hData));
  186.         USES_CONVERSION;
  187.         LPCSTR s = W2A(pszText);
  188.         SetWindowText(hText, s);
  189.         // Release the lock
  190.         GlobalUnlock(hData);
  191.  
  192.         // Release the clipboard
  193.         CloseClipboard();
  194.     }
  195.         break;
  196.     case WM_COMMAND:
  197.         switch (wParam)
  198.         {
  199.         case ID_PRZYCISK1:
  200.         {
  201.             //MessageBox(hWnd, "Wcisnąłeś przycisk 1", "Test", MB_ICONINFORMATION);
  202.             DWORD dlugosc = GetWindowTextLength(hText);
  203.             LPSTR Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
  204.             GetWindowText(hText, Bufor, dlugosc + 1);
  205.             USES_CONVERSION;
  206.             LPCWSTR w = A2W(Bufor);
  207.  
  208.             ustawSchowek((LPWSTR) w);
  209.         }
  210.             break;
  211.         default:
  212.             //MessageBox(hWnd, "Zrobiłeś coś innego ;-)", "Test", MB_ICONINFORMATION);
  213.             break;
  214.         }
  215.         break;
  216.     default:
  217.         return DefWindowProc(hWnd, message, wParam, lParam);
  218.         break;
  219.     }
  220.  
  221.     return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement