2012-10-26 38 views
4

我用VC++ 6.0運行代碼,一切正常。但是,在Visual C++ 2010下運行相同的代碼時,wnd(即m_hWnd)的句柄始終爲NULL。此外,返回值bRet是TRUE(即成功)。函數CWnd :: CreateControl成功,但m_hWnd爲NULL

這裏是我的代碼:

BOOL CDemoDlg::OnInitDialog() 
{ 
    CDialog::OnInitDialog(); 

    // Set the icon for this dialog. The framework does this automatically 
    // when the application's main window is not a dialog 
    SetIcon(m_hIcon, TRUE);   // Set big icon 
    SetIcon(m_hIcon, FALSE);  // Set small icon 

    // TODO: Add extra initialization here 
    CRect rect; 
    GetClientRect(rect); 
    CWnd wnd; 
    BOOL bRet = wnd.CreateControl(_T("WMPlayer.OCX"), NULL, WS_VISIBLE, rect, this, 19089); 

    return TRUE; // return TRUE unless you set the focus to a control 
} 
+1

您是否嘗試使用調試器進入'CreateControl'以查看會發生什麼? – sashoalm

+3

它工作嗎?實際上OCX可以是無窗口的(所以m_hWnd將**總是**爲NULL)。 –

+1

@Tri也[see this](http://stackoverflow.com/q/7956520/492336) – sashoalm

回答

0

的WND對象是走出去的範圍 - 試圖使它成爲一個成員和檢查,然後會發生什麼。另外,如果你正在嘗試爲對話框m_hWnd對象分配一個不同的句柄,那麼你就犯了一個錯誤,因爲在調用OnInitDialog的時候m_hWnd應該對你的對話有效(對創建調用作出響應),所以你不應該重新分配對話框成員窗口句柄,而是爲它創建一個單獨的成員。希望這可以幫助。