saalek
مدیر بازنشسته
چون تاپيك آموزش سي++ خيلي بزرگ شده است، بايد چند انشعاب بزنيم.
و اين تاپيك قسمت گرافيك آن است.
آدرس تاپيك اصلي:
.
.
و اين تاپيك قسمت گرافيك آن است.
آدرس تاپيك اصلي:
.
.
#include<graphics.h>
#include<conio.h>
void main()
{
char c ;
menu:
// ----------- shoroe ghaaleb -------------
int d=DETECT,m;
initgraph(&d,&m,"c:\\tc\\bgi");
circle(320,240,92);
line(10,10,300,300);
// ----------- entehaye ghaaleb -----------
getch() ;
closegraph();
}
int d=DETECT,m;
initgraph(&d,&m,"c:\\tc\\bgi");
setcolor(9);
#include<graphics.h>
#include<conio.h>
void main()
{
char c ;
menu:
// ----------- shoroe ghaaleb -------------
int d=DETECT,m;
initgraph(&d,&m,"c:\\tc\\bgi");
setcolor(9);
outtextxy(320,25,"welcome");
setcolor(4);
circle(320,240,92);
circle(420,240,92);
setfillstyle(SOLID_FILL, 14);
floodfill(320,240,4);
// ----------- entehaye ghaaleb -----------
getch() ;
closegraph();
}
getbkcolor returns the current background color
setbkcolor sets the current background color using the palette
Declaration:
int far getbkcolor(void);
void far setbkcolor(int color);
setbkcolor(7);
getcolor returns the current drawing color
setcolor sets the current drawing color
Declaration:
int far getcolor(void);
void far setcolor(int color);
getpixel gets the color of a specified pixel
putpixel plots a pixel at a specified point
Declaration:
unsigned far getpixel(int x, int y);
void far putpixel(int x, int y, int color);
Clears the graphics screen
Declaration: void far cleardevice(void);
Returns maximum x or y screen coordinate
Declaration:
int far getmaxx(void);
int far getmaxy(void);
For example, on a CGA in 320 x 200 mode, getmaxx returns 319 and getmaxy
returns 199.
#include<graphics.h>
#include<conio.h>
[color=red]#include<stdlib.h>[/color]
void main()
{
char c ;
menu:
// ----------- shoroe ghaaleb -------------
int d=DETECT,m;
initgraph(&d,&m,"c:\\tc\\bgi");
int my_x;
int my_y;
char x_string[7];
char y_string[7];
my_x= getmaxx();
my_y= getmaxy();
[color=red]itoa(my_x,x_string,10);[/color]
[color=red]itoa(my_y,y_string,10);[/color]
outtextxy(320,25,”x=”,x_string);
outtextxy(320,35,y_string);
// ----------- entehaye ghaaleb -----------
getch() ;
closegraph();
}
[color=blue]outtext[/color] displays a string in the viewport (graphics mode)
[color=blue]outtextxy[/color] displays a string at the specified location (graphics mode)
Declaration:
void far outtext(char far *textstring);
void far outtextxy(int x, int y, char far *textstring);
Remarks:
outtext and outtextxy display a text string, using the current justification
settings and the current font, direction, and size.
outtext outputs textstring at the current position (CP)
outtextxy displays textstring in the viewport at the position (x, y)
ـــــــــــــــــــــــ
line, linerel, lineto <GRAPHICS.H>
------------------------
line draws a line between two specified points
linerel draws a line a relative distance from the current position (CP)
lineto draws a line from the current position (CP) to (x,y)
Declaration:
void far line(int x1, int y1, int x2, int y2);
void far linerel(int dx, int dy);
void far lineto(int x, int y);
Remarks:
line draws a line from (x1, y1) to (x2, y2) using the current color, line
style, and thickness. It does not update the [color=red]current position (CP)[/color].
linerel draws a line from the CP to a point that is a relative distance
(dx, dy) from the CP, then advances the CP by (dx, dy).
lineto draws a line from the CP to (x, y), then moves the CP to (x, y).
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
// ----------------------------------------
int d=DETECT,m;
initgraph(&d,&m,"c:\\tc\\bgi");
// -------------- faze 2 -------------------
int x, y;
x = getmaxx() / 2;
y = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
// ----------------------------------------
outtextxy(x, y, "Press a key to close the graphics system:");
getch();
closegraph();
cout<<"We're now back in text mode."<<endl;
cout<<"Press any key to halt:"<<endl;
getch();
}
ــــــــــــ
closegraph <GRAPHICS.H>
------------
Shuts down the graphics system
Declaration: void far closegraph(void);
Remarks:
closegraph deallocates all memory allocated by the graphics system.
It then restores the screen to the mode it was in before you called
initgraph.
(The graphics system deallocates memory, such as the drivers, fonts, and an
internal buffer, through a call to _graphfreemem.)
moveto(100,70);
moverel(5, 5);
x_cp=getx();
y_cp=gety();
// draw two lines and a circle on your console screen
// original BCX basic code by Sir Joe Caverly
// translated to C code and modified to work with Dev-C++
// link with GDI32.lib or with Dev-C++ link libgdi32.a via
// Project>>Project Options>>Parameters>>Add Lib>>libgdi32.a
// this is a Windows Console Application
#include <windows.h> // Win32API Header File
#include <cstring>
#include <cstdio>
//using namespace std;
#define Red RGB (255,0,0)
#define Lime RGB (206,255,0)
#define Blue RGB (0,0,255)
static HWND hConWnd;
int BCX_Line (HWND,int,int,int,int,int=0,HDC=0);
int BCX_Circle (HWND,int,int,int,int=0,int=0,HDC=0);
HWND GetConsoleWndHandle (void);
int main()
{
hConWnd = GetConsoleWndHandle();
if (hConWnd)
{
// be creative here, draw your own circles or lines
// hwin, centerX, centerY, radius, pencolor
BCX_Circle(hConWnd, 150, 130, 105, Blue);
// hwin, ulcX, ulcY, lrcX, lrcY, pencolor
BCX_Line(hConWnd, 5, 5, 300, 250, Red);
BCX_Line(hConWnd, 295, 5, 5, 250, Lime);
getchar(); // wait
}
return 0;
}
int BCX_Line (HWND Wnd,int x1,int y1,int x2,int y2,int Pen,HDC DrawHDC)
{
int a,b=0;
HPEN hOPen;
// penstyle, width, color
HPEN hNPen = CreatePen(PS_SOLID, 2, Pen);
if (!DrawHDC) DrawHDC = GetDC(Wnd), b = 1;
hOPen = (HPEN)SelectObject(DrawHDC, hNPen);
// starting point of line
MoveToEx(DrawHDC, x1, y1, NULL);
// ending point of line
a = LineTo(DrawHDC, x2, y2);
DeleteObject(SelectObject(DrawHDC, hOPen));
if (b) ReleaseDC(Wnd, DrawHDC);
return a;
}
// converts circle(centerX,centerY,radius,pen) to WinApi function
// ellipse inside box with upper left and lower right coordinates
int BCX_Circle(HWND Wnd,int X,int Y,int R,int Pen,int Fill,HDC DrawHDC)
{
int a, b = 0;
if (!DrawHDC) DrawHDC = GetDC(Wnd), b = 1;
// penstyle, width, color
HPEN hNPen = CreatePen(PS_SOLID, 2, Pen);
HPEN hOPen = (HPEN)SelectObject(DrawHDC, hNPen);
HBRUSH hOldBrush;
HBRUSH hNewBrush;
// if true will fill circle with pencolor
if (Fill)
{
hNewBrush = CreateSolidBrush(Pen);
hOldBrush = (HBRUSH)SelectObject(DrawHDC, hNewBrush);
}
else
{
hNewBrush = (HBRUSH)GetStockObject(NULL_BRUSH);
hOldBrush = (HBRUSH)SelectObject(DrawHDC, hNewBrush);
}
a = Ellipse(DrawHDC, X-R, Y+R, X+R, Y-R);
DeleteObject(SelectObject(DrawHDC, hOPen));
DeleteObject(SelectObject(DrawHDC, hOldBrush));
if (b) ReleaseDC(Wnd, DrawHDC);
return a;
}
// the hoop ...
HWND GetConsoleWndHandle(void)
{
HWND hConWnd;
OSVERSIONINFO os;
char szTempTitle[64], szClassName[128], szOriginalTitle[1024];
os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
GetVersionEx( &os );
// may not work on WIN9x
if ( os.dwPlatformId == VER_PLATFORM_WIN32s ) return 0;
GetConsoleTitle( szOriginalTitle, sizeof( szOriginalTitle ) );
sprintf( szTempTitle,"%u - %u", GetTickCount(), GetCurrentProcessId() );
SetConsoleTitle( szTempTitle );
Sleep( 40 );
// handle for NT
hConWnd = FindWindow( NULL, szTempTitle );
SetConsoleTitle( szOriginalTitle );
// may not work on WIN9x
if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
{
hConWnd = GetWindow( hConWnd, GW_CHILD );
if ( hConWnd == NULL ) return 0;
GetClassName( hConWnd, szClassName, sizeof ( szClassName ) );
while ( strcmp( szClassName, "ttyGrab" ) != 0 )
{
hConWnd = GetNextWindow( hConWnd, GW_HWNDNEXT );
if ( hConWnd == NULL ) return 0;
GetClassName( hConWnd, szClassName, sizeof( szClassName ) );
}
}
return hConWnd;
}