前提・実現したいこと
Ctrl + V
によってクリップボードのテキストを表示するコードを作成しましたがウィンドウを動かしたときカクつきます.文字列の開放の場所が問題なのだとは思いますがどのタイミングでどのように開放すればいいのでしょうか
お願いします.
該当のソースコード
C
1#define UNICODE 2 3#include <windows.h> 4 5#define ID_PASTE 10001 6 7LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 8 9int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpszCmdLine, int nCmdShow) 10{ 11 UNREFERENCED_PARAMETER(hPrevInstance); 12 UNREFERENCED_PARAMETER(lpszCmdLine); 13 14 wchar_t CLASS_NAME[] = L"sample"; 15 16 WNDCLASSEX wc = { 17 sizeof(WNDCLASSEX),CS_VREDRAW | CS_HREDRAW, WindowProc, 18 0, 0, hInstance, 19 NULL, (HCURSOR)LoadImage(NULL, IDC_ARROW, IMAGE_CURSOR, 0, 0, LR_SHARED),(HBRUSH)GetStockObject(WHITE_BRUSH), 20 NULL, CLASS_NAME, NULL 21 }; 22 23 RegisterClassEx(&wc); 24 25 HWND hwnd = CreateWindowEx( 26 0, CLASS_NAME, L"Sample", WS_OVERLAPPEDWINDOW, 27 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 28 NULL, NULL, hInstance, NULL); 29 if (hwnd == NULL)return 0; 30 31 static ACCEL accel[1]; 32 accel[0].key = 0x56; 33 accel[0].cmd = ID_PASTE; 34 accel[0].fVirt = FCONTROL | FVIRTKEY; 35 36 HACCEL haccel = CreateAcceleratorTable(accel, 1); 37 38 ShowWindow(hwnd, nCmdShow); 39 UpdateWindow(hwnd); 40 41 MSG msg = {}; 42 while (GetMessage(&msg, NULL, 0, 0) > 0) { 43 if (!TranslateAccelerator(hwnd, haccel, &msg)) { 44 TranslateMessage(&msg); 45 DispatchMessage(&msg); 46 } 47 } 48 49 DestroyAcceleratorTable(haccel); 50 51 return (int)msg.wParam; 52} 53 54LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 55{ 56 static LPWSTR str; 57 HDC hdc = GetDC(hwnd); 58 static RECT rc; 59 60 switch (uMsg) { 61 case WM_CREATE: 62 break; 63 64 case WM_COMMAND: 65 if (LOWORD(wParam) == ID_PASTE) 66 { 67 OpenClipboard(hwnd); 68 if (IsClipboardFormatAvailable(CF_UNICODETEXT) != 0) { 69 HGLOBAL hg = GetClipboardData(CF_UNICODETEXT); 70 71 wchar_t message[128]; 72 wsprintf(message, L"GlobalSize: %dByte", GlobalSize(hg)); 73 MessageBox(hwnd, message, L"ALERT", MB_OK); 74 str = (LPWSTR)malloc(GlobalSize(hg) * sizeof(LPWSTR)); 75 LPWSTR strclip = (LPWSTR)GlobalLock(hg); 76 lstrcpy(str, strclip); 77 78 GlobalUnlock(hg); 79 CloseClipboard(); 80 81 InvalidateRect(hwnd, NULL, TRUE); 82 } 83 } 84 85 break; 86 87 case WM_PAINT: 88 { 89 PAINTSTRUCT ps = {}; 90 91 BeginPaint(hwnd, &ps); 92 GetClientRect(hwnd, &rc); 93 DrawText(hdc, str, -1, &rc, DT_LEFT); 94 EndPaint(hwnd, &ps); 95 } 96 break; 97 98 case WM_DESTROY: 99 free(str); 100 101 PostQuitMessage(0); 102 return 0; 103 104 default: 105 break; 106 } 107 108 return DefWindowProc(hwnd, uMsg, wParam, lParam); 109} 110
補足情報
Windows10 Pro
VisualStudio2017 Community
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。