0

サイズに基づいてダイアログ ウィンドウを動的に調整する必要があります。そのために、次の手法を使用します。

  1. それをロードして、CDialog::OnInitDialog() ハンドラからサイズを取得します。

  2. サイズが大きすぎる場合は、CDialog::EndDialog を呼び出してダイアログを終了します

  3. 次に、グローバル変数を更新し、ダイアログ派生クラスをサイズ調整で再度初期化します。

2 回目のパスで、一部の API が異常な動作を開始します。たとえば、MessageBox が表示されず (したがって、すべての ASSERT マクロが機能しなくなります)、一部の SetWindowText API がアプリをクラッシュさせます。理由はありますか?

コード スニペットは次のとおりです。

#define SPECIAL_VALUE -1
//From CWinApp-derived class
BOOL CWinAppDerivedClass::InitInstance()
{
    //...

    for(;;)
    {
        CDialogDerivedClass dlg(&nGlobalCounter);
        m_pMainWnd = &dlg;
        if(dlg.DoModal() != SPECIAL_VALUE)
            break;
    }

    //...
}

そして、ダイアログ クラス自体から:

//From CDialogDerivedClass
BOOL CDialogDerivedClass::OnInitDialog()
{
    //The following API shows message box only on the 1st pass, why?
    ::MessageBox(NULL, L"1", L"2", MB_OK);

    //...

    if(checkedDialogSizeIndicatesReload)
    {
        this->EndDialog(SPECIAL_VALUE);
        return FALSE;
    }

    //Continue loading dialog as usual
    ...
}

編集:次の行をコメントアウトすると、うまくいくように見えることに偶然気付きました。理由はありますか?

//m_pMainWnd = &dlg;
4

2 に答える 2

1

InitDialog は、ダイアログ ウィンドウが画面に表示される前に処理される最後のメッセージです。その場でサイズを検出して調整することができます。

if(checkedDialogSizeIndicatesReload)
    {
    // look up SetWindowPos - 
    // I am nt sure if there is another parameter or not that is optional
    int x,y,cx,cy;
    WINDOWPLACEMENT wp;
    GetWindowPlacement(&wp);
    // calc new size here
    SetWindowPos(this,x,y,cx,cy);
    }

// window appears when the message handler returns
于 2013-01-18T03:07:02.920 に答える