Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/articles/mfc-tutorials/)
-   -   Win32 vs. MFC (http://www.go4expert.com/articles/win32-vs-mfc-t2533/)

Sanskruti 12Jan2007 18:48

Win32 vs. MFC
 

Software Development Kit (SDK)



It is a set of tools designed to help C programmers create Windows applications.

Windows SDK consists of the large set of books describing functions, messages, structures, macros and resources .There are also tools including a dialog editor and an image editor, on-line help files and a set of windows libraries and header files. It has large collection of API functions written in C.

What is an API?



API is an acronym for Application Programming Interface.It is simply a set of functions that are part of Windows OS. Programs can be created by calling the functions present in the API.The programmer doesn’t have to bother about the internal working of the functions .By just knowing function prototype and return value he can invoke the API functions. A good understanding of Windows API would help you to become a good Windows programmer .Windows itself uses the API to perform its GUI magic.
Windows APIs are of two basic varieties:
API for 16-bit Windows (Win16 API)
API for 32-bit Windows (Win32 API)
Each of these have sub-APIs within it. If you are working in Windows 3.1 then you have to use Win16 API and in Windows NT you have to use Win32 API.

Microsoft Foundation Class (MFC)



VC++ provides us a rich class library called the MFC Library, which is a set of C++ classes that encapsulate the functionality of applications written for the Microsoft Windows Operating System. The class library gives you a complete application framework .The framework defines an architecture for integrating user interface of an application for Windows with the rest of the application. It also provides implementations for a set of the user-interface components.MFC represents entire family of class libraries.MFC offers high level of abstraction that lets you focus in details to your application while allowing its classes to be customized and extended.MFC makes programming in Windows and C++ a much more productive endeavour.It is a collection of Windows API calls which are written in C.Programming in MFC reduces the code to a considerable extent as compared to SDK.

Let ‘s look at the program that creates simple window using MFC.



Code: CPP

#include <afxwin.h>
//The  CWinApp class is the base class from which you derive
//a Windows  application  object.
//An application  object provides member functions  for  initializing your application
//(and each instance of it) and for running the application

class mywin : public CFrameWnd
{
public :
    myWin()
    {
        Create (0,”HELLO – WINDOW”) ;
    }
};

class  myapp     :  public    CWinApp
{
public:
   
    // Override InitInstance to initialize each new  instance of your application running
    //under   Windows.Typically ,you  override  InitInstance  to construct  your main window //object and set the CWinThread : : m_pMainWnd  data member to point to that //window.
   
    int   InitInstance()
    {
        mywin     *f ;
        f = new   mywin;
        f - >ShowWindow(3);
        m_pMainWnd  = f;
        return  1;
    }
};

myapp app;

Lets see the same program to create a window in SDK as we did with MFC.

Code: CPP

#  include  <windows.h>
HWND  hwnd;
MSG  msg;
WNDCLASS   wnd;

long WINAPI  process(HWND,UINT,WPARAM,LPARAM);
int WINAPI  WinMain(HINSTANCE   h,HINSTANCE   p, LPSTR   s , int  show)
{
    wnd.hInstance=h;
    wnd.lpfnWndProc=process;
    wnd.lpszClassName="first";
    wnd.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    RegisterClass(&wnd) ;
    hwnd=CreateWindow("first","SDK-Window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,h,0);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    While(GetMessage(&msg,0,0,0))
    {
        DispatchMessage(&msg) ;
    }
    return msg.wParam;
   
}
long  WINAPI process(HWND hd,UINT mess,WPARAM more,LPARAM pos)
{
    Switch(mess)
       
    {
        case   WM_CREATE:MessageBox(hd,"CREATING","wait!",0)  ;
            break;   
        case   WM_ClOSE:MessageBox(hd,"Closing","wait",0);
            break;
        case    WM_DESTROY:MessageBox(hd,"destroying","wait",0);
            PostQuitMessage(0);
            break;
    }
    Return DefWindowProc(hd,mess,more,pos);
}

Comparing MFC vs SDK program



Thus we can see that programming in MFC reduces the code to a considerable extent as compared to SDK.

This is because in SDK we have to write the whole code right from scratch,i.e,we need to write each and every function.Whereas in case of an MFC program the MFC does lot of things on behalf of the user, i.e we need not write each and every function explicitly.

parvez.yu 6Mar2008 15:00

Re: Win32 vs. MFC
 
good one


All times are GMT +5.5. The time now is 07:25.