Question:
How to display rectangle in C++?
2007-12-03 15:36:51 UTC
I am somewhat familiar with how to display a circle in C++. I have no idea where to even start with displaying a rectangle, however. Can anyone give me an idea of how to get this started or what needs to be included to make this work?
Four answers:
DukeInstinct
2007-12-04 19:58:25 UTC
If your creating a windows application you can add something like this:



RECT rc;

HDC hdc=GetDC( Your window handle(HWND) );

rc.left= (X coordinate of top left corner)

rc.top= (Y coordinate of top left corner)

rc.right= (X coordinate of bottom right corner)

rc.botton= (Y coordinate of bottom right corner)

FillRect(hdc,&rc,

CreateSolidBrush((Red amount),

(Green amount),(Blue amount));



Example:



RECT rc;

HDC hdc=GetDC(hwnd);

rc.left=5;

rc.top=5;

rc.right=20;

rc.bottom=20;

FillRect(hdc,&rc,

CreateSolidBrush(0,0,255);



This will create a filled area in a form of a rectangle you can also do this do create a rectangle with it's outline drawn by the current pen in the hdc and filled the current brush in the hdc:



HDC hdc=GetDC( Window handle );

HPEN pen;

HBRUSH brush;

pen=CreatePen((PenStyle),(PenWidth),(Color));

brush=CreateSolidBrush((Color));

SelectObject(hdc,pen);

SelectObject(hdc,brush);

Rectangle(hdc,(Top left X coordinate),(Top left Y coordinate),(Bottom right X coordinate),(Bottom right Y coordinate));



Example:

HDC hdc=GetDC(Hwnd);

HPEN pen;

HBRUSH brush;

pen=CreatePen(PS_SOLID,1,

RGB(0,255,0));

brush=CreateSolidBrush(

RGB(0,0,255));

SelectObject(hdc,pen);

SelectObject(hdc,brush);

Rectangle(hdc,5,5,10,10);



This would create a rectangle that has an green outline and blue fill.



Other pen styles include PS_DASH,PS_DOT,PS_DASHDOT,

PS_DASHDOTDOT,and PS_NULL.



There are also more than one function for creating a brush which allow different effects when being used to

draw something which include

CreateHatchBrush(HatchStyle,

color); which creates a pattern and

CreatePatternBrush(Bitmap handle(HBITMAP));

which creates a pattern using a 8x8 bitmap.



Hatch styles include HS_BDIAGONAL,HS_CROSS,

HS_DIAGCROSS,HS_FDIAGONAL,

HS_HORIZONTAL,HS_VERTICAL.
schaner
2016-12-10 16:37:23 UTC
You stated setw, and that i think of that's a thank you to bypass. utilising that, alongside with setfill, makes this gorgeous undemanding, saving you the hassle of coding the nested loops your self. See under for a fashion I did it. i'm uncertain in case you needed a hollow rectangle or sturdy, so I made it hollow. It should not be confusing to fill it in, if want be. #comprise #comprise #comprise #comprise utilising namespace std; const char image('X'); const char spacer(' '); const int minSide = 2; const int maxSide = 10; int substantial(int argc, char *argv[]) {     int x, y, w, h;     string in;     stringstream ss;     do {         cout << "enter rectangle dimensions ( x y, in variety ["                   << minSide << ".." << maxSide << "] ) : ";         getline(cin,in);         ss.sparkling(); ss.str(in);     } on an analogous time as ((!(ss >> x >> y)) || (x < minSide) || (x > maxSide)                       || (y < minSide) || (y > maxSide));     cout << endl;     // Width may be the extra advantageous of the two dimensions     if (x >= y) {         w = x; h = y;     } else {         h = x; w = y;     }     // show suitable     cout << setfill(image) << setw(w+a million) << 'n';     // show center     for (int i = 0; i < h-2; i++) {         cout << image << setw(w-a million) << setfill(spacer) << image << endl;     }     // show backside     cout << setfill(image) << setw(w+a million) << 'n';     return 0; } #if 0 pattern run: enter rectangle dimensions ( x y, in variety [2..10] ) : 4 8 XXXXXXXX X           X X           X XXXXXXXX #endif
sun
2007-12-03 15:42:24 UTC
well if u just simply wonna display it just use cout and setw codes thats simply makes a ractangle by the way how did u do the circle?
nb_philly
2007-12-03 15:47:28 UTC
platform wasn't specified, but try System.Drawing. Graphics.DrawRectangle on the .NET framework


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...