VC 背景填充位图的几种方法

IDB_BG 是位图资源的ID

 

方法一
OnEraseBkgnd(CDC* pDC) 
{
 // 设置画刷为希望的背景
 CDC backBmp;               
 backBmp.CreateCompatibleDC(pDC);
 CBitmap bitmap;
 bitmap.LoadBitmap(IDB_BG);
 CBrush m_brushBackground;
 m_brushBackground.CreatePatternBrush(&bitmap);    ///创建位图画刷
 CBrush* pOldMemDCBrush=pDC->SelectObject(&m_brushBackground); 
 pDC->PatBlt(0,0,m_ScrollRect.Width(),m_ScrollRect.Height(),PATCOPY);
 pDC->SelectObject(pOldMemDCBrush);
 return TRUE;
}


方法二
OnEraseBkgnd(CDC* pDC) 
{
 CBitmap m_pMap;
 m_pMap.LoadBitmap(IDB_BG);
 CRect rect;
 GetClientRect(&rect); 
 BITMAP bm;
 CDC dcMem;
 m_pMap.GetObject(sizeof(BITMAP),&bm);
 dcMem.CreateCompatibleDC(pDC); 
 CBitmap* pOldBitmap = dcMem.SelectObject(&m_pMap);
 for (register int nX=0;nX<rect.right-rect.left;nX+=bm.bmWidth)
  for(int nY=0;nY<rect.bottom-rect.top;nY+=bm.bmHeight)
 pDC->BitBlt(nX,nY,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
 dcMem.SelectObject(pOldBitmap); 
 return TRUE;
}

方法三
OnEraseBkgnd(CDC* pDC) 
{
 CBitmap bmp;
 bmp.LoadBitmap(IDB_BG); ///加载位图
 CBrush m_brushBackground;
 m_brushBackground.CreatePatternBrush(&bmp);    ///创建位图画刷
 CRect rect;
 GetClientRect(&rect); 
 pDC->FillRect(rect,&m_brushBackground); //用背景画填充区域
 return TRUE;
}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include #include #include #include #include //////////////////////////////////////////////////////////////functions///////////////////////////////////////////////// void swap(float &m,float &n) { float temp=n; n=m; m=temp; } int sign(float a,float b)//sign() { int t; if(a>b) { t=1;} else if(adx) { swap(dx,dy); Flag=1; } else Flag=0; float Nerror=2*dy-dx; for(int i=1;i=0) { if(Flag) { x=x+sx;} else y=y+sy; Nerror=Nerror-2*dx; } if(Flag) { y=y+sy;} else x=x+sx; Nerror=Nerror+2*dy; } } ///////////////////////////////////四连通种子填充/////////////////////////////////////////////////// void BoundaryFill4(HDC hdc,int x,int y,int FilledColor,int BoundaryColor) { int CurrentColor; CurrentColor=GetPixel(hdc,x,y); if(CurrentColor!=BoundaryColor&&CurrentColor!=FilledColor) { SetPixel(hdc,x,y,FilledColor); BoundaryFill4(hdc,x+1,y,FilledColor,BoundaryColor); BoundaryFill4(hdc,x-1,y,FilledColor,BoundaryColor); BoundaryFill4(hdc,x,y+1,FilledColor,BoundaryColor); BoundaryFill4(hdc,x,y-1,FilledColor,BoundaryColor); } } ////////////////////////////////////////扫描线填充/////////////////////////////////////////////////// //DrawLine()函数:在(x1,y)和(x2,y)两点之间画一条颜色为FilledColor的横线(用来扫描填充) void drawline(HDC hdc, int x1, int x2,int y0, int FilledColor) { for(int n=x1+1;n<x2;n++) { SetPixel(hdc,n,y0,FilledColor); } } //Scan()函数:扫描线函数,将扫描线与形的交点坐标存在数组中 //数组中同行的点即为该行扫描线与形的交点(一般为2个) //数组中的行代表扫描线的纵坐标 void scan(HDC hdc, int boundarycolor) { int currentcolor; int a[300][2]={0}; for (int j=0;j<300;j++) { for(int i=300;i<700;i++) { currentcolor=GetPixel(hdc,i,j); if((currentcolor==boundarycolor)&&(GetPixel(hdc,i+1,j)!=boundarycolor)&&(i500)) {a[j][1]=i;} } } //利用循环调用DrawLine函数逐行填充两交点之间的点 for(int k=0;k<300;k++) { if((a[k][0]!=0)&&(a[k][1]!=0)) drawline(hdc,a[k][0],a[k][1],k,RGB(255,0,0));} } ///////////////////////////////////////////////边界填充////////////////////////////////////// //Contrary()取反函数:如果点的颜色为白,则将点置为填充色;如果点的颜色为填充色,则将点置为白色 //忽略了边界色,即不对边界点作颜色处理 void contrary(HDC hdc, int x, int y,int FilledColor) { for(int h=x;h<280;h++) { if(GetPixel(hdc,h,y)==RGB(255,255,255)) { SetPixel(hdc,h,y,FilledColor); } else if(GetPixel(hdc,h,y)==FilledColor) { SetPixel(hdc,h,y,RGB(255,255,255)); } } } //borderline()边线函数: 先找出形的边界 左边和右边,从右到左的顺序调用contrary()函数进行填充 void borderline(HDC hdc, int boundarycolor) { for(int j=280;j<499;j++) { for(int i=80;i100)) { contrary(hdc,i,j,RGB(0,0,255)); } } } for(int m=280;m<499;m++) { for(int n=80;n<280;n++) { int currentcolor=GetPixel(hdc,n,m); if((currentcolor==boundarycolor)&&(GetPixel(hdc,n+1,m)!=boundarycolor)&&(GetPixel(hdc,n-1,m)!=boundarycolor)&&(n<101)) { contrary(hdc,n,m,RGB(0,0,255)); } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值