获得对话框的HDC, HDC hdc = GetDC(hDlg); 再用BitBlt在这个hdc中指定坐标贴图即可。BitBlt函数原形如下: BOOL BitBlt( HDC hdcDest, // handle to destination DC int nXDest, // x-coord of destination upper-left corner int nYDest, // y-coord of destination upper-left corner int nWidth, // width of destination rectangle int nHeight, // height of destination rectangle HDC hdcSrc, // handle to source DC int nXSrc, // x-coordinate of source upper-left corner int nYSrc, // y-coordinate of source upper-left corner DWORD dwRop // raster operation code );
如果是MFC对话框程序,默认就已经生成了这个代码,你稍微改改.
HDC hdc = GetDC(hDlg);
再用BitBlt在这个hdc中指定坐标贴图即可。BitBlt函数原形如下:
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
如果是MFC,记得用::GetDC
比如你要用来获取窗口DC
HDC hDC=::GetDC(m_hWnd);//记得要用ReleaseDC释放.
不不推荐这种方式,还是在OnPaint里绘制更合理.
void CMainDlg::OnPaint()
{
if (IsIconic())
{
......//MFC自动生成的代码若干,这里不列出
}
else
{
CPaintDC dc(this);
dc...... //你可以用这个dc来绘制了
}
}
这个hDlg表示对话框的句柄,为HWND类型。
{
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();
CPaintDC dc(this); CPoint pt1,pt2;
pt1.x=5;pt1.y=5;
pt2.x=100;pt2.y=100;
dc.MoveTo(pt1.x,pt1.y);
dc.LineTo(pt2.x,pt2.y);
}
}