VP Lab
VP Lab
# Write a program in Visual C++ to create a DLL Application and link it to a Visual Basic
form.
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:
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.
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.
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.
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.
Output:
INDEX
S.No Name of the Program Date Sign
1. WAP in VC++ to create a DLL application.
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.
5. Select
the
option
Single
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:
Initializing in Constructor:
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:
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
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:
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:
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”.
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.
Double click this new menu item, opening the Menu Properties Box.
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.
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).
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:
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:
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:
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
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.
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.
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:
Output:
Program 8
# Write a program in Microsoft Visual C++ to create an Active X Control.
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.
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.
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.
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.
1. Open Visual C++ and click the New item in the File menu, opening the New dialog box.
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.
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.
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:
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.
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: