# Urgent: Coloring Drawn Hexagons

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

Joined:
Nov 9, 2009
Messages:
4
0
Trophy Points:
0
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

File size:
1.7 KB
Views:
522
2. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
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.

Joined:
Nov 9, 2009
Messages:
4
0
Trophy Points:
0
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;

//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=m_StartPoint;
pts=m_EndPoint;

m_EndPoint.x=m_StartPoint.x;
m_EndPoint.y-= int(ceil(r));

pts=m_EndPoint;

m_EndPoint.x-=int(ceil(s));

pts=m_EndPoint;

Temp.x=m_EndPoint.x;
m_EndPoint.x-=int(ceil(r));
m_EndPoint.y+= int(ceil(r));

pts=m_EndPoint;

m_EndPoint.x = Temp.x;
m_EndPoint.y+= int(ceil(r));

pts=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;

//this "test" is passed by reference in the class containing the above code...
//so how to clipper and BitBlt it?
```

Last edited by a moderator: Nov 11, 2009
4. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
This paints a bitmap defined in resources as IDB_BITMAP1 into a triangular clipping region on the screen:

Code:
```	  CPaintDC dc(this);
POINT pts;
CRgn rgn1;
CDC memdc;
CBitmap cbm;
BITMAP bmp;

pts.x=10;
pts.y=100;

pts.x=60;
pts.y=20;

pts.x=100;
pts.y=100;

//dc.Polygon(pts,3);
rgn1.CreatePolygonRgn(pts,3,ALTERNATE);
dc.SelectClipRgn(&rgn1);
memdc.CreateCompatibleDC(&dc);

cbm.GetBitmap(&bmp);
CBitmap *tbmp=memdc.SelectObject(&cbm);
dc.BitBlt(0,0,bmp.bmWidth,bmp.bmHeight,&memdc,0,0,SRCCOPY);

memdc.SelectObject(tbmp);

```

Joined:
Nov 9, 2009
Messages:
4
0
Trophy Points:
0
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.

File size:
350.1 KB
Views:
431
File size:
32.3 KB
Views:
454
6. ### Gene PooleNew Member

Joined:
Nov 10, 2009
Messages:
93
5
Trophy Points:
0
Between drawing each hexagon, call SelectClipRgn(NULL) to reset the clipping region, then call it again with the new hex region.

Joined:
Nov 9, 2009
Messages:
4
0
Trophy Points:
0
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;

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;//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=m_StartPoint;
pts=m_EndPoint;

m_EndPoint.x=m_StartPoint.x;
m_EndPoint.y-= int(ceil(r));

pts=m_EndPoint;

m_EndPoint.x-=int(ceil(s));

pts=m_EndPoint;

Temp.x=m_EndPoint.x;
m_EndPoint.x-=int(ceil(r));

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

pts=m_EndPoint;

m_EndPoint.x = Temp.x;

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

pts=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

Joined:
Nov 10, 2009
Messages:
93