[go: up one dir, main page]

0% found this document useful (0 votes)
93 views80 pages

VP Lab

This program creates menus and menu items in Visual C++ and sets various properties like graying out menu items, checked items, submenus, shortcuts, status bar prompts, and tool tips. It uses the menu editor to add new menus and menu items. ClassWizard is used to connect menu items to code handling click events. Methods like Enable() are used to gray out menu items programmatically.

Uploaded by

Kshitij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views80 pages

VP Lab

This program creates menus and menu items in Visual C++ and sets various properties like graying out menu items, checked items, submenus, shortcuts, status bar prompts, and tool tips. It uses the menu editor to add new menus and menu items. ClassWizard is used to connect menu items to code handling click events. Methods like Enable() are used to gray out menu items programmatically.

Uploaded by

Kshitij
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 80

Program 1

# Write a program in Visual C++ to create a DLL Application and link it to a Visual Basic
form.

Steps to create an ATL COM Project:


1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.

2. Now select the ATL COM AppWizard entry in the New dialog box.
3. Give the new program a name in the project name box.
4. Click OK to start the ATL COM AppWizard.

5. Select the option Dynamic Link Library (DLL) in Server Type group box in the
AppWizard and click Finish.
6. This opens an Information Box.

7. Click the OK button and the new project has been created.
Creating the ATL object:
1. Right click the dllserver classes in the workspace area and select new ATL object.

2. Select simple object in ATL Object Wizard dialog box. Click Next.

3. Write object name as dllobject in Short Name box of ATL Object Wizard properties.
Click OK.
4. Right click on Idllobject in workspace and select Add Method. A dialog box appears and
give the Method name as message.
Coding:

Click on message method in Cdllobject and in code editor window, write the following code:

Compile and Build the program.

Creating a form in VB:


1. Open Microsoft Visual Basic 6.0. In the New Project box, select Standard EXE. Click
Open.

2. Add a Button to the form. Rename it as Hello.

3. Click on Project Menu and select References menu item.


4. In References Dialog box, select dllserver 1.0 Type Library. Click OK.

Coding:
Double click on the Button and write the following code in the code editor window:

Output:

Click on the (Run) button in the toolbar and output goes as follows:
Program 2

# Write a program in Visual Basic 6.0 to show connectivity to a database by using ADO
Control.
Steps:

1. Open Microsoft Visual Basic 6.0. A New Project dialog box will appear. Select
Standard.Exe and click Open.

2. Go to Project menu and select Components.

Following Dialog box will appear:


3. Under Controls Tab, look for Microsoft ADO Data Control 6.0 (OLEDB). Press OK.

4. An ADODC component will appear on the tool box. Drag an instance of this tool on the
form.
5. Right Click on instance that you have dragged and select ADODC properties.

6. Property Pages dialog box will appear. Under the General tab, be sure that ‘Use
Connection String’ is selected. Click on Build button. Data Link Properties dialog box
will appear.
7. Under the Provider tab, select an appropriate driver or provider. Here we select Microsoft
Jet 4.0 OLEDB Provider. It is the default driver for MS Access databases.

Click Next. Connection tab will appear.


8. Under Connection Tab, Select or Enter Database Name by clicking a button adjacent to
the text box.

Select Access Database dialog box will appear.

9. Select a database (Biblio.mdb). Click Open.


10. We will again be directed to Data Link Properties Box. Press Test Connection button. If
an information box, “Test Connection Succeeded” appears, the connection is successful.
Click OK.

11. Select Record Source in the property section of ADODC component inserted on the form.

12. Under RecordSource group box, select an appropriate Command Type. Here, select 2-
adCmd Table.

13. Select a table Authors. Click Apply and then OK.

14. Place two instances of Text Box control on the form.


15. Select both text boxes. Under properties box, in Data Source, select ADODC1.

16. Select Text 1. Under Data Field in Properties Box, select AU_ID.
17. Select Text 2. Under Data Field in Properties Box, select AUTHOR.

18. Run the Project.

Output:
INDEX
S.No Name of the Program Date Sign
1. WAP in VC++ to create a DLL application.

2. WAP in VB to show Database Connectivity.

Program 3
# Write a program in Microsoft Visual C++ 6.0 to create a MFC application which displays a
string on the window as an example of Document –View architecture.

Steps to create a MFC Application:


1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.

2. No select the MFC AppWizard(exe) entry in the New dialog box.


3. Give the new program a name in the project name box.
4. Click OK to start the Visual C++ AppWizard.

5. Select
the
option
Single

Document in the AppWizard and click Next.


6.
Leave the None option selected in the window and press Next.

7. Keep pressing Next until the wizard reports us of the creation of the classes in the program.
8. Click on the Finish button now. This opens an Information Box.

9. Click the OK button and the new project has been created.
Code:

Declaration in Document class:


Declaring a data object data of type CString, where CString is a MFC class.

Initializing in Constructor:

Data object data is passed a string “Welcome to Visual C++”.

Displaying the string using OnDraw method of View class:

OnDraw method is used to display the string. Here TextOut method of CDC class is called using
pDC, a pointer to CDC. Three parameters are passed to textout method. They are: coordinates of the
window to display the text (here (0,0)) and the string to be displayed which is called using pDoc, a
pointer to CFirstDoc class.

Output:-
Program 4

# Create a small program in Microsoft Visual Basic 6.0 to implement a simple calculator.
Form:

Code:

Dim num1 As Integer


Dim num2 As Integer
num1 = Text1.Text
num2 = Text2.Text
Text3.Text = num1 + num2
End Sub

Private Sub Command2_Click()


Dim num1 As Integer
Dim num2 As Integer
num1 = Text1.Text
num2 = Text2.Text
Text3.Text = num1 - num2
End Sub

Private Sub Command3_Click()


Dim num1 As Integer
Dim num2 As Integer
num1 = Text1.Text
num2 = Text2.Text
Text3.Text = num1 * num2
End Sub

Private Sub Command4_Click()


Dim num1 As Integer
Dim num2 As Integer
num1 = Text1.Text
num2 = Text2.Text
Text3.Text = num1 / num2
End Sub

Output:-
Program 5

# Write a program in Microsoft Visual C++ which creates new menus and menu items in
Menu bar. Also set various properties like:
(i) Graying out of menu item
(ii) Checked item
(iii) Submenus
(iv) Status Bar Prompts
(v) Shortcuts
(vi) Accelerator keys
(vii) Tool Bar Tools
(viii) Tool tips

Steps to create a MFC Application:

1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.
2. No select the MFC AppWizard(exe) entry in the New dialog box.
3. Give the new program a name in the project name box.
4. Click OK to start the Visual C++ AppWizard.
5. Select the option Single Document in the AppWizard and click Next.
6. Leave the None option selected in the window and press Next.
7. Keep pressing Next until the wizard reports us of the creation of the classes in the program.
8. Click on the Finish button now. This opens an Information Box.
9. Click the OK button and the new project has been created.

Code:

Using the Menu Editor:

The Menu Editor is one of the most useful Visual C++ tools, and lets us design menus easily. To
open the menu editor, the steps to be followed are:
1. Click on the Resources tab in VC++’s viewer window. This opens the menus program’s
resources folder.
2. Find the folder marked Menus and open it.
3. Double click on entry in that folder, IDR_MAINFRAME, opening the menu editor.
Adding a New menu item:

Now, we will add a new menu item, Print Current. To do that, we will do the following:

 Click on the file menu in the menu editor.


 To add Print Current between Print Preview and Print menu items, click Print Preview menu
item and press the Insert key.
 This will add a new, blank menu item.
 Double click this new menu item, opening the Menu Item Properties Box.
 Place the caption Print Current in caption box and close it.
 This automatically gives ID ID_FILE_PRINTCURRENT to new menu item.
Connecting Menu Items to Code:

Now, we will connect the menu item to the code. For this, we will use ClassWizard. Open the
ClassWizard and look for the ID ID_FILE_PRINTCURRENT in the object IDs box in ClassWizard.
Click on this ID and then click the item’s Command entry in the Messages box. This makes
ClassWizard suggest a method OnFilePrintcurrent(). Click OK.
Double click on OnFilePrintcurrent() in View class. Add code for displaying a Message Box, which
displays a message “Sending the page to printer…” and title of the message box would be “Print
Current”.

Creating the Full Menu:


Creating Whole New Menus:

Now, we will add a new menu, Print. To do that, we will do the following:

 To add Print menu between Edit and View menu, click View menu and press the Insert key.

 This will add a new, blank menu.

 Double click this new menu item, opening the Menu Properties Box.

 Place the caption Print in caption box and close it.

 To add menu items to Print menu we will follow the previous steps to add the menu items.

 The Print menu will now contain two menu items, Print Preview and Print.

Grayed Item:
We will make the item Print Current in File Menu Grayed (inactive). For this we need ClassWizard.
Open the ClassWizard and find the ID_FILE_PRINTCURRENT item in Object IDs box. Now, click
on UPDATE_COMMAND_UI and add the function OnUpdateFilePrintcurrent() in View class.

Now, to gray out the menu item, we call CCmdUI object’s Enable() method, passing the value false
to the method to make the item grayed.

Checked Item:

Now we will check menu item Print Preview in Print menu i.e. a check mark appears in the front of
Print Preview. For this we again need ClassWizard. Open the ClassWizard and find the
ID_PRINT_PRINTPREVIEW item in Object IDs box. Now, click on UPDATE_COMMAND_UI
and add the function OnUpdatePrintPrintpreview() in View class.

Now, to Check the menu item, we call CCmdUI object’s Enable() method, passing the value true to
the method to make the item ungrayed and we will also call SetCheck() method and pass the value 1
to make the item checked.

Adding Submenu:

Now, we will add submenu to the menu print and menu item print. The steps are:
1. Double click on Print menu item to open the menu item properties box.
2. Click the Pop-up item in that box.
3. This adds a new submenu with one blank item to the submenu menu item.
4. Then, we can add new items Print All, Print Current and Print Selected to the Print
submenu.

Status Bar Prompts:

These prompts give more information about an item when mouse is placed over them. Here, we
would add Status Bar prompt to the Print Current item in File menu. For this, we will open the
properties box of Print Current and add the prompt “Prints current page” in the Prompt box. So,
when the mouse cursor is placed over the Print Current, the status bar will display the message.
Adding Shortcuts:

The F in File is underlined this means that when we press Alt+F, the File menu opens. Similarly, we
will add a shortcut key to the newly created Print menu i.e. Alt+P. To do this we will open the
properties box of File menu and place an ampersand (&) just before the letter which is to be used as
shortcut (here, P).

Adding Accelerator keys:

An accelerator key is the control key that user can press any time, and doing so is same as clicking
the menu item. Here, we will add accelerator keys Ctrl+F8 to Print All item of Print Submenu of
Print menu. To do this following steps are to be followed:

 Go to the Resource view and open the Accelerator folder.


 Double click the IDR_MAINFRAME entry in that folder.
 Double Click the last blank entry in the accelerator editor, opening the Accel Properties box.
 Select ID for Print All i.e. ID_PRINT_PRINT_PRINTALL.
 To connect Ctrl+F8 to this ID, click the Ctrl box in the Modifiers box.
 Select VK_F8 in Key box.
 Close the box. This adds Ctrl+F8 as accelerator key to the Print All.
 To indicate that we have added an accelerator key for Print All, change the item’s caption to
Print All\tCtrl+F8. Here, \t indicates the tab.
Adding tools to the toolbar:

Now, we will add two toolbar buttons, one indicating a rectangle and other, a circle. The steps for
creating the buttons are as follows:

 Open the menu editor by clicking the Resources tab in VC++ viewer’s window.
 Find the folder marked Toolbar and open it.
 Double click on IDR_MAINFRAME opening the program’s toolbar.
 Add two new buttons by using drawing tools. These buttons will look as follows:

Adding Tool tips to the buttons:

Now, we will add the tool tips to our buttons. Tool tip is a message displayed in a yellow box
whenever the mouse cursor is placed over the button. To add the tool tip, double click on the
button in the Toolbar editor. This will open the Toolbar Button Properties box. In prompt box,
enter the string you want to display as tool tip. We can also add status bar prompt for the button
by adding \n and then the message after the tool tip string.
Output:-
Menu ‘Print’ added to the menu bar:

Menu item Print Current along with its Status bar prompt added to File menu:
Message box displayed when Print Current is Clicked:

Print Current in Grayed Mode:


Menu items Print Preview and Submenu Print:

Here, Print Preview is checked and Print All Submenu item is connected to Accelerator key Ctrl+F8.

Two buttons displaying rectangle and circle along with their tool tips and Status bar prompts
are displayed:
Program 6

# Write a program in Microsoft Visual C++ to add a Caret to the window.


Steps to create a MFC Application:

1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.
2. No select the MFC AppWizard(exe) entry in the New dialog box.
3. Give the new program a name in the project name box.
4. Click OK to start the Visual C++ AppWizard.
5. Select the option Single Document in the AppWizard and click Next.
6. Leave the None option selected in the window and press Next.
7. Keep pressing Next until the wizard reports us of the creation of the classes in the program.
8. Click on the Finish button now. This opens an Information Box.
9. Click the OK button and the new project has been created.

Code:

To add Caret to the window, we need to add coding in OnDraw() method of CCaretView class. We
have set up a Boolean variable named Caretcreated in the view object to keep track of whether or
not the caret has been already created.
The Caretcreated is firstly set to false. To check if the caret has already been created in the
previous call we need if statement. If we haven’t created the caret, we will create it now. To decide
the size of the caret we will use TEXTMETRIC structure by calling GetTextMetrics(). We fill a
TEXTMETRIC structure named textmetric in the program. We will make the caret the same
height as our text, using textmetric.tmHeight, and ¼ Th the width of an average character
textmetric.tmAveCharWidth/4. We call CreateSolidCaret() to actually create the caret. We store
the caret’s position in a new CPoint object named Caretposition. The CPoint class has two data
members, x and y, which will hold the position of the caret. Initially, we set the caret’s position to
(5, 5). Then we set the caret’s position with SetCaretPos(), and show the caret on the screen with
ShowCaret(), and set the Caretcreated Boolean flag to true.
Output:-
Program 7
# Write a program in Microsoft Visual C++ to create a Web Browser.

Steps to create a Dialog Based Application:

1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.
2. No select the MFC AppWizard(exe) entry in the New dialog box.
3. Give the new program a name in the project name box.
4. Click OK to start the Visual C++ AppWizard.
5. Select the option Dialog Based in the AppWizard and click Next.
6. Click on the Finish button now. This opens an Information Box.
7. Click the OK button and the new project has been created.

Steps to create a Web Browser:


1. Select Project menu >> Add to Project >> Components and Controls. This opens the
Visual C++ Components and Controls gallery.

2. Now double-click the entry marked Registered Active X Controls to open the list of
ActiveX Controls in the system.
3. One of the controls in the components and controls gallery should be the Microsoft Web
Browser. Select that and click the Insert button.

4. Visual C++ asks what class you want for this new control; accept the default suggestion:
CWebBrowser2. This adds the web browser control to the dialog editor’s toolbox.
5. Open the main dialog window in the dialog editor. The browser control appears at the
bottom of the toolbox.

6. Drag a new control of that type in the dialog window and resize it. Also add a text box in
the window.

7. Using ClassWizard, connect a member variable to this control now, naming that variable
m_browser. Also add a member variable m_text for the text box.
8. Also add a button with the caption Browse to the dialog window. It will look as:

9. Connect an Event Handler method to the browse button, OnButton2().


10. When the user enters a URL in the text box, this is passed on to m_text which has been left
blank. So, UpdateData() is set to true. Then the user clicks on Browse button. To navigate
our web browser we need Navigate() method. The value of m_text is passed to the
Navigate() as one of the parameters.

Output:
Program 8
# Write a program in Microsoft Visual C++ to create an Active X Control.

Steps to create a MFC ActiveX Application:

1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.

2. Now select the MFC ActiveX ControlWizard entry in the New dialog box.

3. Give the new program a name in the project name box.

4. Click OK to start the Visual C++ MFC ActiveX ControlWizard.

5. Select the option 1 in How many controls would you like your project to have? Click
Next.
6. Click on the Finish button in next window. This opens an Information Box.

7. Click the OK button and the new project has been created.
Drawing and Inserting Active X Control:

1. Here we create a rectangular box, which contains four equal partitions. We do the coding in
the class BoxerCtrl.cpp’s OnDraw() function.

2. Then we set the flags for each of the four partitions. This is done in BoxerCtrl.h.

3. Then we set these flags to false in BoxerCtrl class’s constructor.


4. We add an event handler WM_LBUTTONDOWN using MFC ClassWizard in View
menu.

This will add a method OnLButtonDown() in BoxerCtrl.cpp.

5. Then we set Boolean Fill flags using PtInRect() method.

6. Then we test the ActiveX control. This is done by selecting BuildBoxer.ocx in Build menu
and registering that control with windows.
7. Select ActiveX Control Test Container item in the Tools menu & select Insert New
Control item in container’s edit menu.

8. Double-click the boxer control in the Insert Control Box.


9. Click one of the Boxes in the control now & shade it.
10. Using AppWizard create a new Dialog-Based program boxerapp. Insert a control of boxer
type in it. Select Project -> Add to Project -> Components & controls -> Registered
ActiveX Controls.

11. Now select Boxer Control Entry & insert it.


12. Boxer Control is inserted in the toolbox as OCX. Draw it on the Dialog Window.

13. Compile and Run the project.

Output:
Program 9
# Write a program in Microsoft Visual C++ to create a dialog box and add check boxes and
radio buttons to it, so that when one of them is selected, corresponding message is displayed
in a textbox.

Steps to create a Dialog Based Application:

1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.

2. No select the MFC AppWizard(exe) entry in the New dialog box.


3. Give the new program a name in the project name box.
4. Click OK to start the Visual C++ AppWizard.

5. Select the option Dialog Based in the AppWizard and click Next.
6. Click on the Finish button now. This opens an Information Box.
7. Click the OK button and the new project has been created.

Code:
Adding Check boxes and Radio Buttons:

To add three checkboxes, three radio buttons and a text box, just drag these controls over to the
dialog box from dialog editor’s toolbox. The editor gives the check boxes the captions Check 1,
Check 2, Radio 1, Radio 2, and so on.

Connecting Check boxes and Radio Buttons to code:

We connect the controls to the code using ClassWizard as follows:


1. Open View menu and click on ClassWizard.
2. In ClassWizard box, make sure that Project name is Checks and Class name is
CChecksDlg.
3. Select the Object ID, IDC_CHECK1 and select the Message BN_CLICKED. Then click
on Add Function button. This will add Function OnCheck1() in Member Functions Box.
4. Similarly, add functions for each check box and each radio button by selecting their
respective IDs.
5. Also add a member variable m_text to the text box using Member Variables tab. Select
Category Value and Variable type CString.
Now, in Dlg class, double click on the method OnCheck1() and pass a string “Check 1 clicked”
to m_text and set UpdateData() to false. This means that the data is updated in the text box by
m_text. Similarly, add string to each method corresponding to new controls.

Output:
Program 10
# Write a program in Microsoft Visual C++ to create a dialog box and add check boxes and
radio buttons together using group boxes so that when one radio button is clicked, one or
more checkboxes are automatically selected and a message is displayed in a text box.

Steps to create a Dialog Based Application:

1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.
2. No select the MFC AppWizard(exe) entry in the New dialog box.
3. Give the new program a name in the project name box.
4. Click OK to start the Visual C++ AppWizard.
5. Select the option Dialog Based in the AppWizard and click Next.
6. Click on the Finish button now. This opens an Information Box.
7. Click the OK button and the new project has been created.

Code:

Using Group Boxes:


Group boxes arrange controls both visually and functionally. Here, we add two group boxes, one
for the check boxes and other for the radio buttons. We add group boxes by simply dragging them
from the dialog editor’s toolbox to the dialog box.

To give a group box a new caption:

 Right click the group box in the dialog editor.


 Select Properties in the pop-up menu.
 Type the new name in the caption box.
Adding Checkboxes and Radio Buttons:

Add three radio buttons to Arrangements box and Three Checkboxes to Flowers box. Give
appropriate captions to each control and also add a text box to the program.
Add a member variable m_text to the text box with the help of ClassWizard.
Also add methods OnRadio1()… OnRadio3() for all the radio buttons using ClassWizard.

Adding Member Variables to Check Box Controls:

We can add member variables for check boxes using ClassWizard and then refer to the checkboxes
using those variables.
1. Start ClassWizard now, and select the Member Variables tab.
2. Click the first check box, IDC_CHECK1, and click the Add Variable button. This opens the
Add Member Variable box:

3. Give this new variable the name m_check1, and make sure that Value option in the Category
box and BOOL in Variable Type box is selected.
4. Click OK and do the same for other two checkboxes giving them variables m_check2 and
m_check3.

5. Now we will set the state of various check boxes in radio button’s click event handlers. For
e.g., for OnRadio1():

6. Then we place the price of arrangement in the text box and set UpdateData() to false.
7. Similarly, do the same for other two radio buttons, setting up other flower arrangements:

Output:

You might also like