在做一个MFC程序时遇到问题,需要用这个函数画个图void workman::display(CDC* pDC)
//…………
Graphics graphics(pDC->m_hDC);
//…………就是需要传个值进去,只用到了m_hDC,请问下main函数怎么写呢,下面的老报错,说#include "stdafx.h"
#include "workman.h"
#include <sstream>
#include <Windows.h>//独一无二的类名,一般用GUID字串,以免与其他程序的类名重复
static const char * CLASS_NAME = "{198CEAB2-AD78-4ed3-B099-247639080CB0}";void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
CDC* pDC;
workman* w1;/************************************************************************
    回调函数,当主窗口收到任何Windows消息时被调用
************************************************************************/
LRESULT CALLBACK onMainWndMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_DESTROY:
        PostQuitMessage(0); //如果是“窗口销毁”事件,则应该在消息队列中投递
        break;              //一个WM_QUIT消息,使GetMessage()返回FALSE
case WM_PAINT:
{
        //hWnd=GetSafeHwnd(); 
OnPaint(hWnd,msg,wParam,lParam);                                //指示这一行报错
break;
}
    default:
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}
/************************************************************************
画图方法
************************************************************************/
void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
    
//PAINTSTRUCT ps;
HDC hDC;
//hDC=BeginPaint(hWnd,&ps);
hDC=GetDC(hWnd);
pDC->m_hDC=hDC;
w1->display(pDC);
//TextOut(hDC,50,50,"Hello",6);
//EndPaint(hWnd,&ps);}
/************************************************************************
登记自己的窗口类
************************************************************************/
bool registerMyClass() {
    WNDCLASSEX  wce = {0};
    wce.cbSize          = sizeof(wce);
    wce.style           = CS_VREDRAW | CS_HREDRAW;
    wce.lpfnWndProc     = &onMainWndMessage;  //指明回调函数
    wce.hInstance       = GetModuleHandle(0);
    wce.hIcon           = LoadIcon(0, MAKEINTRESOURCE(IDI_WINLOGO));
    wce.hCursor         = LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW));
    wce.hbrBackground   = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);
    wce.lpszClassName   = CLASS_NAME; //独一无二的类名
    wce.hIconSm         = wce.hIcon;
    return 0!=RegisterClassEx(&wce);
}/************************************************************************
创建并显示主窗口
************************************************************************/
bool createMyWindow(int cmdShow) {
    HWND mainWnd = CreateWindowEx(0, CLASS_NAME, "Demo", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        0, 0, GetModuleHandle(0), 0);
    if (0!=mainWnd) {
        ShowWindow(mainWnd, cmdShow);
        UpdateWindow(mainWnd);
        return true;
    } else {
        return false;
    }
}/************************************************************************
消息循环
************************************************************************/
int messageLoop() {
    MSG msg;
    while (GetMessage(&msg, 0, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return static_cast<int>(msg.wParam);
}/************************************************************************
WinMain,程序入口
************************************************************************/
int WINAPI WinMain(HINSTANCE, HINSTANCE, char *, int cmdShow) {
    if (registerMyClass() && createMyWindow(cmdShow)) {
        HWND hWnd;
ShowWindow(hWnd,cmdShow);
UpdateWindow(hWnd);
return messageLoop();
    } else {
        std::ostringstream msg;
        msg << "创建主窗口失败,错误代码:" << GetLastError();
        MessageBoxA(0, msg.str().c_str(), 0, MB_OK | MB_ICONSTOP);
        return 0;
    }

解决方案 »

  1.   

    HDC GetWindowDC(
      HWND hWnd   // handle to window
    );
      

  2.   

    CDC是MFC封装的类,你还是使用操作HDC相关的API函数吧
      

  3.   

    void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
      HDC hDC = GetWindowDC(hWnd);
      Graphics graphics(hDC);
    }
      

  4.   

    你把创建窗口得到的hWnd做全局变量传进入就行
      

  5.   

    你把创建窗口得到的hWnd做全局变量传进入就行,替换GetDC