質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

3回答

1326閲覧

CreateWindowに整数の変数をHMENUにキャストして渡すとボタンが反応しない

退会済みユーザー

退会済みユーザー

総合スコア0

Windows

Windowsは、マイクロソフト社が開発したオペレーティングシステムです。当初は、MS-DOSに変わるOSとして開発されました。 GUIを採用し、主にインテル系のCPUを搭載したコンピューターで動作します。Windows系OSのシェアは、90%を超えるといわれています。 パソコン用以外に、POSシステムやスマートフォンなどの携帯端末用、サーバ用のOSもあります。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2022/01/17 04:43

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)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

ベストアンサー

int32_tさんの回答によると,子ウィンドウIDは8以上でないといけないとのこと。

C++

1#include <windows.h> 2#include <tchar.h> 3 4LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { 5 enum { BTN0 = 8 , BTN1 = 9 , BTN2 = 10 , BTN3 = 11 , BTN4 = 12 , BTN5 = 13 , BTN6 = 14 , BTN7 = 15 , BTN8 = 16 , BTN9 = 17 }; 6 int count , x; 7 TCHAR text[2]; 8 9 switch (msg) { 10 case WM_CREATE: 11 for(count = 0 , x = 0 ; count < 10 ; count++ , x+=20) 12 { 13 wsprintf(text , _T("%d") , count); 14 CreateWindow(_T("BUTTON") , text , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , x , 0 , 20 , 20 , hwnd , reinterpret_cast<HMENU>(count + 8) , ((LPCREATESTRUCT)(lp))->hInstance , NULL); 15 } 16 return 0; 17 case WM_DESTROY: 18 PostQuitMessage(0); 19 return 0; 20 case WM_COMMAND: 21 switch(LOWORD(wp)) { 22 case BTN0: 23 MessageBox(hwnd , _T("BTN0") , _T("TEST") , MB_OK); 24 break; 25 case BTN1: 26 MessageBox(hwnd , _T("BTN1") , _T("TEST") , MB_OK); 27 break; 28 case BTN2: 29 MessageBox(hwnd , _T("BTN2") , _T("TEST") , MB_OK); 30 break; 31 case BTN3: 32 MessageBox(hwnd , _T("BTN3") , _T("TEST") , MB_OK); 33 break; 34 case BTN4: 35 MessageBox(hwnd , _T("BTN4") , _T("TEST") , MB_OK); 36 break; 37 case BTN5: 38 MessageBox(hwnd , _T("BTN5") , _T("TEST") , MB_OK); 39 break; 40 case BTN6: 41 MessageBox(hwnd , _T("BTN6") , _T("TEST") , MB_OK); 42 break; 43 case BTN7: 44 MessageBox(hwnd , _T("BTN7") , _T("TEST") , MB_OK); 45 break; 46 case BTN8: 47 MessageBox(hwnd , _T("BTN8") , _T("TEST") , MB_OK); 48 break; 49 case BTN9: 50 MessageBox(hwnd , _T("BTN9") , _T("TEST") , MB_OK); 51 break; 52 } 53 return 0; 54 } 55 return DefWindowProc(hwnd , msg , wp , lp); 56} 57 58int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { 59 HWND hwnd; 60 MSG msg; 61 WNDCLASS winc; 62 63 winc.style = CS_HREDRAW | CS_VREDRAW; 64 winc.lpfnWndProc = WndProc; 65 winc.cbClsExtra = winc.cbWndExtra = 0; 66 winc.hInstance = hInstance; 67 winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); 68 winc.hCursor = LoadCursor(NULL , IDC_ARROW); 69 winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 70 winc.lpszMenuName = NULL; 71 winc.lpszClassName = _T("WINDOW"); 72 73 if (!RegisterClass(&winc)) return -1; 74 75 hwnd = CreateWindow( 76 TEXT("WINDOW") , TEXT("WINDOW") , 77 WS_OVERLAPPEDWINDOW | WS_VISIBLE , 78 CW_USEDEFAULT , CW_USEDEFAULT , 79 CW_USEDEFAULT , CW_USEDEFAULT , 80 NULL , NULL , hInstance , NULL 81 ); 82 83 if (hwnd == NULL) return -1; 84 85 while(GetMessage(&msg , NULL , 0 , 0)) DispatchMessage(&msg); 86 return msg.wParam; 87} 88

投稿2022/01/17 06:26

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Zuishin

2022/04/21 00:23 編集

自分で解決していないでしょう。
guest

0

この場合、たんに整数を HMENU にキャストすればいいはずです。(HMENU)count とか reinterpret_cast<HMENU>(count) とか。質問文のコードはcountのアドレスを渡しています。

投稿2022/01/17 05:18

int32_t

総合スコア20856

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/01/17 05:24

どちらも試しましたが結果は変わりませんでした。
退会済みユーザー

退会済みユーザー

2022/01/17 06:09

#include <windows.h> #include <tchar.h> LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { enum { BTN0 = 8 , BTN1 = 9 , BTN2 = 10 , BTN3 = 11 , BTN4 = 12 , BTN5 = 13 , BTN6 = 14 , BTN7 = 15 , BTN8 = 16 , BTN9 = 17 }; int count , x; TCHAR text[2]; switch (msg) { case WM_CREATE: for(count = 0 , x = 0 ; count < 10 ; count++ , x+=20) { wsprintf(text , _T("%d") , count); CreateWindow(_T("BUTTON") , text , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , x , 0 , 20 , 20 , hwnd , (HMENU)(count + 8) , ((LPCREATESTRUCT)(lp))->hInstance , NULL); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_COMMAND: switch(LOWORD(wp)) { case BTN0: MessageBox(hwnd , _T("BTN0") , _T("TEST") , MB_OK); break; case BTN1: MessageBox(hwnd , _T("BTN1") , _T("TEST") , MB_OK); break; case BTN2: MessageBox(hwnd , _T("BTN2") , _T("TEST") , MB_OK); break; case BTN3: MessageBox(hwnd , _T("BTN3") , _T("TEST") , MB_OK); break; case BTN4: MessageBox(hwnd , _T("BTN4") , _T("TEST") , MB_OK); break; case BTN5: MessageBox(hwnd , _T("BTN5") , _T("TEST") , MB_OK); break; case BTN6: MessageBox(hwnd , _T("BTN6") , _T("TEST") , MB_OK); break; case BTN7: MessageBox(hwnd , _T("BTN7") , _T("TEST") , MB_OK); break; case BTN8: MessageBox(hwnd , _T("BTN8") , _T("TEST") , MB_OK); break; case BTN9: MessageBox(hwnd , _T("BTN9") , _T("TEST") , MB_OK); break; } return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = _T("WINDOW"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("WINDOW") , TEXT("WINDOW") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) DispatchMessage(&msg); return msg.wParam; }
退会済みユーザー

退会済みユーザー

2022/01/17 06:10

できました!ありがとうございます!
guest

0

HMENU の定義を調べてみては。
むちゃくちゃしても動きませんよ

投稿2022/01/17 05:09

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問