WindowsAPIで,ボタンを50個ほど表示するプログラムをつくったのですが,どれもうまく反応しませんでした。
下のようなソースコードで試してもやはりだめでした。
C++
1#include <windows.h> 2#include <tchar.h> 3 4template<typename T_int> static inline HMENU ToHMENU(T_int &integer) 5{ 6 volatile T_int *hMenu = (T_int*)&integer; 7 return (HMENU)(void*)hMenu; 8} 9 10LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { 11 enum { BTN0 = 0 , BTN1 = 1 , BTN2 = 2 , BTN3 = 3 , BTN4 = 4 , BTN5 = 5 , BTN6 = 6 , BTN7 = 7 , BTN8 = 8 , BTN9 = 9 }; 12 int count , x; 13 TCHAR text[2]; 14 15 switch (msg) { 16 case WM_CREATE: 17 for(count = 0 , x = 0 ; count < 10 ; count++ , x+=20) 18 { 19 wsprintf(text , _T("%d") , count); 20 CreateWindow(_T("BUTTON") , text , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , x , 0 , 20 , 20 , hwnd , ToHMENU(count) , ((LPCREATESTRUCT)(lp))->hInstance , NULL); 21 } 22 return 0; 23 case WM_DESTROY: 24 PostQuitMessage(0); 25 return 0; 26 case WM_COMMAND: 27 switch(LOWORD(wp)) { 28 case BTN0: 29 MessageBox(hwnd , _T("BTN0") , _T("TEST") , MB_OK); 30 break; 31 case BTN1: 32 MessageBox(hwnd , _T("BTN1") , _T("TEST") , MB_OK); 33 break; 34 case BTN2: 35 MessageBox(hwnd , _T("BTN2") , _T("TEST") , MB_OK); 36 break; 37 case BTN3: 38 MessageBox(hwnd , _T("BTN3") , _T("TEST") , MB_OK); 39 break; 40 case BTN4: 41 MessageBox(hwnd , _T("BTN4") , _T("TEST") , MB_OK); 42 break; 43 case BTN5: 44 MessageBox(hwnd , _T("BTN5") , _T("TEST") , MB_OK); 45 break; 46 case BTN6: 47 MessageBox(hwnd , _T("BTN6") , _T("TEST") , MB_OK); 48 break; 49 case BTN7: 50 MessageBox(hwnd , _T("BTN7") , _T("TEST") , MB_OK); 51 break; 52 case BTN8: 53 MessageBox(hwnd , _T("BTN8") , _T("TEST") , MB_OK); 54 break; 55 case BTN9: 56 MessageBox(hwnd , _T("BTN9") , _T("TEST") , MB_OK); 57 break; 58 } 59 return 0; 60 } 61 return DefWindowProc(hwnd , msg , wp , lp); 62} 63 64int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { 65 HWND hwnd; 66 MSG msg; 67 WNDCLASS winc; 68 69 winc.style = CS_HREDRAW | CS_VREDRAW; 70 winc.lpfnWndProc = WndProc; 71 winc.cbClsExtra = winc.cbWndExtra = 0; 72 winc.hInstance = hInstance; 73 winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); 74 winc.hCursor = LoadCursor(NULL , IDC_ARROW); 75 winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 76 winc.lpszMenuName = NULL; 77 winc.lpszClassName = _T("WINDOW"); 78 79 if (!RegisterClass(&winc)) return -1; 80 81 hwnd = CreateWindow( 82 TEXT("WINDOW") , TEXT("WINDOW") , 83 WS_OVERLAPPEDWINDOW | WS_VISIBLE , 84 CW_USEDEFAULT , CW_USEDEFAULT , 85 CW_USEDEFAULT , CW_USEDEFAULT , 86 NULL , NULL , hInstance , NULL 87 ); 88 89 if (hwnd == NULL) return -1; 90 91 while(GetMessage(&msg , NULL , 0 , 0)) DispatchMessage(&msg); 92 return msg.wParam; 93} 94
ただ単に(HMENU)1みたいにすれば問題ないのでしょうが,僕が実際につくっているプログラムはボタンを50個ほど扱うので,いちいちCreateWindowしていると効率が悪いです。
整数型->HMENUの変換処理が悪いだけなのか,そもそも変数を指定してはいけないのか,わかる方は回答をお願いします。
環境
Windows 8.1
MinGW-W64
gcc
1Using built-in specs. 2COLLECT_GCC=gcc 3COLLECT_LTO_WRAPPER=D:/Apps/MinGW-W64/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe 4Target: x86_64-w64-mingw32 5Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw810/x86_64-810-pos 6ix-seh-rt_v6-rev0/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable 7-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-libstdcxx-pch --disable-libstdcxx-debug --e 8nable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --w 9ith-system-zlib --with-gmp=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw810/prerequisites/x86_64-w64 10-mingw32-static --with-isl=/c/mingw810/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, Built by MinGW-W64 project' --with-bugurl=https://sourceforge.net/pro 11jects/mingw-w64 CFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include -I/c/mingw810/prerequisi 12tes/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -fno-ident -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/include 13 -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS=' -I/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/include -I/c/mingw810/prerequisites/x86_64-zlib-static/in 14clude -I/c/mingw810/prerequisites/x86_64-w64-mingw32-static/include' LDFLAGS='-pipe -fno-ident -L/c/mingw810/x86_64-810-posix-seh-rt_v6-rev0/mingw64/opt/lib -L/c/mingw810/prerequisites/x86_6 154-zlib-static/lib -L/c/mingw810/prerequisites/x86_64-w64-mingw32-static/lib ' 16Thread model: posix 17gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
回答3件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/04/21 00:23 編集