Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 50

VISUAL PROGRAMMING

COLORS
COLOR

- one the most fundamental objects that enhances the aesthetic

appearance of an object

- non-spatial object that is added to an object to modify some of


its visual aspects

-Three numeric values are used to create a color


Red
Green
Blue
COLOR

- Each one represented in 8-bits and the value for each color
ranges from 0-255

- These 3 colors can be combined to form other colors

- Using these 3 we can create approximately 16 million colors


(255*255*255=16581375)
COLOR

- computer monitor has a surface that resembles a series of tiny


horizontal and vertical lines

- intersection of a one horizontal line and a vertical line is called


a
pixel that holds, carries, or displays one color

- As the pixels close to each other have different colors, the effect

is a wonderful distortion that creates an aesthetic picture


COLOR AS A DATATYPE
- Windows considers that a color is a 32-bit numeric value and
a color is actually a combination of 32 bits

-The bits of the most significant byte (the left byte) are reserved
for the operating system's internal use and must be set to 0
- The 32-bit numeric value used to characterize a color is
defined
by the COLORREF data type
- Declared as
COLORREF NewColor;
- After declaring initialize it with a 32-bit numeric value.It can be
done as follows
COLORREF NewColor = 16711935;
- Although the above number is a legitimate color value it does
not mean much
- One macro is defined to create a color called RGB macro. Its
syntax is
COLORREF RGB(BYTE byRed, BYTE byGreen, BYTE
byBlue);
RGB MACRO

- The RGB macro behaves like a function and allows you to pass
three numeric values separated by a comma.

- Each value must be between 0 and 255

- Initialization can be done as follows

COLORREF NewColor = RGB(255, 0, 255);


RETRIEVING COLOR VALUES

- To retrieve the red, green, and blue values of a color three


macros are defined

BYTE GetRValue(DWORD rgb);


BYTE GetGValue(DWORD rgb);
BYTE GetBValue(DWORD rgb);

- Each macro takes a 32-bit value as argument, arg


COLORING A PIXEL

- A pixel is the real object that holds a color

-The pixels are stored in an array of [x][y] value. To change the


color of a pixel, you can call the CDC::SetPixel() method
- Its syntaxes are:

COLORREF SetPixel(int x, int y, COLORREF crColor);


COLORREF SetPixel(POINT point, COLORREF crColor);
FONTS
FONT

- A list of symbols that can be drawn on a device context to


produce a readable message

- FONT CREATION

- FONT SELECTION
FONT CREATION

- Font is created as a variable of the CFont class which is based


on the CGdiObject

- To declare a CFont variable use the default constructor as


follows

CFont NewFont;

- After declaring a CFont variable, you must initialize it. Calling


one of the Create member functions can do
- There are three class methods used for creating fonts

• CreatePointFont
• CreateFont
• CreateFontIndirect
CreatePointFont Method

- Its syntax is

BOOL CreatePointFont(int nPointSize, LPCTSTR


lpszFaceName, CDC* pDC = NULL);

Where nPointSize is the height of the font

lpszFaceName is the name of the font or NULL


void myframe::OnLButtonDown(UINT
nFlags,CPoint point)
{
CFont font;
CClientDC pDC(this);
font.CreatePointFont(920, "Arial");
pDC.SelectObject(&font);

pDC.SetTextColor(RGB(0, 125, 250));


pDC.TextOut(20, 18, "Hello", 5);
}
CreateFont Method
- Its syntax is
BOOL CreateFont( int nHeight,
int nWidth,
int nEscapement,
int nOrientation,
int nWeight,
BYTE bItalic,
BYTE bUnderline,
BYTE cStrikeOut,
BYTE nCharSet,
BYTE nOutPrecision,
BYTE nClipPrecision,
BYTE nQuality,
BYTE nPitchAndFamily,
LPCTSTR lpszFacename);
CreateFont Method

- nHeight - height applied to the text.


- nWidth - desired width that will be applied on the text.
- nEscapement - angle used to orient the text (oriented
counterclockwise)
- nOrientation -angular orientation of the text with regards to the
horizontal axis.
- nWeight -Font weight
- bItalic specifies whether the font will be italicized (TRUE) or
not (FALSE).
- bUnderline is used to underline (TRUE) or not underline
(FALSE) the text.
- cStrikeOut is specifies whether the text should be stroke out
(TRUE) or not (FALSE) with a line.
CreateFont Method

- nCharSet specifies the character set used


- nOutPrecision - amount precision used to evaluate the numeric
values used on this function for the height, the width, and angles
-nClipPrecision – amount of clipping
-nQuality specifies how the function will attempt to match the
font's characteristics
-nPitchAndFamily specifies the category of the font used
-lpszFacename is the name of the font used
void myframe::OnLButtonDown(UINT nFlags,CPoint
point)
{
CFont font;
font.CreateFont(46, 28, 215, 0,FW_NORMAL,
FALSE, FALSE, FALSE,ANSI_C HARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_ROMAN, "Times New
Roman");
pDC->SelectObject(&font);
pDC->TextOut(20, 128, “Hello", 5);
}
CreateFontIndirect Method

-Its syntax is
BOOL CreateFontIndirect(const LOGFONT* lpLogFont);

- LOGFONT structure is used to define all the characteristics of


the font
typedef struct tagLOGFONT { LONG lfHeight;
LONG lfWidth;
LONG lfEscapement;
LONG lfOrientation;
LONG lfWeight;
BYTE lfItalic;
BYTE lfUnderline;
BYTE lfStrikeOut;
BYTE lfCharSet;
BYTE lfOutPrecision;
BYTE lfClipPrecision;
BYTE lfQuality;
BYTE lfPitchAndFamily;
TCHAR lfFaceName[LF_FACESIZE];
} LOGFONT, *PLOGFONT;
void myframe::OnLButtonDown(UINT nFlags,CPoint
point)
{ CClientDC pDC(this);
CFont font;
LOGFONT LogFont;
LogFont.lfStrikeOut = 0; LogFont.lfUnderline =
0; LogFont.lfHeight = 42;
LogFont.lfEscapement = 0; LogFont.lfItalic
= TRUE;
font.CreateFontIndirect(&LogFont);
pDC.SelectObject(&font);
pDC.TextOut(20, 18, “Hello", 5);
}
MAPPING MODES
DEFAULT COORDINATE SYSTEM

- The coordinates of the drawing area are located on


the upper-left corner of the screen.
- The horizontal axis moves from (0,0) to the right and
the vertical axis moves from (0,0) down
+X
0,0

+Y
- For example, consider the code for creating an
ellipse
DEFAULT COORDINATE SYSTEM

void CExoDraw1View::OnPaint()
{
CPaintDC dc(this); // device context for painting
// A circle whose center is at the origin (0, 0)
dc.Ellipse(-50, -50, 50, 50);
// A line that starts at (0, 0) and ends at (100, 100)
dc.MoveTo(0, 0);
dc.LineTo(100, 100);
}
- The output of this is shown in the figure as follows
CHANGING COORDINATE SYSTEM

- The MFC provides various functions to deal with the coordinates


positions and extents of the drawing area .

- One such function is the CDC::SetViewportOrg(). The syntaxes of


this method are
SetViewportOrg(int X, int Y);
SetViewportOrg(CPoint Pt);

- The function of this method is to change the origin of the


coordinate system
CHANGING COORDINATE SYSTEM

- To see the effect of this method change the previous OnPaint()


method as follows

void CExoDraw1View::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SetViewportOrg(200, 150);
// A circle whose center is at the origin (0, 0) dc.Ellipse(-50, -50,
50, 50);
// A line that starts at (0, 0) and ends at (100, 100)
dc.MoveTo(0, 0);
dc.LineTo(100, 100);
}
MAP MODES 

- To control the orientation of the axes of the device context , a


method called SetMapMode() is used.

- The syntax of this method is


int SetMapMode(int nMapMode);

- It performs two functions


-> controls the orientation of the coordinate system
-> helps with the unit system you would prefer to use. 
MAP MODES 

- The various types of map modes are

MM_TEXT,
MM_LOENGLISH
MM_HIENGLISH
MM_ANISOTROPIC
MM_HIMETRIC
MM_ISOTROPIC
MM_LOMETRIC
MM_TWIPS
MM_TEXT

- Default map mode

- With this map mode the dimensions or measurements specified in


the CDC methods are kept as it is.

- horizontal axis moves from (0, 0) to the right and the vertical axis
moves from (0, 0) down

- In general, no change is made to both the orientation and the


measurement units.
MM_TEXT
void CExoDraw1View::OnPaint() {
CPaintDC dc(this); // device context for painting
dc.SetMapMode(MM_TEXT);
dc.SetViewportOrg(380, 220);
CPen PenRed(PS_SOLID, 1, RGB(255, 0, 0));
dc.SelectObject(PenRed);
dc.Ellipse(-100, -100, 100, 100);
CPen PenBlue(PS_SOLID, 1, RGB(0, 0, 255));
dc.SelectObject(PenBlue);
dc.MoveTo(-380, 0); dc.LineTo(380, 0);
dc.MoveTo(0, -220); dc.LineTo(0, 220);
CPen PenOrange(PS_SOLID, 1, RGB(255, 128, 0));
dc.SelectObject(PenOrange);
// A diagonal line at 45 degrees
dc.MoveTo(0, 0); dc.LineTo(120, 120); } 
MM_TEXT
MM_LOENGLISH

- It performs two actions

* It changes the orientation of the vertical axis: the positive y


axis would move from (0, 0) up

+Y

0,0 +X
* Each unit of measure provided is divided by 100
(unit/100)

- If we change the map mode in the previous code to


MM_LOENGLISH the output will look as follows
MM_LOENGLISH
MM_HIENGLISH

- It sets the orientation so the vertical axis moves from (0, 0)

- Each unit is divided by 1000 (1/1000 = 1000th) that is significant


and can change the display of a drawing

- Now the output will look as follows


MM_HIENGLISH
MM_LOMETRIC & MM_HIMETRIC

- uses the same axes orientation as the previous two modes


- With MM_LOMETRIC map mode each unit is reduced by 10%
- With MM_HIMETRIC map mode each unit is multiplied with
0.01mm.

- The example program using these modes are shown in following


figures.
MM_LOMETRIC
MM_HIMETRIC
MM_TWIPS

- The MM_TWIPS map mode divides each logical unit by 20

- Actually a twip is equivalent to 1/1440 inch


MM_TWIPS
CUSTOMIZING THE UNIT AND
COORDINATE SYSTEM
- To control the unit system & the orientation of the axes two
map modes are used. They are
MM_ISOTROPHIC
MM_ANISOTROPHIC
- With MM_ISOTROPHIC , one unit in the horizontal axis is
equivalent to one unit in the vertical axis

- With MM_ANISOTROPHIC, the units should be converted on


each individual axis

- One more method used to change the unit system is


SetWindowExt() that specifies how much each new unit will be
multiplied by the old or default unit system
MM_ISOTROPHIC

- The syntaxes of this method are


CSize SetWindowExt(int cx, int cy);
CSize SetWindowExt(SIZE size);

- The following example shows the effect of this method


void CExoDraw1View::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SetMapMode(MM_ISOTROPIC);
dc.SetViewportOrg(340, 220);
dc.SetWindowExt(480, 480);
…. Same as previous
}
Without Using MM_ISOTROPHIC
Using MM_ISOTROPHIC
THANK YOU

You might also like