请教达人,最好有实例,简单的就可以了,网上的都很模糊啊~
最好讲得清楚一点,谢谢大家。

解决方案 »

  1.   

    双缓冲的意思就是先将图画在内存中,然后再一次拷贝显示到屏幕上,没啥神秘的
    画图一般是在OnPaint中画:
    CPoint ptCenter; 
    CRect rect,ellipseRect; 
    GetClientRect(&rect); 
    ptCenter = rect.CenterPoint(); 
    CDC dcMem; //用于缓冲作图的内存DC 
    CBitmap bmp; //内存中承载临时图象的位图 
    dcMem.CreateCompatibleDC(pDC); //依附窗口DC创建兼容内存DC (就是一个内存DC,所有图形先画在上面)
    bmp.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());//创建兼容位图 
    dcMem.SelectObject(&bmp); //将位图选择进内存DC 
    //按原来背景填充客户区,不然会是黑色 
    dcMem.FillSolidRect(rect,pDC->GetBkColor()); 
    for(int i=20;i>0;i--) //在内存DC上做同样的同心圆图象 

    ellipseRect.SetRect(ptCenter,ptCenter); 
    ellipseRect.InflateRect(i*10,i*10); 
    dcMem.Ellipse(ellipseRect); 
    } pDC->BitBlt(0,0,rect.Width(),rect.Height(), 
    &dcMem,0,0,SRCCOPY);//将内存DC上的图象拷贝到前台 
     
      

  2.   

    pDC CDC
    在哪里定义啊?怎么定义啊?
      

  3.   

    大家有没有从对话框里用双缓冲的例子啊?传我一份吧
    [email protected]
    谢谢大家了
      

  4.   

    就是bitblt嘛,create一个兼容dc,在上面画图,然后在onpaint里面bitblt到paintdc上。
      

  5.   

    在onpaint里面画的时候pDC,CDC这些在哪里定义啊?怎么定义?
    我是字对话框里面画,没有ondraw啊~
      

  6.   

    这是我的原程序大家帮我看一看啊,编译是没错可是exe打开就报错
    Xv001Dlg.cpp文件// xv001Dlg.h : header file
    //#if !defined(AFX_XV001DLG_H__0B7DC168_1268_40FB_B2B2_1786D9C474D1__INCLUDED_)
    #define AFX_XV001DLG_H__0B7DC168_1268_40FB_B2B2_1786D9C474D1__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
    // CXv001Dlg dialogclass CXv001Dlg : public CDialog
    {
    // Construction
    public:
    CXv001Dlg(CWnd* pParent = NULL); // standard constructor
    CRect rect;                  // 存储绘图控件的绘图区域         CDC *pDC;                  // 控件的屏幕绘图设备指针         CDC MemDC;              // 内存绘图设备         CBitmap memBitmap;  // 用于内存绘图的位图           CBitmap* pOldBmp;    // 备份旧的位图指针         CWnd* pWnd;             // 绘图控件的指针
    // Dialog Data
    //{{AFX_DATA(CXv001Dlg)
    enum { IDD = IDD_XV001_DIALOG };
    // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CXv001Dlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    HICON m_hIcon; // Generated message map functions
    //{{AFX_MSG(CXv001Dlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnTimer(UINT nIDEvent);
    afx_msg void OnClose();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    void DrawWave(CDC *pDC);
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_XV001DLG_H__0B7DC168_1268_40FB_B2B2_1786D9C474D1__INCLUDED_)
      

  7.   

    Xv001Dlg.cpp文件 上面的是.h文件写错了// xv001Dlg.cpp : implementation file
    //#include "stdafx.h"
    #include "xv001.h"
    #include "xv001Dlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();

    // Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
    }void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
    // No message handlers
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CXv001Dlg dialogCXv001Dlg::CXv001Dlg(CWnd* pParent /*=NULL*/)
    : CDialog(CXv001Dlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CXv001Dlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CXv001Dlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CXv001Dlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CXv001Dlg, CDialog)
    //{{AFX_MSG_MAP(CXv001Dlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_TIMER()
    ON_WM_CLOSE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CXv001Dlg message handlersBOOL CXv001Dlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
        pWnd = GetDlgItem(IDC_COORD); // 获得对话框上的picture的窗口句柄        pWnd->GetClientRect(&rect);         // 获取绘制坐标的文本框pDC = pWnd->GetDC();                 // 获得对话框上的picture的设备指针        pOldBmp = NULL;    // 将旧的位图指针置空        // 创建内存绘图设备,使内存位图的DC与控件的DC关联        memDC.CreateCompatibleDC(pDC);memBitmap.CreateCompatibleBitmap(pDC,rect.right,rect.bottom);        pOldBmp = MemDC.SelectObject(&memBitmap); CDC MemDC; //首先定义一个显示设备对象
    CBitmap memBitmap;//定义一个位图对象SetTimer(1,10,NULL);          
    // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // 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

    return TRUE;  // return TRUE  unless you set the focus to a control
    }void CXv001Dlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CXv001Dlg::OnPaint() 
    {
    pDC->BitBlt(rect.left,rect.top,rect.right,rect.bottom,&MemDC,0,0,SRCCOPY); 
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CXv001Dlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }void CXv001Dlg::DrawWave(CDC *pDC){  
    //随后建立与屏幕显示兼容的内存显示设备
    MemDC.CreateCompatibleDC(NULL);
    //这时还不能绘图,因为没有地方画 ^_^
    //下面建立一个与屏幕显示兼容的位图,至于位图的大小嘛,可以用窗口的大小,也可以自己定义(如:有滚动条时就要大于当前窗口的大小,在BitBlt时决定拷贝内存的哪部分到屏幕上)
    memBitmap.CreateCompatibleBitmap(pDC,100,100);//将位图选入到内存显示设备中
    //只有选入了位图的内存显示设备才有地方绘图,画到指定的位图上
    CBitmap *pOldBit=MemDC.SelectObject(&memBitmap);
    //先用背景色将位图清除干净,这里我用的是白色作为背景
    //你也可以用自己应该用的颜色
    MemDC.FillSolidRect(0,0,100,100,RGB(255,255,255));
    //绘图
    MemDC.MoveTo(0,0);
    MemDC.LineTo(50,50);//将内存中的图拷贝到屏幕上进行显示
    pDC->BitBlt(0,0,100,100,&MemDC,0,0,SRCCOPY);
    //绘图完成后的清理
    memBitmap.DeleteObject();
    MemDC.DeleteDC();        
    /*CPen pen,*oldpen;            memDC.Rectangle(rect.left,rect.top,rect.right,rect.bottom);         for(int i =0; i<1500; i++)        {                pen.CreatePen(PS_SOLID,1, RGB(rand()%255,rand()%255,rand()%255));                 oldpen = memDC.SelectObject(&pen);                 int x0,y0,x1,y1;                  x0 = rand()%rect.Width();             x1 = rand()%rect.Width();           y0 = rand()%rect.Height();            y1 = rand()%rect.Height();                memDC.MoveTo(x0,y0);            memDC.LineTo(x1,y1);               memDC.SelectObject(oldpen);           pen.DeleteObject();        } */}void CXv001Dlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
    DrawWave(&MemDC);         // 在位图中画图片         OnPaint();                            // 使屏幕刷新          CDialog::OnTimer(nIDEvent);
    }void CXv001Dlg::OnClose() 
    {
    // TODO: Add your message handler code here and/or call default
    MemDC.SelectObject(pOldBmp);         MemDC.DeleteDC();         memBitmap.DeleteObject(); CDialog::OnClose();
    }
      

  8.   

    CPaintDC dc(this);在对话框的ONPaint中这样获取dc
      

  9.   


    在对话框下,最主要的是找不到ON_WM_ERASEBKGND()消息的映射。所以这个需要手动加入。在.h文件中加入:afx_msg BOOL OnEraseBkgnd(CDC* pDC);在.cpp文件消息映射中加入:    ON_WM_ERASEBKGND()再添上函数:BOOL CPaintPicture::OnEraseBkgnd(CDC* pDC){     return TRUE; }
    剩下的绘图工作放在OnPaint()中:CDC dcMem;dcMem.CreateCompatibleDC (&dc);CBitmap MemBitmap;MemBitmap.CreateCompatibleBitmap(&dc,rect.right,rect.bottom);
    CBitmap *OldBitmap = dcMem.SelectObject(&MemBitmap);for(int i=0;i<20;i++){       p[i] = CPoint((i*count)*4,i*50);}
    COLORREF clrBk1(RGB(255,255,0));CRect rect1(96,36,484,394);CBrush brush(clrBk1);dcMem.FillRect(rect,&brush);
    CPen pen3(PS_DASH,2,RGB(255,0,0));dcMem.SelectObject(&pen3);
    for(i=0;i<count;i++){              dcMem.MoveTo(p[i]);      dcMem.LineTo(p[i+1]);}dc.BitBlt(0,0,rect.right,rect.bottom,&dcMem,0,0,SRCCOPY);
    dcMem.SelectObject(OldBitmap);