DRAWING INSIDE THE VIEW WINDOW
OnDraw member function:
Ø Is a virtual member function of the CView class that the application framework calls every time the view window needs to be repainted.
Windows device context:
Ø Windows doesn’t allow direct access to the display hardware but communicates through an abstraction called a “device context” that is associated with the window.
Ø In the MFC library, the device context is a C++ object of class CDC that is passed as a parameter to OnDraw function.
Steps to build the application:
1.Run AppWizard to generate SDI application source code.
Ø Choose New from visual C++ File menu & then click Projects tab in the resulting New dialog box.
Ø Make sure that MFC AppWizard (exe) is selected and specify the path in the Location textbox finally specify Project Name and click OK.
Ø In the sequence of AppWizard screens select the following:
v Single Document Interface.
v Accept the default in the next four screens.
v In the last screen check the project name, class name and click Finish.
v It displays the New Project Information dialog box, click OK button.
Ø The AppWizard starts to create application’s subdirectory and a series of files in that subdirectory.
FILE | DESCRIPTION |
Filename.dsp | A project file that allows Visual C++ to build the application. |
Filename.dsw | A workspace file that contains a single entry for the .dsp file. |
Filename.rc | An ASCII resource script file. |
filenameView.cpp | A view class implementation file that contains class member functions. |
filenameView.h | A view class header file that contains the class declaration. |
Filename.opt | A binary file that tells Visual C++ which files are open for this project and how the windows are arranged. (not created until project is saved. |
Readme.txt | A text file that explains the purpose of generated files. |
Resource.h | A header file that contains #define constant declaration. |
2. Edit the OnDraw function in the filenameView.cpp:
Ø Find the AppWizard generated OnDraw function in View.cpp and add the following code.
void CfilenameView::OnDraw(CDC* pDC)
{
CfilenameDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC->TextOut(0,0,”Hello World!”); // prints in default font & //size, top left corner.
pDC->SelectStockObject(GRAY_BRUSH); // selects a brush for //the circle interior.
pDC->Ellipse(CRect(0,20,100,120)); // draws a gray circle 100 //units in diameter.
}
3. Compile & test the file.
No comments:
Post a Comment