1

The message loop generated by class wizard often looks like

while( GetMessage() )
{
    if( !TranslateAccelerator() )
    {
        TranslateMessage();
        DispatchMessage();
    }
}

Whereas TranslateAccelerator documentation says:
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

In my tests, when the only reason why the TranslateAccelerator failed was no accelerator was found for this particular message, GetLastError returned 0==ERROR_SUCCESS.

I'm just curious, are there any realistic conditions when TranslateAccelerator fails because of some other reason, and the message should not be translated & dispatched?

Thanks in advance!

4

2 に答える 2

2

はい。メッセージは、 などの他のメッセージ フィルタリング API を通過する場合がありますIsDialogMessage()

MSDN から:

IsDialogMessage関数は必要なすべてのメッセージの変換とディスパッチを実行するため、IsDialogMessage によって処理されたメッセージをTranslateMessageまたは DispatchMessage 関数に渡してはなりません。

于 2009-10-30T21:07:01.957 に答える
1

TranslateMessage が失敗する可能性のあるさまざまな理由が何であれ、メッセージをまだ DispatchMessage に配信してはならないということはまったく暗示されていません (そして確かに明示的に述べられていません)。

提供されているコード サンプルの実際の問題は、0 に加えて、GetMessage が -1 のエラー コードを返す可能性があることです。-1 は、メッセージを処理しないことを意味します (メッセージがないため、MSG 構造体はおそらく初期化されていないか、前のメッセージのデータを持っている可能性があります)。壊れた状態を「修正」するために何かを行うことができない限り、GetMessage が一度 -1 を返した場合、その後の呼び出しではおそらく -1 が返されます - (おそらく) 正しい戦略は、メッセージ ループを終了することです。

また、初期化されていない MSG 構造体は、TranslateMessage や DispatchMessage が失敗する原因として考えられます。

于 2009-11-02T12:28:38.547 に答える