9.18 晴
实习了2个半月了,期间遇到很多技术问题,基本都能找到,但是许多经过一段时间又忘了,所以决定开个博客,记录点滴技术性问题,以后再遇问题自己可来此找答案。
1、mfc窗口不间断刷新时,窗口会一直闪。在类向导消息函数上找到OnEraseBkgnd,添加,并在函数里return false。
2、如何将CDC的图片保存下来,以下代码是保存在图片控件已经处理过的图像。
CDC* pDC = GetDlgItem( ID ) ->GetDC(); // 获得显示控件的 DC HDC hDC = pDC ->GetSafeHdc(); // 获取 HDC(设备句柄) 来进行绘图操 CRect rect; GetDlgItem(ID) ->GetClientRect( &rect ); int rw = rect.right - rect.left; // 求出图片控件的宽和高 int rh = rect.bottom - rect.top; int iw = img->width; // 读取图片的宽和高 int ih = img->height; int tx = (int)(rw - iw)/2; // 使图片的显示位置正好在控件的正中 int ty = (int)(rh - ih)/2; SetRect( rect, tx, ty, tx+iw, ty+ih ); CvvImage cimg; cimg.CopyOf( img ); // 复制图片 cimg.DrawToHDC( hDC, &rect ); // 将图片绘制到显示控件的指定区域内 if(m_bDraw) { pDC->MoveTo(1,1); pDC->LineTo(100,100); } ATL::CImage image; image.Create(220,220,24);//图像的长和宽 //CDC *pDC=GetDC(); CDC dstDC; dstDC.CreateCompatibleDC(pDC); dstDC.SelectObject(image); dstDC.StretchBlt(0,0,220,220,pDC,0,0,220,220,SRCCOPY); image.Save(_T("1.jpg")); ReleaseDC( pDC );3.获取控件在窗口的位置
CRect rect;
GetDlgItem(控件ID)->GetWindowRect(&rect);//获取控件的屏幕坐标
ScreenToClient(&rect);//转换为对话框上的客户坐标
4.mfc改变控件的大小与位置
- CWnd *pWnd;
- pWnd = GetDlgItem( IDC_BUTTON1 ); //获取控件指针,IDC_BUTTON1为控件ID号
- void MoveWindow(int x,int y,int nWidth,int nHeight);
- void MoveWindow(LPCRECT lpRect);