ダイアログにIDCANCEL制御IDを持つ非ボタンがあるとどのような恐ろしいことが起こるか?
Raymond Chen氏のブログ「The Old New Thing」で、ダイアログ内の非ボタン要素にIDCANCELという制御IDを誤って割り当てた場合に発生する可能性のある予期せぬ通知や動作の問題についての技術的な注意点が紹介されている。
キーポイント
IDCANCEL制御IDの誤用による問題
ダイアログ内のボタン以外の要素(例:ラベル、テキストボックス)にIDCANCELという制御IDを誤って割り当てると、システムがその要素をキャンセルボタンと誤認し、予期しない通知や動作が発生する可能性がある。
Windowsダイアログボックスの仕組み
Windowsのダイアログボックス管理システムは、特定の制御ID(IDOK, IDCANCELなど)を持つ要素に対して特別な動作を定義しており、この仕組みを理解していないと意図しない動作を引き起こす。
開発者向けの実践的注意点
この記事は、Windowsアプリケーション開発者がダイアログリソースを設計・実装する際に、制御IDの命名規則とシステム予約IDを正しく理解し、誤用を避けるための具体的な注意点を提供している。
影響分析・編集コメントを表示
影響分析
この記事は特定の技術的ニッチ(Windowsダイアログプログラミング)に特化した実践的な注意点を提供しており、該当領域の開発者にとっては有用な情報だが、AI業界全体や広範な技術トレンドに直接的な影響を与えるものではない。Raymond Chen氏の長年のWindows開発経験に基づく実践的知見の共有という位置付けである。
編集コメント
AI/機械学習の核心的な話題ではなく、Windowsアプリケーション開発の実践的な技術ノートである。AI業界のアナリストとしては参考情報程度の扱いが適切で、広範な技術トレンド分析の対象としては限定的。
意味を成さない通知が表示されることがあります。
この投稿「ダイアログにIDCANCELというコントロールIDを持つ非ボタンがあると、どのような問題が発生するのか?」は、The Old New Thingで最初に公開されました。
原文を表示
I noted in the bonus chatter that if you have a control whose ID is IDCANCEL, it had better be a button if you know what’s good for you. Shawn Keene doesn’t know what’s good for him and asks, “I can’t wait to find out what happens if my control is not a button.”
What happens is that the dialog manager will generate a WM_COMMAND message *as if* your IDCANCEL control were a button, even if it isn’t.
This means that the message arrives with a notification code of BN_CLICKED, a control ID of IDCANCEL, and a window handle of your not-actually-a-button window.
Since your control is not actually a button, it will treat the BN_CLICKED as if it were a notification appropriate to its type. The numeric value of BN_CLICKED is zero, so it’ll be treated as whatever a notification code of zero means for that control type. For example, if it’s actually a static control, you’ll interpret the zero to mean STN_CLICKED.
If it’s actually a list box, then you’ll see the zero and say “Huh? I don’t see any notification code with the numeric value of zero. What’s going on?”
If it’s a custom control, you will interpret the zero as whatever that custom control defines notification code zero to mean.
Basically, what you did is signed yourself up for confusion. You’re going to get a WM_COMMAND message with BN_CLICKED as the notification code, IDCANCEL as the control ID, and your non-button window as the control. What you do with that information is up to you.
Bonus reading: Why do dialog editors start assigning control IDs with 100?
Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み