1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Urgent: Coloring Drawn Hexagons

Discussion in 'MFC' started by chadz, Nov 9, 2009.

  1. chadz

    chadz New Member

    hello i am a beginner in using MFC,
    i drew a hexagon in mfc using this code: ( a part of the code)
    m_StartPoint.x -= int(ceil(r));//50,100
    m_EndPoint.y -= int(ceil(h)); //100,99

    dc.MoveTo(m_StartPoint);
    dc.LineTo(m_EndPoint);

    m_EndPoint.y -= int(ceil(s)); //m_Endpoint:100,49
    dc.LineTo(m_EndPoint);

    m_EndPoint.y -= int(ceil(h));
    m_EndPoint.x -= int(ceil(r));
    dc.LineTo(m_EndPoint);

    m_EndPoint.y += int(ceil(h));
    m_EndPoint.x -= int(ceil(r));
    dc.LineTo(m_EndPoint);

    m_EndPoint.y += int(ceil(s));
    dc.LineTo(m_EndPoint);

    m_EndPoint.y += int(ceil(h));
    m_EndPoint.x += int(ceil(r));
    dc.LineTo(m_EndPoint);



    and it looks like the picture "hexagon.jpg"

    the question is how can i fill it with color, lets say in red? or even more how can i fill it with a small picture instead of coloring it?

    Any idea?
    thank you
     

    Attached Files:

  2. Gene Poole

    Gene Poole New Member

    For solid color, use CDC::FloodFill(). For an image, you'll have to set a clipping region to the shape of your hexagon and bitblt the image.
     
  3. chadz

    chadz New Member

    thx, but can u tell me how do i do that :set a clipping region to the shape of your hexagon and bitblt the image?

    can you tell me what should i add in this code:
    ----------------------------------------------------
    Code:
    CClientDC dc(pntWnd);
       
      
       CPoint m_StartPoint = crdSouthEast;//(100,100)
       CPoint m_EndPoint = crdSouthEast;
       CPoint Temp;
       computeHexagonQuantities(hexagonSide);//
    
    	POINT pts[6];
    	
    		
    //Drawing
    	m_StartPoint.x -= int(ceil(r));
    
    	int temp=int(ceil(r));
    	m_EndPoint.x =m_StartPoint.x+temp;
    	m_EndPoint.y -= int(ceil(r)); 
    
    	pts[0]=m_StartPoint;
    	pts[1]=m_EndPoint;
    
    	m_EndPoint.x=m_StartPoint.x;
    	m_EndPoint.y-= int(ceil(r));
    		
    	pts[2]=m_EndPoint;
     
    	m_EndPoint.x-=int(ceil(s));
    	
    	pts[3]=m_EndPoint;
    	
    	Temp.x=m_EndPoint.x;
    	m_EndPoint.x-=int(ceil(r));
            m_EndPoint.y+= int(ceil(r));
    	
    
    	pts[4]=m_EndPoint;
    	
    
    	m_EndPoint.x = Temp.x;
    	m_EndPoint.y+= int(ceil(r));
    	
    
    	pts[5]=m_EndPoint;
    	
    
    	m_EndPoint.x =m_StartPoint.x;
    	m_EndPoint.y =m_StartPoint.y;
    	
    
             dc.Polygon(pts,6);
    
    //PS
    //i loaded a bitmap in another cpp file by doing : 
      CBitmap test;
      test.LoadBitmapW(IDB_BITMAP7);//IDB_BITMAP2
    
    //this "test" is passed by reference in the class containing the above code...
    //so how to clipper and BitBlt it?
    
    thank you for your help
     
    Last edited by a moderator: Nov 11, 2009
  4. Gene Poole

    Gene Poole New Member

    This paints a bitmap defined in resources as IDB_BITMAP1 into a triangular clipping region on the screen:

    Code:
    	  CPaintDC dc(this);
    	  POINT pts[3];
    	  CRgn rgn1;
    	  CDC memdc;
    	  CBitmap cbm;
    	  BITMAP bmp;
    	  
    	  pts[0].x=10;
    	  pts[0].y=100;
    	  
    	  pts[1].x=60;
    	  pts[1].y=20;
    	  
    	  pts[2].x=100;
    	  pts[2].y=100;
    	  
    	  //dc.Polygon(pts,3);
    	  rgn1.CreatePolygonRgn(pts,3,ALTERNATE);
    	  dc.SelectClipRgn(&rgn1);
    	  memdc.CreateCompatibleDC(&dc);
    	  
    	  cbm.LoadBitmap(IDB_BITMAP1);
    	  cbm.GetBitmap(&bmp);
    	  CBitmap *tbmp=memdc.SelectObject(&cbm);
    	  dc.BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&memdc,0,0,SRCCOPY);
    	  
    	  memdc.SelectObject(tbmp);
    
    
     
  5. chadz

    chadz New Member

    Thx a Lot..Still one more question , i have the following attached bitmap , when i use the code u gave me, it doesn't fit this bitmap in Each hex, it spread it along as many hex i am drawing... check" Result.jpg" to see what i mean.
    how can i fit this bitmap in one hex, because i want to fit other bitmaps in the other hexes
    thank you for the help.
     

    Attached Files:

  6. Gene Poole

    Gene Poole New Member

    Between drawing each hexagon, call SelectClipRgn(NULL) to reset the clipping region, then call it again with the new hex region.
     
  7. chadz

    chadz New Member

    thx a lot for ur help... but i tried that and it didnt work...
    if u dun mind ill show u my code tell me how exactly should i do it , and thx in advance but its just i need to fix it before wednesday and its not working...

    i have a class called childview.cpp it has sthg like:
    .
    .
    .
    .//code
    .
    .
    Code:
    void CChildView::drawMap(CDC * pdc)
    {
       Hexagon obj;
    
    
       CPoint point(600, 150);
      
       CClientDC dc(this);
       Dimensions dStep;
       double hexagonSide = 40;//side of one edge
       double r=((cos(30*PI/180))*hexagonSide);
       int r2=int (ceil(r));
       int NbOfHexes=3;
        int shiftByX=hexagonSide+r2;
        
        
          CBitmap test;
         test.LoadBitmapW(IDB_BITMAP7);//IDB_BITMAP2
        
    
         COLORREF RGB=RGB(255,0,255);//RGB object
       //**** Starting hex
        dStep = obj.draw(pdc, point, hexagonSide,test,RGB,RandColorsVec);
    ..//then changing coordinate of points then drawing again
      dStep = obj.draw(pdc, point, hexagonSide,test,RGB,RandColorsVec);
    //etc....
    
    
    //and the method .draw is in a class called hexagons.cpp and is as follows:
    Dimensions Hexagon::draw(CDC * pdc, 
        CPoint crdSouthEast, double hexagonSide,CBitmap& test,COLORREF RGB,vector <COLORREF> *RandColorsVec)
    {    
    
    
       
       Dimensions dResult;
       CPoint m_StartPoint = crdSouthEast;//(100,100)
       CPoint m_EndPoint = crdSouthEast;
       CPoint Temp;
       computeHexagonQuantities(hexagonSide);//
    
        POINT pts[6];//set of vertices of the hex
        
        
        
    //Drawing
        m_StartPoint.x -= int(ceil(r));
    
        int temp=int(ceil(r));
        m_EndPoint.x =m_StartPoint.x+temp;
        m_EndPoint.y -= int(ceil(r)); 
     
    
    
    
        pts[0]=m_StartPoint;
        pts[1]=m_EndPoint;
    
        m_EndPoint.x=m_StartPoint.x;
        m_EndPoint.y-= int(ceil(r));
    
        
        pts[2]=m_EndPoint;
     
    
        m_EndPoint.x-=int(ceil(s));
        
        pts[3]=m_EndPoint;
    
        
        Temp.x=m_EndPoint.x;
        m_EndPoint.x-=int(ceil(r));
    
        m_EndPoint.y+= int(ceil(r));
        //dc.LineTo(m_EndPoint);
    
        pts[4]=m_EndPoint;
    
    
        m_EndPoint.x = Temp.x;
        
        m_EndPoint.y+= int(ceil(r));
        //dc.LineTo(m_EndPoint);
    
        pts[5]=m_EndPoint;
    
    
    
        m_EndPoint.x =m_StartPoint.x;
        m_EndPoint.y =m_StartPoint.y;
        
    
       pdc->Polygon(pts,6);
        
    
       dResult.height = int(ceil(b));
       dResult.width = int(ceil(a));
       dResult.h = int(ceil(h));
       dResult.r = int(ceil(r));
       
      
       return dResult;
       
    }
    
    
    //so can u add the codes for me plz? thx again
     
    Last edited by a moderator: Nov 16, 2009
  8. Gene Poole

    Gene Poole New Member

    Sorry, I can't do much with that code. There are several classes that I have no idea what they do and I don't know how or where you are calling this function. I don't see any use of CRgn or SetClipRgn in this code snippit either.
     

Share This Page