The following text is identical to Part 1. If you haven't already read that article, this article will be useless to you, so by all means, catch up. We'll wait here. If you have read the Part 1 article, you can skip these intro sections.

This article series is another in my series of "code we really use" articles. There is no unnecessary discussion about theory, no expounding on technique, and no chest-thumping because I thought it all up myself. It's just a bunch of stuff I did to stand one of our applications up. MOST of the stuff in this article is based on other code that I got from CodeProject, and what follows describes the basis for a project I am actively developing and how I integrated articles and help I got from CodeProject.

Rant

I've been a member on CodeProject for over six years (as of this writing), and I've come to discover some disturbing trends regarding articles. First, article authors tend to post an article and as time goes by, the author essentially abandons the article and people posting questions are greeted with either silence from the author, or a response that says something like "I don't code in this/that language any more". Let's face it, you can't blame them. Many of the articles I use are three or four years old, and I understand that programmers need to move on and that often means completely abandoning older code.

On the other side of the fence are the people that download the source code and samples associated with a given article. Many times, someone will post a question in an article that has absolutely nothing to do with the article itself, but the subject will be related to a certain aspect of the article. As an example, I posted an article about dynamically building a menu. Recently, someone posted a message in that article that asked about adding winhelp to their dynamically built menu. Then there's the people that encounter an issue (real or imagined) with an article, and expect someone else to fix it for them. These people really annoy me. After all, we're all supposed to be programmers here.

So, What's the Point of This Article?

The entire point of this article is to illustrate real-world use of code-snippets, classes and techniques I gleaned from CodeProject over the last six years, including work-arounds to fit code into my sometimes bizarre requirements. Many times, I'll use the VC++ forum to ask a question that will help me understand an article, or massage the article's code for my own use.

Assumptions

The original version of this article started out as a kind of detailed tutorial describing how to use the IDE, and other inane items like that. After a while, I realized this created a huge amount of overhead as far as the article's weight was concerned. Beyond that, I was starting to become bored with the whole thing and I could plainly see that the quality of my writing was beginning to suffer as a result.

The only solution was to start over and make the assumption that you, the user, have a working knowledge of the VS2005 IDE, especially as it relates to creating VC++/MFC applications. This way, we can talk more about the important stuff than suffer through stuff you should already know. I also assume that you have a decent working knowledge of MFC. I'm not saying you have to be an expert, but I assume you can move around in a MFC project without bumping your head on the intricacies of CMainFrame.

Other Stuff

Sprinkled throughout the article, you'll find "Coding Notes". These simply describe the way I do things when coding, and why I do them. They are certainly not requirements by any stretch of the imagination, but they often concern code readability and maintainability. I'm sure that many of you have your own ways of doing things, but please keep comments regarding these issues to a minimum. After all, this article is not about style.

The total process of coding the complete demo application requires just an hour or so (if you know all the steps ahead of time). Writing this article series has taken me DAYS, so don't be put off by it's length.

The html and images for this article is included in the project download, but doesn't include the pretty CodeProject formatting. If you can mentally handle that, you can simply refer to this .HTML file and get on with your programming.

Finally, I know there are folks out there that vote my stuff a 1 simply because it's, well, something I wrote. I request that you be mature and professional and restrict your politics to the soapbox when voting. Remember, you're voting on the article, not on the author.

What We've Already Done

In part one of this article series, we went through the steps of creating a MFC SDI application and making the view a little more interesting by adding the MFC Grid Control to it. In Part 2, we'll create a flat splitter window that can switch views in the primary pane.

Adding a Splitter Window with Swappable Views

Adding a splitter window is really fairly simple in a MFC application, with all of the work is done in the CMainFrame class. Of course, there's a MSDN article available that describes the process of adding a splitter window, but I hate chasing links to find out how to do stuff, and I bet you probably do, too. So, in the interest of just getting the job done, we'll skip the basic splitter, and go right to the one we really want - the flat splitter window.

In the case of my real-world application, I only needed a horizontal splitter, so this discussion is limited to that requirement. Further, I started with Marc Richarme's Flat Splitter Window article, and then added most of the code from Dan Clark's Multi-View Splitter Window article to get the swappable view functionality. This is a perfect example of using two separate articles on CodeProject to create a single specialized class. Because of the combining of code from these two articles, you should probably use the code I provided in my sample project unless you just want to go through the experience of doing the same thing on your own.

In Part 1, we added the MFC Grid Control to the CSDIMultSpliView class (created by the Application Wizard). While this is all fine and dandy, we're about to get a bit fancier with swappable views. Toward this end, we need to create the views that we'll be swapping.

Create New View Class - CPrimaryView

In the subsequent Add Class dialog, select MFC in the Categories tree (left side of dialog box) and then MFC Class in the Templates list (right side of dialog). Click ADD.

In the next dialog box, specify a class name (this sample uses CPrimaryView, and select the base class. For our sample, we'll use CView. Click FINISH.

Move all of the code related to the grid control from the CSDIMultiSplitView class to the CPrimaryView class. You should use the IDE to add the appropriate overrides in this class. If you need detailed instructions, refer to Part 1 of this article series. In short, you need to override OnInitialUpdate and OnCmdMsg, and add message handlers for OnSize and OnEraseBkgnd. Just copy the code from within the resulting functions from CSDIMultiApp1View to this class.

Optional step - remove the grid control code from the CSDIMultiSplit class. We don't need it there because that class is going to be reduced to be just a place holder view for the swappable views. In the sample application provided with this article, I just ifdef'd around the grid control code so that it wouldn't be included when the application was compiled.

Create New View Class - CSecondaryView

This will be a simple GDI view that contains text in the form of a report (actually, it will be a simple collection of lines created and displayed in a for loop). This view will be able to print as well (no easy task as you will soon see).

Since you just did it for the class above, I won't detail the act of creating a new class in this step. When you get the class wizard dialog box, the class name should be CSecondaryView, and it should be derived from CScrollView, as shown below:

Here's a new version of the OnDraw() function to make the view interesting. Don't be alarmed at the number of lines we're putting on the screen because we'll be using this to test the printing functionality we'll be adding a little later.

And to support the font we need, here's the BuildFont function that you have to add to the class. The more-eagle-eyed user may recognize this function as being the same one we used in the from the CFlatSplitterWnd class.

Create New View Class - CInfoView

Following the same steps described above, create another new view class called CInfoView, and once again, set the base class to CSrollView.

Here's some code to make the view more interesting looking. The only difference between this code and the code we used in the CSecondaryView class is the number of lines we're putting on the screen. Since we won't be making this view printable, we don't need as many lines with which to test the view.

Finally, put a copy of the BuildFont() function into this class as well.

Go ahead and check the Solution Explorer pane for the appropriate files (or the ClassView pane if that's what trips your trigger). All that's left to complete this view's basic functionality is to add some code in OnDraw that fills the view up with text. This will server two purposes - providing immediate and gratuitous visual feedback when we switch between this view and the grid view, and setting us up to add some printing functionality a little later in the article. For now, here's a new OnDraw function:

Create New View Class - CSecondaryView

Following the same steps described above, create another new view class called CSecondaryView.

Create New View Class - CInfoView

Following the same steps described above, create another new view class called CInfoView.

Here's some code to make the view more intersting looking.

Implement The Splitter Window with Swappable View Support

One of the tasks for our real-world application was to provide a split view that allows the user to see some specific type of data in another window. The only real requirements provided were that the splitter bar be visible all the time, and that it split the main window horizontally. In the interest of brevity and keeping everything within the intended scope, we'll ignore other kinds of splits and issues regarding the combination of horizontal and vertical splitters.

Beyond the stated requirements, it was left to me to make it happen anyway I wanted. After looking around a bit on CodeProject, I found a good starting point with the FlatSplitterWnd class from Marc Richarme. Essentially, this class provided me with an always-visible flat splitter window, and was my job to make it pretty. I chose to make the splitter bar itself yellow, and put the word "Info" on the bar with two arrowheads pointing down. The idea here was to make it easy to see. Without further delay, here are the steps I followed to implement this feature.

So, first we're going to add the CFlatSplitterWnd files to our project. Once again, I prefer to place files from external sources into their own folders (yes, even if I modify them like we're going to do in this case), so after you've downloaded the FlatSplitterWnd article's source code, extract the FlatSplitterWnd.CPP and FlatSplitterWnd.H files to the folder of your choice (I put them into \SDIMultiSplit\FlatSplit). Since we've performed similar steps when we added the MFC Grid Ctrl, I don't think there's any reason to illustrate how to add files to a project a second time. Just Right click on the SDIMultiSplit project in the Solution Explorer, click Add | Existing... in the context menu, and the browse to the folder in which you placed the FlatSplitterWnd files, and click ADD.

If you put these files into their own folder like I did, you have to add the folder to your project settings Addition include directories. Make sure you do this for all configurations.

Now that we have our files where we want them, let's look at the changes I made to implement my visual styling on the splitter bar. Open FlatSplitterWnd.CPP, and look at the constructor for the class.

The key to my modification lies in the ESplitType parameter. There are four possible values we can expect, but we're only interested in one - splitBar. The revised version of the OnDrawSplitter function (shown below) includes sufficient comments to preclude me from providing further descriptions.

Because we're just copy/pastng from another article into our existing splitter window class, there's no need to actually use any of the files from Dan's article. Well just talk about the functions I copy/pasted, and the changes I made to those functions. Of course, you could alternately just derive this class from CFlatSplitterWnd (or vice-versa), but I personally don't like a lot of files, not to mention taxing VS2005's ability to keep up with everything (the more files/classes you have, the longer it takes the IDE to perform certain funcitons, like updating intellisense).

The AddSwitchableView function is called from the CMainFrame OnCreateClient() function, and is called for each view you want to make swappable. When I first looked at this code, I decided there were slight improvements that could be made, so these functions don't quite match Dan's article. Changes to the original code are noted in the comments.

As you can see, these functions merely return a pointer to the active view that is indicated in the name of the function. Since we're using dynamic_cast, the pointer will be NULL if the view returned is not of the type specified between the < > symbols.

Now that we have all of our view classes coded and the splitter window implemented, we need to provide the user wth a way to switch the views.

Menus Make It Go

The user will be able to switch between the Primary View and the Secondary View via menu items. Adding menus in a MFC app is fairly trivial.

In the IDE's Resource View, open up the resource, expand the tree until you see a list of resource categories, expand the Menu item, and double-click IDR_MAINFRAME. A new window will open in the IDE showing the program's current menu.

Find the View Item, click it, and then right-click on the line that reads "Type Here" (just beneath the Status Bar item).

Right click the button and select the Insert Separator item from the subsequent menu. A separator line will be added to the menu, and a new "Type Here" line will appear below the separator.

Click on the new Type Here item, and type in "Primary View". Notice that a new "Type Here" item is added directly below.

Click on the new Type Here item, and type in "Secondary View".

Go back and right-click on the Primary View item and select Add Event Handler... from the menu.

The Event Handler Wizard dialog box should now be displayed (see below). This dialog allows you to set one event handler at a time. Notice there are two possible events we can set. We need them both for the sample but since we can only do one at a time, we'll pick ON_COMMAND. Make double-damn sure that you've selected the correct class to which we are adding this handler - it MUST be CMainFrame. You'll have to repeat the previous step to get back here to set a handler for ON_COMMAND_UI. Again, make sure that you add the handler to the correct class.

Repeat the last two steps for the Secondary View menu item. If you performed the previous steps correctly, you should see the following functions at the bottom of your CMainFrame class:

Compile The App

Rebuild the solution, and run the app. The 1st image shows the app the way it's initally displayed. The 2nd screenshot shows what the app looks like when you click/drag the splitter bar. The 3rd image illustrates the menu selection of the Secondary view, and the 4th image shows the secondary view.

What's Next?

In Part 3, we'll be adding a custom status bar class and some multi-threading components, again using articles found here on CodeProject.

End of Part 2

Due to the length of this article, I've decided to break it up into several parts. If the site editors did what I asked, all of the subsequent parts should be in the same code section of the site. Each part has it's own source code, so as you read the subsequent parts, make sure you download the source code for that part (unless you're doing manually all the stuff I'm outlining in the article in question).

In the interest of maintaining some cohesiveness (and sanity), please vote on all of the parts, and vote the same way. This helps keep the articles together in the section. Thanks for understanding.

Share

About the Author

I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my telephone."-Bjarne Stroustrup, computer science professor, designer of C++
programming language (1950- )