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.
作图时再去取得当前dc,CPaintDC dc(this);
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.
pDC=GetDC();
pDC->....
我在对话框初始化时传了个主控程序的cdc*
但在对话框里我用这个cdc*画是无效的.
我不知到为什么,主控程序的cdc*难道是在变的?
您是否在对话中正确地完成了主控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);}
“我不知到为什么,主控程序的cdc*难道是在变的?”你已经有答案了!CDC指针是一个暂态指针,不能长期保留。除非注册类时选择了私有Context选项。
talcon_hu :有啊,你是不是怀疑我是画成功了,又被覆盖了。
这我也想过,但不可能。我检测过了,没有调用它
xaocao(小草) :你的代码从道理上讲,跟我的一样啊。都是传CDC*
另外,引用它和传指针应该是一样的啊,毕竟引用的实现是靠指针的.
你可以在主对话框中写一个函数,或其它。
当要做事情的时候,你可以由子对话框发一个消息给主对话框,在消息处理里再调用函数。就可以了。这一过程还可以演变成其它类事过程。
函数里可以用getdc等任何函数,用来作图。
// 点击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);
}
谢谢
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);
}
而且你这样画是低30个象素的(工具栏高)