CClientDC和CPaintDC有些区别,但是我不知道怎么用CPaintDC这个类,我用CClientDC类能在屏幕上显示出来,但是CPaintDC类却不能,我发现只有在更新窗口框架的时候,才能闪一下而已,请问这个CPaintDC该怎么用,什么时候用?

解决方案 »

  1.   

    在OnPaint()函数中用CPaintDC,用于对窗口的绘制
    CClientDC用于对窗口客户区的绘制,在非OnPaint()中用
      

  2.   

    说得清楚点呀,我在OnPaint()中用CPaintDC也没有显示出来
      

  3.   

    CPaintDC objects encapsulate the common idiom of Windows, calling the BeginPaint function, then drawing in the device context, then calling the EndPaint function. The CPaintDC constructor calls BeginPaint for you, and the destructor calls EndPaint. The simplified process is to create the CDC object, draw, and destroy the CDC object. In the framework, much of even this process is automated. In particular, your OnDraw function is passed a CPaintDC already prepared (via OnPrepareDC), and you simply draw into it. It is destroyed by the framework and the underlying device context is released to Windows upon return from the call to your OnDraw function.CClientDC objects encapsulate working with a device context that represents only the client area of a window. The CClientDC constructor calls the GetDC function, and the destructor calls the ReleaseDC function.CWindowDC objects encapsulate a device context that represents the whole window, including its frame. 
      

  4.   

    CPaintDC 它执行一些有用的内务函数,当窗口响应WM_PAINT时需要它,
    CClientDC 当DC只在输出到窗口的客户区时使用。
      

  5.   

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc; switch (message) 
    {
    case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_OPEN:
    {
    // get file name to open
    TCHAR szFile[MAX_PATH];
    ZeroMemory(szFile, MAX_PATH);
    OPENFILENAME ofn;
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
    ofn.hwndOwner = hWnd;
    ofn.lpstrFilter = _T("Supported Files Types(*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf)\0*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf\0Bitmaps (*.bmp)\0*.bmp\0GIF Files (*.gif)\0*.gif\0JPEG Files (*.jpg)\0*.jpg\0Icons (*.ico)\0*.ico\0Enhanced Metafiles (*.emf)\0*.emf\0Windows Metafiles (*.wmf)\0*.wmf\0\0");
    ofn.lpstrTitle = _T("Open Picture File");
    ofn.lpstrFile = szFile;
    ofn.nMaxFile = MAX_PATH;
    if (IDOK == GetOpenFileName(&ofn))
    // load the file
    LoadPictureFile(szFile);
    }
    break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    if (gpPicture)
    {
    // get width and height of picture
    long hmWidth;
    long hmHeight;
    gpPicture->get_Width(&hmWidth);
    gpPicture->get_Height(&hmHeight);
    // convert himetric to pixels
    int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
    int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
    RECT rc;
    GetClientRect(hWnd, &rc);
    // display picture using IPicture::Render
    gpPicture->Render(hdc, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
    }
    EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }
      

  6.   

    #include <afxwin.h>class CMyApp : public CWinApp
    {
    public:
    BOOL InitInstance();
    };class CMainWindow : public CFrameWnd
    {
    public:
    CMainWindow();
    protected:
    afx_msg void OnPaint();
    DECLARE_MESSAGE_MAP()
    };CMyApp Myapp;CMyApp::InitInstance()
    {
    m_pMainWnd = new CMainWindow;
    m_pMainWnd->ShowWindow (m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
    }BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
       ON_WM_PAINT()
    END_MESSAGE_MAP()CMainWindow::CMainWindow()
    {
    Create (NULL,_T("first text"));
    }void CMainWindow::OnPaint()
    {
    CPaintDC dc (this);
    CRect rect;
    GetClientRect(&rect);
    dc.DrawText(_T("hahahah"),-1,&rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    }
      

  7.   

    以上代码出自msdn。上面为实现读并显示(*.bmp;*.gif;*.jpg;*.ico;*.emf;*.wmf)
    用WM_PAINT。
      

  8.   

    同意rockersz(天生我菜必有用)
      

  9.   

    To szhf1980 (s') :
    把你的代码贴上来看看