26288 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
STROKE my mouse to
make apps pop, says
Microsoft
Oz shared services
collapse looks bad
for NetApp
Googlerola loses
bid to ban US Xbox
sales after ITC
slapdown
Samsung, carriers
tout first Tizen
mobes for late 2013
Google to double
encryption key
lengths for SSL
certs by year"s end
Facebook Home phone
plans canned in the
UK
Joyent cuts prices
on cloudy
infrastructure
Yahoo! continues
quest for youth
with yet another
acquisition
Internet2 superfast
boffin network
peers with Azure
cloud
Google slashes App
Engine NoSQL data
storage prices by
25 per cent
Slashdot
Cockroaches
Evolving To Avoid
Roach Motels
Meet the 23-Ton
X-Wing, the World"s
Largest Lego Model
Android Malware
Intercepts Text
Messages, Forwards
To Criminals
Scientists Growing
New Crystals To
Make LED Lights
Better
Google Takes Street
View To the
Galapagos Islands
Bitcoin"s Success
With Investors
Alienates Earliest
Adopters
WIPO Panel Says Ron
Paul Guilty of
Reverse Domain Name
Hijacking
Red Hat"s Diane
Mueller Talks About
OpenShift (Video)
5-Pound UAV Flies
For 50 Minutes,
Streams HD From
Over 3 Miles
Google Code
Deprecates Download
Service For Project
Hosting
Article viewer

Introduction To MFC



Written by:bitshift
Published by:bb
Published on:2004-07-01 05:07:08
Topic:C++
Search OSI about C++.More articles by bitshift.
 viewed 17924 times send this article printer friendly

Digg this!
    Rate this article :
This Article Provides An Introduction To Using The Microsoft Foundation Class or MFC.

Using MFC:
============

Many people think that MFC is a very complex and difficult API. So I've decided to write this article to demystify it.

The first thing you must do when writing an MFC application is define a class for the window you will be creating, so allow me to show you the source code, and then explain it:

#include <afxwin.h>
#include <atlbase.h>
#include <afxtempl.h>

class CSampleWindow : public CFrameWnd {
public:
    CSampleWindow();
protected:
    int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnClose();

    DECLARE_MESSAGE_MAP();
};

BEGIN_MESSAGE_MAP(CSampleWindow, CFrameWnd)
        ON_WM_CREATE()
        ON_WM_CLOSE()
END_MESSAGE_MAP()

CSampleWindow::COpenGLWindow() {
    Create(NULL, "Sample MFC Window", WS_OVERLAPPEDWINDOW, rectDefault);
}

int CSampleWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) {
    if(CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;

    ShowWindow(SW_SHOW);
    return 0;
}

void CSampleWindow::OnClose() {
    DestroyWindow();
}


This code is fairly simple, and much of the Win32 API obfuscation is hidden in the code:

BEGIN_MESSAGE_MAP(CSampleWindow, CFrameWnd)
        ON_WM_CREATE()
        ON_WM_CLOSE()
END_MESSAGE_MAP()


This says that our CSampleWindow application which is a form of a CFrameWnd will handle the messages WM_CREATE and WM_CLOSE. These messages are handled through the pre-defined functions OnCreate(LPCREATESTRUCT lpCreateStruct) and OnClose(). Those of you who have programmed Win32 API will probably recognize these messages.

Now Lets Examine The Constructor and OnCreate().

The constructor calls the CFrameWnd function Create which is inherited by our class. Then we declare our window name, its properties, and a CRect structure defining its size. The variable rectDefault is also inherited from CFrameWnd, and makes up a 320x240 window, of course you can modify this to whatever you'd like.

The OnCreate function handles the create window message. All our version of this function does is pass off window creation to CFrameWnd::OnCreate and then set the window Show Window mode to SW_SHOW, this is to make the window visible.

Now that was quite simple and painless, was it not? In fact you probably didn't even need my explanations of the code. Now lets see how we can get a window showing. Lets append a little something to our previous source code:

#include <afxwin.h>
#include <atlbase.h>
#include <afxtempl.h>

class CSampleWindow : public CFrameWnd {
public:
    CSampleWindow();
protected:
    int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnClose();

    DECLARE_MESSAGE_MAP();
};

BEGIN_MESSAGE_MAP(CSampleWindow, CFrameWnd)
        ON_WM_CREATE()
        ON_WM_CLOSE()
END_MESSAGE_MAP()

CSampleWindow::COpenGLWindow() {
    Create(NULL, "Sample MFC Window", WS_OVERLAPPEDWINDOW, rectDefault);
}

int CSampleWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) {
    if(CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;

    ShowWindow(SW_SHOW);
    return 0;
}

void CSampleWindow::OnClose() {
    DestroyWindow();
}

/////////////////////////////////////////

class CSampleApp : public CWinApp {
public:
    virtual BOOL InitInstance();
};

BOOL CSampleApp::InitInstance() {
    m_pMainWnd = new CSampleWindow();
    return TRUE;
}

CSampleApp sampleApp;


In the function InitInstance, which is called from the CSampleApp constructor, we create an instance of our CSampleWindow which we place into the variable m_pMainWnd which is inherited from CWinApp. Finally we create an instance of CSampleApp. This is officially enough source code to display a window.

NOTE: When trying to compile this in MSVC you may come up against a linker error. Simply go to Project -> Settings, and make sure you are using "Use MFC in a shared DLL".

Hopefully this has helped you get started with MFC.

Did you like this article? There are hundreds more.

Comments:
Anonymous
2007-08-24 10:22:24
I don't know if this is common knowledge or if i am on working on a freak system (VC7 on Win NT) but
to make this work, i had to write CSampleWindow instead of COpenGLWindow() (we're using the contructor, after all), and _T("Sample MFC Window") instead of "Sample MFC Window" in the argument of Create(). I am new to this and wasted precious hours finding out.
cheers, m
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
harry
Blog entry for Thu 28th Sep 12pm on Thu 28th Sep 12pm
Hi In school i want to net send my mates but hide who its coming off any ideas. no programs though as the machine in school sets off an alarm if it detects any batch files etc... thanks Harry


     
Your Ad Here
 
Copyright Open Source Institute, 2006