MFC已经有鸟瞰图的指针,图像已经显示,怎么实现鸟瞰图的显示 代码
时间: 2024-02-09 09:09:14 浏览: 80
下面是一个简单的示例代码,演示了如何在MFC程序中显示鸟瞰图:
```
// 定义一个CDialog派生类用于显示鸟瞰图
class CBirdViewDlg : public CDialog
{
public:
CBirdViewDlg(CWnd* pParent = NULL)
: CDialog(IDD_BIRD_VIEW_DLG, pParent)
{
m_pBirdView = NULL;
}
// 设置鸟瞰图指针
void SetBirdView(void* pBirdView)
{
m_pBirdView = pBirdView;
}
protected:
void* m_pBirdView;
virtual BOOL OnInitDialog()
{
CDialog::OnInitDialog();
// 将鸟瞰图绘制到对话框的客户区中
if (m_pBirdView != NULL)
{
CDC* pDC = GetDC();
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, 800, 600);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&bmp);
memDC.BitBlt(0, 0, 800, 600, (CDC*)m_pBirdView, 0, 0, SRCCOPY);
pDC->BitBlt(0, 0, 800, 600, &memDC, 0, 0, SRCCOPY);
ReleaseDC(pDC);
}
return TRUE;
}
afx_msg void OnPaint()
{
CPaintDC dc(this);
// 绘制鸟瞰图边框
dc.SelectStockObject(BLACK_PEN);
dc.MoveTo(0, 0);
dc.LineTo(799, 0);
dc.LineTo(799, 599);
dc.LineTo(0, 599);
dc.LineTo(0, 0);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CBirdViewDlg, CDialog)
ON_WM_PAINT()
END_MESSAGE_MAP()
// 定义一个CMainFrame派生类,用于创建菜单和按钮,以及处理菜单和按钮的响应事件
class CMainFrame : public CFrameWnd
{
public:
CMainFrame()
{
Create(NULL, _T("Bird View Demo"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 800, 600));
}
protected:
CBirdViewDlg m_birdViewDlg;
afx_msg void OnShowBirdView()
{
// 显示鸟瞰图对话框
m_birdViewDlg.SetBirdView(m_pBirdView);
m_birdViewDlg.DoModal();
}
afx_msg void OnPaint()
{
CPaintDC dc(this);
// 在主窗口上绘制一张示意图
dc.SelectStockObject(BLACK_PEN);
dc.MoveTo(0, 0);
dc.LineTo(799, 599);
dc.MoveTo(799, 0);
dc.LineTo(0, 599);
// 绘制一个按钮,用于触发鸟瞰图的显示
dc.SelectStockObject(WHITE_BRUSH);
dc.Rectangle(10, 10, 110, 30);
dc.SetTextColor(RGB(0, 0, 255));
dc.TextOutW(20, 15, _T("Show Bird View"));
// 绘制一个菜单项,用于触发鸟瞰图的显示
CMenu* pMenu = GetMenu();
pMenu->AppendMenu(MF_STRING, ID_SHOW_BIRD_VIEW, _T("Show Bird View"));
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_PAINT()
ON_COMMAND(ID_SHOW_BIRD_VIEW, OnShowBirdView)
END_MESSAGE_MAP()
// 在应用程序类的InitInstance函数中创建主窗口对象
BOOL CBirdViewApp::InitInstance()
{
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
pFrame->ShowWindow(SW_SHOW);
pFrame->UpdateWindow();
// 创建一个示意图,并将其绘制到主窗口上
CDC* pDC = pFrame->GetDC();
CBitmap bmp;
bmp.CreateCompatibleBitmap(pDC, 800, 600);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(&bmp);
memDC.SelectStockObject(WHITE_BRUSH);
memDC.Rectangle(0, 0, 799, 599);
memDC.SelectStockObject(BLACK_PEN);
memDC.MoveTo(0, 0);
memDC.LineTo(799, 599);
memDC.MoveTo(799, 0);
memDC.LineTo(0, 599);
pDC->BitBlt(0, 0, 800, 600, &memDC, 0, 0, SRCCOPY);
pFrame->ReleaseDC(pDC);
// 创建一个鸟瞰图,并将其绘制到内存DC中
CDC* pBirdView = new CDC;
pBirdView->CreateCompatibleDC(NULL);
pBirdView->SelectObject(&bmp);
pBirdView->SelectStockObject(WHITE_BRUSH);
pBirdView->Rectangle(0, 0, 800, 600);
pBirdView->SelectStockObject(BLACK_PEN);
pBirdView->MoveTo(0, 0);
pBirdView->LineTo(799, 599);
pBirdView->MoveTo(799, 0);
pBirdView->LineTo(0, 599);
// 将鸟瞰图指针保存到主窗口对象中
pFrame->m_pBirdView = pBirdView;
return TRUE;
}
```
在上面的示例代码中,我们创建了一个CBirdViewDlg派生类,用于显示鸟瞰图,该类中的SetBirdView函数用于设置鸟瞰图指针,OnInitDialog函数用于在对话框中绘制鸟瞰图。在CMainFrame派生类中,我们创建了一个菜单和一个按钮,用于触发显示鸟瞰图,响应函数OnShowBirdView用于显示鸟瞰图对话框。在应用程序类的InitInstance函数中,我们创建了一个主窗口对象,并在其上绘制了一张示意图和一个菜单,同时创建了一个鸟瞰图,并将其保存到主窗口对象中。
阅读全文