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

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

 

IDB_BG 是位图资源的ID

方法一


  1. OnEraseBkgnd(CDC* pDC)   
  2. {  
  3.  // 设置画刷为希望的背景  
  4.  CDC backBmp;                 
  5.  backBmp.CreateCompatibleDC(pDC);  
  6.  CBitmap bitmap;  
  7.  bitmap.LoadBitmap(IDB_BG);  
  8.  CBrush m_brushBackground;  
  9.  m_brushBackground.CreatePatternBrush(&bitmap);    ///创建位图画刷  
  10.  CBrush* pOldMemDCBrush=pDC->SelectObject(&m_brushBackground);   
  11.  pDC->PatBlt(0,0,m_ScrollRect.Width(),m_ScrollRect.Height(),PATCOPY);  
  12.  pDC->SelectObject(pOldMemDCBrush);  
  13.  return TRUE;  
  14. }  

方法二

  1. OnEraseBkgnd(CDC* pDC)   
  2. {  
  3.  CBitmap m_pMap;  
  4.  m_pMap.LoadBitmap(IDB_BG);  
  5.  CRect rect;  
  6.  GetClientRect(&rect);   
  7.  BITMAP bm;  
  8.  CDC dcMem;  
  9.  m_pMap.GetObject(sizeof(BITMAP),&bm);  
  10.  dcMem.CreateCompatibleDC(pDC);   
  11.  CBitmap* pOldBitmap = dcMem.SelectObject(&m_pMap);  
  12.  for (register int nX=0;nX<rect.right-rect.left;nX+=bm.bmWidth)  
  13.   for(int nY=0;nY<rect.bottom-rect.top;nY+=bm.bmHeight)  
  14.  pDC->BitBlt(nX,nY,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);  
  15.  dcMem.SelectObject(pOldBitmap);   
  16.  return TRUE;  
  17. }  

方法三

  1. OnEraseBkgnd(CDC* pDC)   
  2. {  
  3.  CBitmap bmp;  
  4.  bmp.LoadBitmap(IDB_BG); ///加载位图  
  5.  CBrush m_brushBackground;  
  6.  m_brushBackground.CreatePatternBrush(&bmp);    ///创建位图画刷  
  7.  CRect rect;  
  8.  GetClientRect(&rect);   
  9.  pDC->FillRect(rect,&m_brushBackground); //用背景画填充区域  
  10.  return TRUE;  
  11. }  

参考资料:

http://blog.163.com/tiger_zl2005/blog/static/5632972006631231250/

#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、付费专栏及课程。

余额充值