我作了个非模态的对话框,但有时在对话框里想对主控程序上画点东西,不知该怎么作.
  我在初始化时传了个主控程序的CDC* pDC ,但是不能在主控程序视图里画东西,不知为什么?谢

解决方案 »

  1.   

    两个地方的dc不一样。
    作图时再去取得当前dc,CPaintDC dc(this);
      

  2.   

    Draw picture in any windows,the first setp is getdc(),use this api function get the device  handle.
    The csdn describe like this :GetDC
    The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context. The GetDCEx function is an extension to GetDC, which gives an application more control over how and whether clipping occurs in the client area. HDC GetDC(
      HWND hWnd   // handle to a window
    );
     
    Parameters
    hWnd 
    Handle to the window whose device context is to be retrieved. If this value is NULL, GetDC retrieves the device context for the entire screen. 
    Windows 98, Windows NT 5.0 and later: If this parameter is NULL, GetDC retrieves the device context for the primary display monitor. To get the device context for other display monitors, use the EnumDisplayMonitors and CreateDC functions. Return Values
    If the function succeeds, the return value identifies the device context for the specified window's client area. If the function fails, the return value is NULL. Windows NT: To get extended error information, callGetLastError.Res
    The GetDC function retrieves a common, class, or private device context depending on the class style specified for the specified window. For common device contexts, GetDC assigns default attributes to the device context each time it is retrieved. For class and private device contexts, GetDC leaves the previously assigned attributes unchanged. After painting with a common device context, the ReleaseDC function must be called to release the device context. Class and private device contexts do not have to be released. The number of device contexts is limited only by available memory.
      

  3.   

    CDC* pDC;
    pDC=GetDC();
    pDC->....
      

  4.   

    主控dc也不是对话框dc当然画不上了。同意楼上的。
      

  5.   

    可能我没说清楚,我在对话框里发个动作,要在主控程序里话东西,
    我在对话框初始化时传了个主控程序的cdc* 
    但在对话框里我用这个cdc*画是无效的.
    我不知到为什么,主控程序的cdc*难道是在变的?
      

  6.   

    您是否已经获取了主控程序的DC呢?
    您是否在对话中正确地完成了主控dc有引用?//-----------------------------------------------------------------
    //-       Dialog
    //-----------------------------------------------------------------
    CMyDialog::ControlToMainFram(CDC *pDC)
    {
     pDC->..
    }
    //---------------------------------------------------
    //        MainFram 
    //---------------------------------------------------
    CMainFram::OnDialogCreate()
    {
      CMyDialog *m_pDialog=new (CMyDialog *)CMyDialog();
      m_pDialog->Create(IDD_DIALOG1,this);
      m_pDialog->ShowWindow(SW_SHOW);  CDC* pDC;
      pDC=GetDC();
      m_pDialog->ControlToMainFram(pDC);}
      

  7.   


    “我不知到为什么,主控程序的cdc*难道是在变的?”你已经有答案了!CDC指针是一个暂态指针,不能长期保留。除非注册类时选择了私有Context选项。
      

  8.   

    virtualfunction : 这在书上没提过,可能是不断变的吧
    talcon_hu       :有啊,你是不是怀疑我是画成功了,又被覆盖了。
                      这我也想过,但不可能。我检测过了,没有调用它
    xaocao(小草)   :你的代码从道理上讲,跟我的一样啊。都是传CDC*
                      另外,引用它和传指针应该是一样的啊,毕竟引用的实现是靠指针的.
      

  9.   

    你想的方法不是不可能,只是很难得实现,这种方法不可取,应转换思维不要拘泥于此。
    你可以在主对话框中写一个函数,或其它。
    当要做事情的时候,你可以由子对话框发一个消息给主对话框,在消息处理里再调用函数。就可以了。这一过程还可以演变成其它类事过程。
    函数里可以用getdc等任何函数,用来作图。
      

  10.   

    传句柄,或者在主控程序中保存好CDC* 变量。
      

  11.   

    那为什么是不能实现呢,象virtualfunction 说的CDC 是经常变的.
      

  12.   

    老兄可如下完成(已在VC6下测试通过).//-------------Dialog------------------------------------------//
    //     点击Button按钮完成MainFrame DC 的引用
    //-------------------------------------------------------------//void CTestDCDialog::OnButton1() 
    {
    // TODO: Add your control notification handler code here
     CDC *pDC;
     pDC=AfxGetMainWnd()->GetDC();
     pDC->TextOut(100,0,"Good Morning!");
    }
    //------------------ MainFrame ----------------------------------
    //            
    //----------------------------------------------------------------
    void CMainFrame::OnTest() 
    {
          / / TODO: Add your command handler code here
             m_pDialog=(CTestDCDialog *)new CTestDCDialog();
    m_pDialog->Create(IDD_DIALOG1,this);
    m_pDialog->ShowWindow(SW_SHOW);
    }
      

  13.   

    谢谢。这个方法我试过了,((YourView*)AfxGetMainWnd())->GetActiveView()->GetDC(),这样效果更好。你的可能画到工具兰上去。我不明白的是在主控程序里传cdc* 是可以的,但传给dialog不行 ,是为什么。
    谢谢
      

  14.   

    这是对话框中一个按钮的代码
    void CTemp::OnButton1() 
    {
    CPen pen(PS_SOLID,2,RGB(255,0,0));
    CDC* pDC=AfxGetMainWnd()->GetDC();
    pDC->SelectObject(&pen);
    pDC->MoveTo(100,100);
    pDC->LineTo(200,200);
    }
      

  15.   

    是啊,如果把moveto去掉,就画到主控程序的工具栏上了.
    而且你这样画是低30个象素的(工具栏高)