MFC Static Libraries

 

Introduction

An MFC static library is a library file that includes MFC functions or classes. Such a library natively supports MFC strings (such as CString, CTime, etc), lists, and any other C/C++ or MFC global functions. Because this library is an MFC type, it can be used only by an MFC-based application.

Creation of an MFC Static Library

To create an MFC static library, from the New dialog box, select Win32 Static Library and specify the directory. Then, in the Win32 Static Library - Step 1 of 1, mark the MFC Support check box. You can also ask Microsoft Visual C++ to generate precompiled header files:

  • If you don't mark the Precompiled Header check box, an empty project would be created
  • If you select the Precompiled Header radio button, the wizard will generate a StdAfx.h header file and a StdAfx.cpp source file for the project. The wizard will also include the afx.h and the afxwin.h files in the StdAfx.h header file. You can still add any cenessary header file in StdAfx.h

Practical Learning: Creating an MFC Static Library

  1. To start a new project, on the main menu, click File -> New…
  2. In the New dialog box, click the Projects property page
  3. In the list, click Win32 Static Library
  4. In the Project Name edit box, type MFCExt
     
  5. Click OK
     
  6. In the Win32 Static Library – Step 1 of 1, click both check boxes and click Finish then click OK
  7. To add a new header file, on the main menu, click File -> New…
  8. In the Files property page of the New dialog box, click C/C++ Header File. In the File Name edit box, type mfcextent and click OK
  9. In the empty file, type the following:
     
    #ifndef MFCEXT_H_
    #define MFCEXT_H_
    
    namespace MFCExtensions
    {
    	BOOL IsNatural(const CString Str);
    	BOOL IsNumeric(const CString Str);
    	int StringToInt(const CString Str);
    	double StringToFloat(const CString Str);
    }
    
    #endif // MFCEXT_H
  10. To add a new source file, on the main menu, click File -> New… In the Files property page of the New dialog box, click C++ Source File. In the File Name edit box, type mfcextent and click OK
  11. In the empty file, type the following:
     
    #include "stdafx.h"
    #include " mfcextent.h"
    
    namespace MFCExtensions
    {
    	BOOL IsNatural(const CString Str)
    	{
    		// Check each character of the string
    		// If a character at a certain position is not a digit,
    		// then the string is not a valid natural number
    		for(int i = 0; i < Str.GetLength(); i++)
    		{
    			if( Str[i] < '0' || Str[i] > '9' )
    			return FALSE;
    		}
    
    		return TRUE;
    	}
    
    	BOOL IsNumeric(const CString Str)
    	{
    		// Make a copy of the original string to test it
    		CString WithoutSeparator = Str;
    		// Prepare to test for a natural number
    		// First remove the decimal separator, if any
    		WithoutSeparator.Replace(".", "");
    
    		// If this number were natural, test it
    		// If it is not even a natural number, then it can't be valid
    		if( IsNatural(WithoutSeparator) == FALSE )
    			return FALSE; // Return Invalid Number
    
    		// Set a counter to 0 to counter the number of decimal separators
    		int NumberOfSeparators = 0;
    
    		// Check each charcter in the original string
    		for(int i = 0; i < Str.GetLength(); i++)
    		{
    			// If you find a decimal separator, count it
    			if( Str[i] == '.' )
    				NumberOfSeparators++;
    		}
    
    		// After checking the string and counting the decimal separators
    		// If there is more than one decimal separator,
    		// then this cannot be a valid number
    		if( NumberOfSeparators > 1 )
    			return FALSE; // Return Invalid Number
    		else // Otherwise, this appears to be a valid decimal number
    			return TRUE;
    	}
    
    	int StringToInt(const CString Str)
    	{
    		// First check to see if this is a valid natural number
    		BOOL IsValid = IsNatural(Str);
    
    		// If this number is valid, then convert it
    		if( IsValid == TRUE )
    			return atoi(Str);
    		else
    		{
    			// Return 0 to be nice
    			return 0;
    		}
    	}
    
    	double StringToFloat(const CString Str)
    	{
    		// First check to see if this is a valid number
    		BOOL IsValid = IsNumeric(Str);
    
    		// If this number is valid, then convert it
    		if( IsValid == TRUE )
    			return atof(Str);
    		else
    		{
    			// Return 0 to be nice
    			return 0;
    		}
    	}
    }
  12. To create the library, on the main menu, click Build -> Build MFCExt.lib
    The Output window will indicate that a file with .lib extension was created

 

 

MFC Static Library Test

Like the regular static library we saw above, you can use an MFC static library either on a console or a graphical application. The main difference is that the application must be able use MFC. Therefore, when creating the application, specify that you will use MFC either In A Shared DLL or In A Static DLL.

Practical Learning: Testing an MFC Static Library

  1. To start a new application, on the main menu, click File -> New…
  2. In the Projects tab of the New dialog box, click MFC AppWizard. In the Project Name edit box, type MFCExtTest and click OK
  3. Set the project type to Dialog Based and click Next
  4. Remove the check box of the About Box. Set the Title to 
    MFC Extension Library then click Finish and click OK
  5. Design the dialog box as follows:
     
    Control ID Caption Additional Properties
    Static Text   Number &1:  
    Edit Control IDC_NUMBER1   Align Text: Right
    Static Text   Number &2:  
    Edit Control IDC_NUMBER2   Align Text: Right
    Button IDC_CALCULATE C&alculate Default Button: True
    Static Text   Result:  
    Edit Control IDC_RESULT    Align Text: Right
    Button IDCANCEL &Close  
  6. Press Ctrl + W to access the ClassWizard. In the Member Variables tab, Add a Variable for each edit box as follows:
     
  7. Using Windows Explorer or My Computer, copy the MFCExt.lib and the mfcextent.h files from the folder of the previously created library and paste them in the folder of the current project (the lib and the h files are in different folder but you should paste them in the main folder of the current project)
  8. To include the library in the current project, on the main menu, click Project -> Add To Project -> Files…
  9. Change the Files Of Type to Library Files (.lib). Select the MFCExt.lib file and click OK
  10. Display the dialog box. Double-click the Calculate button. Accept the suggested name of the function and click OK
  11. Implement the event as follows:
     
    // MFCExtTestDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "MFCExtTest.h"
    #include "MFCExtTestDlg.h"
    
    #include "mfcextent.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CMFCExtTestDlg dialog
    
    CMFCExtTestDlg::CMFCExtTestDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMFCExtTestDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CMFCExtTestDlg)
    	m_Number1 = _T("0.00");
    	m_Number2 = _T("0.00");
    	m_Result = _T("0.00");
    	//}}AFX_DATA_INIT
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    . . .
    
    void CMFCExtTestDlg::OnCalculateBtn() 
    {
    	// TODO: Add your control notification handler code here
    	double Number1, Number2, Result;
    
    	UpdateData();
    
    	// Evaluate the content of each operand edit box
    	if( MFCExtensions::IsNumeric(m_Number1) == TRUE )
    	{
    		Number1 = MFCExtensions::StringToFloat(m_Number1);
    	}
    	else
    	{
    		AfxMessageBox("Invalid Number");
    		Number1 = 0.00;
    	}
    
    	if( MFCExtensions::IsNumeric(m_Number2) == TRUE )
    	{
    		Number2 = MFCExtensions::StringToFloat(m_Number2);
    	}
    	else
    	{
    		AfxMessageBox("Invalid Number");
    		Number2 = 0.00;
    	}
    
    	Result = Number1 + Number2;
    
    	m_Result.Format("%.2f", Result);
    	UpdateData(FALSE);
    }
  12. Test the application

 

Previous Copyright © 2003-2006 FunctionX, Inc. Next