在用MFC ActiveX開發控件時,將一個對話框添加到工程,然后在這個對話框上開發控件需要的應用,能更好的利用MFC帶來的方便....
具體步驟如下:
1)建立一個基於MFC的ActiveX工程;
2)在工程的資源視圖中,添加一個新的對話框,並且將對話框的樣式設置為,style:child,border:none,visible;
3)為這個對話框添加一個類,類型為CMyDlg;
4)為Ctrl類添加一個成員變量 CMyDlg m_dlg;並且添加OnCreate消息響應函數,在函數中添加如下代碼:
int Ctest123Ctrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_dlg.Create(IDD_DIALOG1, this);
return 0;
}
5)改寫Ctrl類中的OnDraw函數,如下:
void Ctest123Ctrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
if (!pdc)
return;
// TODO: Replace the following code with your own drawing code.
// pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
// pdc->Ellipse(rcBounds);
m_dlg.MoveWindow(rcBounds, 1);
}
這樣在測試控件時,顯示的就是上面添加的對話框樣式