Turbo C Graphics - setlinestyle function
setlinestyle function is used to set the linestyle and it accepts 3 parameters as input. The first one is the line style it can be SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE or USERBIT_LINE. The 2nd argument is to use the user bit pattern and the last one is line thickness it can be 1 pixel or 3 pixel wide.
getlinesettings function is used to get the values set by setlinestyle function. It takes the address of the struct linesettingstype as input and fills the struct on its return.
I have used DASHED_LINE and DOTTED_LINE in the given sample. You can see the output screen where two diagonal lines are drawn in a small square box.
// SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE
setlinestyle(DASHED_LINE, 1, 1);
line(left + 20, top + 20, left + 70, top + 70);
setlinestyle(DOTTED_LINE, 1, 3);
line(left + 70, top + 20, left + 20, top + 70);
getlinesettings(&lstype);
sprintf(msg, "linestyle: %d thickness: %d getx : %d, gety: %d", lstype.linestyle, lstype.thickness, getx(), gety());
Back to Turbo C Graphics Index
Source Code
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <dos.h>
#include <stdlib.h>
int InitGraphics()
{
int grd, grm;
int gresult;
// Detect the graphics driver and mode
detectgraph(&grd,&grm);
// initialize the graphics mode with initgraph
initgraph(&grd, &grm, "");
gresult = graphresult();
if(gresult != grOk)
{
printf(grapherrormsg(gresult));
getch();
return -1;
}
// set the background color
setbkcolor(RED);
// set the foreground color
setcolor(WHITE);
// draw a white color border with rectangle
rectangle(0,0,getmaxx(),getmaxy());
return 1;
}
void main()
{
int i, x, y, w;
int left,top,right,bottom;
int margin, width, height;
int cx, cy;
char msg[512];
unsigned char patternbuffer[8];
unsigned char upattern_solid[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
unsigned char upattern[8] = { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 };
struct fillsettingstype fstype;
struct linesettingstype lstype;
if(InitGraphics() == -1)
return;
margin = 100; // 100 pixel margin
left = margin;
top = margin;
bottom = getmaxy() - margin;
right = getmaxx() - margin;
// draw a bar with user defined solid fill on blue color
// use rectangle to have a white color border
setfillstyle(SOLID_FILL, WHITE);
setfillpattern(upattern_solid, BLUE);
getfillpattern(patternbuffer);
getfillsettings(&fstype);
sprintf(msg, "pattern id: %d color: %d buffer: %d", fstype.pattern, fstype.color, patternbuffer[0]);
bar(left, top, right, bottom);
rectangle(left, top, right, bottom);
//another inner rectangle drawn with the use of moveto and lineto functions
setcolor(YELLOW);
moveto(left + 5, top + 5);
lineto(right - 5, top + 5);
lineto(right - 5, bottom - 5);
lineto(left + 5, bottom - 5);
lineto(left + 5, top + 5);
//another inner square drawn with the use of moverel and linerel functions
setcolor(WHITE);
moveto(left, top);
moverel(20, 20);
linerel(50, 0);
linerel(0, 50);
linerel(-50, 0);
linerel(0, -50);
// draw two lines by crossing above square
// SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE
setlinestyle(DASHED_LINE, 1, 1);
line(left + 20, top + 20, left + 70, top + 70);
setlinestyle(DOTTED_LINE, 1, 3);
line(left + 70, top + 20, left + 20, top + 70);
getlinesettings(&lstype);
// find the text width and text height
width = textwidth(msg);
height = textheight(msg);
// calculate the right and bottom with margin and getmaxx and getmaxy function
// change the text foreground color to yellow
setcolor(YELLOW);
cx = (right + left - width) / 2;
cy = (bottom + top - height) / 2;
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
outtextxy(cx,cy,msg);
setcolor(WHITE);
moveto(100, 100);
sprintf(msg, "linestyle: %d thickness: %d getx : %d, gety: %d", lstype.linestyle, lstype.thickness, getx(), gety());
width = textwidth(msg);
cx = (right + left - width) / 2;
cy += textheight(msg) + 10;;
// use moveto and outtext function instead of outtextxy
moveto(cx, cy);
outtext(msg);
getch();
closegraph();
}
Output
|