Program-1
INTRODUCTION TO VISUAL C++
VISUAL C++
Visual C++ is a collection of many tools, all wrapped into one dynamic, ready to
use package for windows programming.
Microsoft introduced the Microsoft Foundation Class (MFC) (it is an extraordinary
package of prewritten, ready to use code) for VC++. ln addition it provides
various programming tools such as menu editor, dialog editor. lt provides us
with one integrated design environment in which we can write our programs and
run them. Visual C++ organizes the many files a windows program needs into
projects, handling a lot of work for us.
THE PARTS OF A VISUAL C++ PROGRAM
There are four major parts of a visual C++ Application Wizard:
The Application Object
The Main Window Object
The Document Object
The View Object
THE APPLICATION OBJECT:
The application object supported by .h and .cpp files (the .h file holds the
definition of constants and the declaration of variables and methods in this
class|, is what windows actually runs first. When this object is started, it places
the main window on the screen.
THE MAIN WINDOW OBJTCT:
The main window object displays the program itself and this is where the menu
bar, the title bar of the window and the toolbar is found..The main window
object is responsible for everything that surrounds the area where the action -
the drawing text and other graphics goes [n our window. That area is called
client area.
THE VIEW OBJECT
The view object handles the client area-this is where we will format and display
the data in our program, such as the text we are editing. ln fact the view object
is really a window itself that appears on the top of the client area.
THE DOCUMENT OBJECT
ln the document object, we store the data for our programs. There can a be a
great deal of data that cannot fit into our client area .Therefore all the data is
stored in document object, then handles the display of data that will fit into the
client area in the view object.
1
(FOUR PARTS OF A VISUAL C++ PROGRAM)
2
Creating a new MFC Application
File → new → projects → select → MFC AppWizard (exe) →give a project name → OK.
Select a single document and FINISH.
3
PROGRAM 2
WAP to print “Visual C++” in Visual C++
STEP 1:
Create an MFC Application(.exe) single document.
STEP 2:
Do the following coding ln OnDraw() method in view.
void CHelloView::On Draw{CDC* pDC}
{
CString stringdata = "welcome to Visual C++";
CHelloDoc* pDoc = GetDocument();
ASSERT_VALID (pDoc);
pDc->Text0ut (0, 0, stringdata);
}
OUTPUT
4
PROGRAM 3
WAP to show relation between document class and relation class.
1: Open the class document and write the code as shown.
2: Write the following code in the constructor of document as shown.
5
3: Click on the view class, find OnDraw() method and write the following code.
OUTPUT
6
PROGRAM 4
WAP to read keystrokes from the keyboard.
1: Setting up storage area for Keyboard data
Open document class.
Initialize the string.
Write code shown in the snapshot.
2: Initialize the empty string in constructor of the document class. Write code
shown in the snapshot
7
3: Reading Keystroke.
ADD WM_CHAR MESSAGE
View → Class → Wizard → Set Class Name to View → Find Object ID as WM_CHAR
→ Double Click on that (u will see it appear in front of ON_CHAR) → Click OK.
4: Recording CHAR in our document
FIND ON_CHAR()METHOD IN VIEW CLASS (LEFT SIDE OF YOUR CODE
WINDOW)
WRITE THE CODE SHOWN IN SNAPSHOT IN ON_CHAR METHOD BY DOUBLE
CLICKING IT.
8
9
5. OPEN ON DRAW() METHOD OF VIEW CLASS AND WRITE THE CODE SHOWN
IN SNAPSHOT
OUTPUT
10
PROGRAM 5
WAP TO DISPLAY CARET
STEPS:
1.Start->Programs->MS Visual Studio 6.0->Visual C++6.0
2.File->New->Select MFC application wizard (exe)
Select single document.
Next.....finish.
3.In document class, initialize string.
4. Go to constructor of document class
11
5. Go to view class and write the code.
6.Set CaretCreated to false in the view class constructor
12
13
7. Start ClassWizard, connect OnChar() method to the WM_CHAR windows
message in the view.
8. Write the code on OnChar() method of view class.
9. Go to OnDraw() method of view class.
10. Set the position of caret.
14
11. Move the caret as the use type text in OnDraw() method.
OUTPUT
15
PROGRAM 6
WAP FOR A MENU EDITOR ADDING A MENU ITEM AND DISPLAYING THE
MESSAGE
STEP 1 USING MENU EDITOR
CLICK ON THE RESOURCE VIEW THIS OPENS THE MENU’S PROGRAM RESOURCE
FOLDER.
FIND OUT THE FOLDER MARKED MENU OPEN IT
THEN DOUBLE CLICK ON THE ENTRY IN THAT FOLDER.,IDR)MAINFRAME
BELOW SNAPSHOT WILL APPEAR
STEP 2 ADDING A NEW MENU ITEM
CLICK ON THE PRINT PREVFIEW MENU ITEM MPRESS INSERT KEY, BLANK MENU ITEM APPEAR WRITE
CAPTION PRINT WELCOME AND CLOSE THE DIALOG BOX
16
STEP 3 CONNECTING MENU ITEM TO CODE
OPEN CLASS WIZARD
SET THE FOLLOWING OPTIONS
PROJECT: NAME OF YOUR PROJECT
CLASS NAME:- VIEW
OBJECT ID ID_FILE_WELCOME
MSG COMMAND :-COMMAND(DOUBLE CLICK ON IT)
17
A DIALOG BOX APPEARS, CLICK OK
STEP 4 OPEN DOCUMENT CLASS IN PUBLIC SECTION DECLARE VARIABLE
STEP 5
INITIALISE EMPTY STRING IN CONSTRUCTOR OF DOCUMENT CLASS
18
STEP 6
STEP 7 OPEN ONDRAW METHOD OF VIEW CLASS AND WRITE THE CODE
19
OUTPUT
20
PROGRAM 7
WAP to create a browser
STEP 1
CLICK ON FILE -> NEW -> MFC APP.exe -> CHOOOSE DIALOG BASED ISTEAD OF SINGLE DOCEMUNT -
> FINISH
YOU WILL SEE A TOOL BOX AND A DESIGN WINDOW AS SHOWN IN SNAPSHOT
STEP2: DRAG A BUTTON FROM TOOL BOX
21
STEP 3: NOW ADD ACTIVEX CONTROL IN YOUR PORJECT
GOTO PROJECT -> ADD TO ACTIVE PROJECT -> COMPONENTS AND CONTROL -> OPEN REGISTERED
ACTIVEX COMPONENTS -> SELECT MICROSOFT WEB BROWSER FROM THE LIST -> INSERT ->
OK(CONFIRM THE CLASS)-> CLOSE
AS SHOWN IN THE SNAPSHOT
22
STEP 4: NOW DRAG THIS CONTROL ON YOUR DESIGN WINDOW AND DRAG A BUTTON
23
STEP 5: NOW CONNECT A MEMBER VARIABLE TO THIS CONTROL AND NAME THAT VARIABLE
m_browser
For this:
CLICK WIZARD->CLICK THE TAB MEMBER VARIABLE->SET CONTROL ID AS IDC_EXPLORER1->CLICK
ADD VARIABLE AS SHOWN IN THE SNAPSHOT AND NAME THAT VARIABLE AS m_browser->CLICK OK
24
STEP 6: NOW CONNECT EVENT HANDLER METHOD TO THAT BUTTON
For this:
VIEW-> CLASS WIZARD->CLICK ON MESSAGE TAB->CONTROL ID AS IDC_BUTTON1->SET MESSAGES
AS BN_CLICKED(DOUBLE CLICK ON IT)->YOU WILL SEE ADD MEMBER FUNCTION DIALOG BOXES-
>OK
25
STEP 7: DOUBLE CLICK ON BUTTON AND WRITE THE CODE
m_browser>Navigate(http://www.gmail.com,0,0,0,0);
26
OUTPUT
27
PROGRAM 8
WAP TO CREATE A CALCULATOR IN VC++
STEP 1: DESIGNING THE FORM WINDOW
I. OPEN THE FORM WINDOW.
II. ADD FOUR BUTTONS ON THE FORM
III. CHANGE THE PROPERTY TEXT AS DESIRED FUNCTION TO BE PERFORMED
IV. ADD THREE TEXT BOXES FOR INPUT AND DISPLAY
FORM
28
STEP 2: CODING THE BUTTON ACTIONS
I. DOUBLE CLICK THE ADD BUTTON TO OPEN ITS CODE WINDOW
II. ADD THE DESIRED CODING
Private Sub Command1_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
num1 = Text1.text
num2 = Text2.text
num3 = num1 + num2
Text3.text = num3
End Sub
Private Sub Command2_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
num1 = Text1.text
num2 = Text2.text
num3 = num1 - num2
Text3.text = num3
End Sub
Private Sub Command3_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
29
num1 = Text1.text
num2 = Text2.text
num3 = num1 * num2
Text3.text = num3
End Sub
Private Sub Command4_Click()
Dim num1 As Integer
Dim num2 As Integer
Dim num3 As Integer
num1 = Text1.text
num2 = Text2.text
num3 = num1 / num2
Text3.text = num3
End Sub
30
OUTPUT
31