Creating a Rich Client Application with MFC
Creating a Rich Client Application with MFC
The following walkthrough describes one procedure for creating an MFC-based rich client application and various
other features of Visual Studio .NET.
During the course of this walkthrough, you will accomplish the following activities:
Design an MFC-based solution that displays data from an existing database and displays the following features
using several dialog boxes:
Hosting a windowless ActiveX control
Using dynamic HTML (DHTML) dialog boxes
Validating the application user using an existing XML Web service.
To complete the walkthrough, you must provide an existing database and at least one searchable table.
Note:
This walkthrough uses the pubs database and authors table as an example database and table.
Using the MFC Application Wizard, you will modify your project to support the database access and display
features of the rich client application.
Note:
This walkthrough assumes that the server being accessed is a SQL server and that the pubs database
exists on this server.
In this section, you will use a simple form and control to demonstrate the access and display of data from the
authors table. Because this is only a demonstration, the query and display are simple. The results of the
query (all authors in the table) are displayed in a list box.
The MyProject application already contains a form object (called IDD_MYPROJECT_FORM), located under the
Dialog node in Resource View. You will modify this form to display a simple list of authors from the authors
table.
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012
Walkthrough: Creating a Rich Client Application with MFC Página 2 de 7
object (IDD_MYPROJECT_FORM).
2. Drag a List Box control onto the default form.
3. Right-click the List Box control and, on the shortcut menu, click Add Variable.
The Add Member Variable Wizard appears.
4. In the Variable name box, enter m_DataList and click Finish.
5. Drag a Button control onto the default form. In the Properties window, change the Caption box to
Control Host.
6. Drag a Button control onto the default form. In the Properties window, change the Caption box to
DHTML Dialog.
Note:
These two controls will be used later in the walkthrough to access other features of the application.
Copy Code
HRESULT hr = S_OK;
TCHAR szAuthor[80];
while(cmd.MoveNext() == S_OK)
m_DataList.InsertString(-1, szAuthor);
This code uses the db_command attribute to initialize the data set of the document object
(m_MyProjectSet) with the last names of all current authors in the table.
3. On the Build menu, click Build Solution.
4. On the Debug menu, click Start Without Debugging.
The results of the query will be displayed in the list box of the child view form.
The next modification to the rich client application is a dialog box that hosts a simple windowless custom
control. The first step is to create a custom ActiveX control with a simple event. Then a dialog box object is
created containing the control and handling the control event.
Once the control is created, you will add a simple event called Click. This event is fired by the control
whenever the mouse is clicked within the control area.
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012
Walkthrough: Creating a Rich Client Application with MFC Página 3 de 7
Copy Code
Click();
return 0;
9. On the Build menu, click Build Solution.
Once the solution builds successfully, a simple dialog box can host the control.
Copy Code
The final step involves hooking the dialog box to the rich client application. This is accomplished with code in
the handler for the Control Host button you created earlier, in the topic Accessing and Displaying Data
from an Existing Database.
Copy Code
CMyCtlDlg dlg;
dlg.DoModal( );
4. Add the following code after the last #include statement in the current source file:
Copy Code
#include "MyCtlDlg.h"
This includes the .h file of the class implementing the control host dialog box.
5. On the Build menu, click Build Solution.
6. On the Debug menu, click Start Without Debugging.
You can invoke the control host dialog box by pressing the Control Host button. Fire the custom Click event
by clicking the left mouse button within the control.
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012
Walkthrough: Creating a Rich Client Application with MFC Página 4 de 7
Another feature of rich client applications is the usage of dialog boxes that use HTML for the user interface
rather than traditional dialog resources. For the purposes of this walkthrough, a simple DHTML dialog box will
be implemented, containing an image control that displays a simple bitmap.
As with the control host dialog implemented earlier, this dialog box will be displayed when the user presses a
button (DHTML Dialog) on the main form of the application.
Copy Code
CMyDhtmlDlg dlg;
dlg.DoModal( );
4. Add the following code after the last #include statement in the current source file:
Copy Code
#include "MyDhtmlDlg.h"
This includes the .h file of the class implementing the DHTML dialog box.
5. On the Build menu, click Build Solution.
6. On the Debug menu, click Start Without Debugging.
When the dialog appears, open the DHTML dialog box by pressing the DHTML Dialog button.
For more information on DHTML dialogs and a more complete example, see the CDHtmlDialog class and the
DHtmlExplore sample.
Occasionally, a rich client application interacts with an external XML Web service by providing a rich front-end
to an existing database or databases. The user is then able to interact with the data in a familiar or graphical
manner.
In this step, you will create a simple XML Web service designed to run on a Web server using Microsoft
Internet Information Services (IIS).
A common part of XML Web services is to verify each user of the application. Once you create the solution, you
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012
Walkthrough: Creating a Rich Client Application with MFC Página 5 de 7
can implement a simple validation method. This validation routine is deliberately simple to illustrate the
concept clearly.
Once the solution has been created, add a validation method to the Service1.asmx source file. To do this,
right-click the Service1.asmx.cs design surface and select View Code. Replace the HelloWorld web
method at the end of the file with the following code:
Copy Code
// C#
[WebMethod]
public bool Validate(string s1, string s2)
{
return s1 == s2;
}
After you have modified the source file, build the solution.
Once the XML Web service exists, you can add and configure a Web reference with the Add Web Reference
dialog box.
Copy Code
http://localhost/MyService/Service1.asmx
4. Click the Add Reference button.
Once the Web reference has been added, you will add a validation dialog box to demonstrate the interaction
between the application and the XML Web service.
Now that the dialog box has been created, add controls to provide a validation service for the user.
Copy Code
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012
Walkthrough: Creating a Rich Client Application with MFC Página 6 de 7
UpdateData(TRUE);
bool result = false;
s->Validate(CComBSTR(m_Name), CComBSTR(m_Password), &result);
if (result)
CDialog::OnOK();
11. Add the following code after the last #include statement in the current source file:
Copy Code
#include "WebService.h"
This includes the .h file of the Web service used by the validation dialog box.
For the validation routine to be effective, the validation dialog box must be the first user interface object to
appear. If the user enters the correct name and password, the rich client application will be displayed. If an
incorrect name or password is entered, the current user will be prevented from accessing the rich client
application.
To implement this behavior, modify the InitInstance function of the main application class to call this dialog
box first. The application continues only when the dialog box correctly exits.
Copy Code
Copy Code
#include "MyValidateDlg.h"
This includes the .h file of the validation dialog box.
5. Build the solution. On the Build menu, click Build Solution.
6. On the Debug menu, click Start Without Debugging. In the verification dialog box, enter the same
text in the Name and Password edit boxes, then click OK. The rich client application will then run,
displaying its main dialog box.
The final step in developing your Visual C++ application is creating a setup project.
The resulting file (MySetup) can be copied to a target machine for installation of the rich client application. For
more information on deployment projects and the Setup Wizard, see Deploying Applications, Deployment
Projects, and Deployment Walkthroughs.
See Also
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012
Walkthrough: Creating a Rich Client Application with MFC Página 7 de 7
Concepts
Rich Client Application Walkthroughs
Visual Studio Walkthroughs
ms-help://MS.MSDNQTR.v90.en/dv_vswalk/html/ff258cfd-ccf1-4986-815e-e586809b9f51.htm 14/09/2012