Supplementary materials for Visual C++ programming

advertisement
Supplementary materials for Visual C++ programming
1.
What are “comments” in a program? How do we put “comments in Visual C++ source codes?
a.
b.
“// This is a comment line”
“/* This is a block of comment, which can across several lines.
This is the second line of a comment */”
2.
What is the syntax of a function:
variable of CBrush class). You can draw a line (the MoveTo and LineTo functions of CDC class) on the
drawing board.
7. “Help” in Visual C++.
The MSDN package is the on-line manual for Visual C++. You can search useful syntax and functions
very easily in MSDN. However, you need it install it separately – not included in Visual C++.
Contents of the “Painting.cpp” file in the TwoDEx program
void DoPainting(CDC *pDC,int Width, int Height)
{…
}
void – return nothing from this function (equivalent to ‘Procedure’ in Visual Basic)
int Width, int Height – arguments passed to this function, of the type ‘integer’.
CDC *pDC – an argument passed to this function. This is a pointer to an object (a variable) of the
CDC class.
{..} – a block of code in C++. Can be nested.
3.
Basic Syntax
-
case sensitive
have to declare variables before use
have ‘;’ after each statement
the ‘for’ loop: for (x=100;x<300;x=x+1)…
4.
What is an object class? Take “CDC” as example.
An object class is something like data structure, but it not only defines a data structure, it also defines
the set of handlers and functions provided with this data structure.
“CDC” is an object class in Visual C++. All object classes defined in Visual C++ is headed with the
‘C’ character’. ‘DC’ stands for ‘Device Context’.
The CDC class models a drawing board. Imagine that you have a drawing board (a variable of CDC
class). You can draw a dot (the SetPixel function of CDC class) of a specific color at a specific
location on it.
// Painting.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "TwoDEx.h"
//======================================================
//InitializeSettings - Initialize settings
// Followings are the global variables of system
// settings which are needed to be defined:
//
gBackgroundColor, gWidthOfView,gHeightOfView
//======================================================
void InitializeSettings()
{
gBackgroundColor=RGB(0,0,0); //background color
gWidthOfView=400;
//width(in pixels) of the view
gHeightOfView=300;
//height(in pixels) of the view
}
//======================================================
//DoPainting - Paint the picture
//======================================================
void DoPainting(CDC *pDC,int WhichCallInCurrentCycle)
{
//Draw a white dot at (200,50);
{
pDC->SetPixel(200,50,RGB(255,255,255));
}
//Draw a horizontal line of green dots with different intensity
{
int x;
int green=0;
for (x=100;x<300;x=x+1)
{
pDC->SetPixel(x,100,RGB(0,green,0));
green=green+1;
}
}
5. How do you paint a bitmap onto a CDC object?
In order to do this, Visual C++ expects you to create an object (variable) of the class CBitmap and
associate it with a temporary DC. However, in this assignment, there is a convenient class defined
for you: CBitmapDC which assembles the necessary things and pack it into a black box. So, now it is
easy: You declare an object of the class CBitmapDC, with passing the resource bitmap’s ID and a
reference DC’s pointer; then you can use BitBlt to put the bitmap to any other CDC objects.
//Put IDB_BITMAP1 on the display
{ CBitmapDC bm_dc(IDB_BITMAP1,pDC);
BitBlt(pDC->GetSafeHdc(),100,150,40,40,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
//Put IDB_BITMAP1 on the display
{
CBitmapDC bm_dc(IDB_BITMAP1,pDC);
BitBlt(pDC->GetSafeHdc(),100,150,40,40,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
//Put IDB_BITMAP2 on the display
{
CBitmapDC bm_dc(IDB_BITMAP2,pDC);
BitBlt(pDC->GetSafeHdc(),200,150,40,40,bm_dc.GetSafeHdc(),0,0,SRCCOPY);
}
6. More information on CDC
The CDC class models a drawing board. Imagine that you have a drawing board (a variable of CDC
class). When you draw, and you pick up a pen (a variable of CPen class), or you pick up a brush (a
}
Download