2014-08-10 09:53:34|?次阅读|上传:huigezrx【已有?条评论】发表评论
关键词:C/C++, 界面设计, MFC|来源:唯设编程网
在对话框类中添加窗体绘制函数SetupRegion,该函数的实现代码如下:
void CCEllipseWndDlgDlg::SetupRegion(CDC *pDC, CBitmap &cBitmap, COLORREF TransColor) { CDC memDC; CBitmap *pOldMemBmp=NULL; BITMAP bit; CRect rect; GetWindowRect(rect); CRgn wndRgn; //创建与传入DC兼容的临时DC memDC.CreateCompatibleDC(pDC); //取得位图参数,这里要用到位图的长和宽 cBitmap.GetBitmap (&bit); //将位图选入临时DC pOldMemBmp=memDC.SelectObject(&cBitmap); //创建总的窗体区域,初始region为0 wndRgn.CreateRectRgn(0,0,rect.Width(),rect.Height()); for(int y=0;y<rect.Height()+1;y++) { CRgn rgnTemp; //保存临时region int iX = 0; do { //等于透明色时,消除后透明色找到下一个非透明色的点. if (memDC.GetPixel(iX, y) == TransColor) { rgnTemp.CreateRectRgn(iX,y,iX+1,y+1); //合并到主"region". wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR); //删除临时"region",否则下次创建时和出错 rgnTemp.DeleteObject(); } iX++; }while(iX <rect.Width()+1); iX = 0; } if(pOldMemBmp) memDC.SelectObject(pOldMemBmp); SetWindowRgn(wndRgn,TRUE); SetForegroundWindow(); }
void CCEllipseWndDlgDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } //新添加代码 CRect rtWindow; GetWindowRect(&rtWindow); CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CDC picDC; picDC.CreateCompatibleDC (&dc);//创建图形设备 CBitmap *pOldBmp; pOldBmp = picDC.SelectObject (&m_bmp);//选择图形设备 BITMAP bm; m_bmp.GetBitmap(&bm);//获得位图 //显示图象 dc.BitBlt (-3,-3,rtWindow.Width(),rtWindow.Height(),&picDC,0,0,SRCCOPY); }
到此,一个不规则窗体的对话框程序就完成了,编译运行,最终得到的结果如下: