VC++ (RSS)

VC++

VC++ elements and Windows Message Processing

Today’s class was pretty much boring because all that we learnt was “The Programming Elements in VC++”. It is the plain old stuff. I was curious to know more about Windows Message Processing. I had once read about it in “Visual Basic 6.0 Bible”. The context was the difference between event-based programming platform (like MS VB 6.0) and something like VC++, where we deal with Windows Messages directly. So I just collected some info about Windows Messages and I’m including the same here.

Programming elements in VC++

1. Compiler
2. Source code editor
3. Class view window
4. Debugger
5. Linker
6. Resource view window

1. Compiler: Used to compile the source code written in C/C++
2. Source code editor: It is a window wherein you can type the source code of an MFC Application
3. Class view window: The classes created can be hierarchically viewed here
4. Debugger: Used to debug the program. It is displayed on the bottom panel and once the error is clicked, the source code editor, points to the corresponding line. This makes debugging easier
5.  Linker: Links the resource files and the source code and creates an EXE
6. Resource view window: Displays the various resources used in the MFC application. Eg.: Bitmaps, Icons etc.

Windows Message Processing

I am sure you would have written at least one C program by now. It is known that the operating system calls the main function when the program is run. So this is a pre-requisite for the operating system to identify the starting point of execution of the program. Thereon, we write the application in the structure we want. If we have to get an input from the user in the C program, we use getchar. The program uses the calls appropriate functions to avail the services provided by the OS.

When Windows launches a program, it calls the WinMain function. So all our Windows-based application WILL have a function called WinMain defined somewhere or the other. Some programming platforms like VB and VC++ hide it from the developer to redude complexity. The important task of WinMain is to create the application window. In case of VC++, the structured message handling process is also hidden to reduce the code complexity and allows the developer to concentrate on more niche areas.

The main difference between an MS DOS based application and a Windows-based application is that the MS DOS program calls the OS (or its system calls) to get user input, whereas in case of Windows programs receive input via “messages” from the OS.

Examples of some Windows Messages

WM_CREATE: message is sent when a window is being created
WM_LBUTTONDOWN: message is sent when the user presses the left mouse button
WM_CHAR: message is sent when the user types a character
WM_CLOSE: message is sent when the user closes a window

Creating a simple MFC application

1. Run AppWizard to generate SDI application source code. Choose New from Visual C++'s File menu, and then click the Projects tab in the resulting New dialog box, as shown here.
2. Type TestApp as shown in the Project Name edit box, and then click the OK button. Now you will step through a sequence of AppWizard screens
3. Select the Single Document option. Accept the defaults in the next four screens.
4. Click the Finish button. Just before AppWizard generates your code, it displays the New Project Information dialog box.
5. Use F7 to compile and F5 to run the EXE

The The CTestAppView View Class
AppWizard generates the CTestAppView view class, and this class is specific to the TestApp application. (AppWizard generates classes based on the project name you entered in the first AppWizard dialog box.) CTestAppView is at the bottom of a long inheritance chain of MFC library classes.

Drawing an object inside the View Window

The OnDraw Member Function
OnDraw is a virtual member function of the CView class that the application framework calls every time the view window needs to be repainted (This wasn’t known to me until I fetched it from Google).

The Windows Device Context
Windows doesn't allow direct access to the display hardware but communicates through an abstraction called a "device context" that is associated with the window. In the MFC library, the device context is a C++ object of class CDC (I think it stands for C Device Context) that is passed (by reference) as a parameter to OnDraw. After you have the device context pointer, you can call the many CDC member functions that do the work of drawing.

Edit the OnDraw function in TestAppView.cpp. Find the AppWizard-generated OnDraw function in TestAppView.cpp:

void CEx03aView::OnDraw(CDC* pDC)
{
    pDC->TextOut(0, 0, "Hello, world!");  // prints in default font & size, top left corner
    pDC->SelectStockObject(GRAY_BRUSH);   // selects a brush for the interior
    pDC->Ellipse(CRect(0, 20, 100, 120)); // draws a gray circle 100 units in diameter
}

Just hit F7 and compile and run the application by pressing F5. Yo! That’s it for now.

with 1 Comments

Microsoft Visual C++ 6.0

I have joined a course for MS VC++. I’ve thought of putting up stuff o’er here as and when I am learning. I have classes on Saturday & Sunday. So, I’ll be summarizing stuff what I learnt for the day.

Today it was a class on basics of VC++ and its architecture. The topics that are going to be covered as a part of the course are:
1. Intro to MFC
2. MFC Base classes and Dialog boxes
3. MFC Applications
4. Intro to Com
5. COM objects and libraries
6. COM Interfaces
7. ActiveX Template Library
8. Creation of COM client
9. Automation and dual interfaces
10. ActiveX controls
11. Data access with ADO
12. Marshalling and COM threading models
13. ActiveX server components
14. COM using Microsoft Transaction Server
15. COM+ and Windows DNA

Class 1: Introduction to MFC

Overview of MFC
• MFC class library is an application framework for writing applications for Windows platform or any other platform that supports Win32 APIs
• It is a collection of C++ classes or in other words MFC is an encapsulation of large portion of Windows API in C++ form
• Using MFCs, we can create Windows dialog boxes, device context and other common graphic objects
• Classes provided by MFCs are accessed through interfaces. MFC code is compiled and converted into Win32 API calls.

Execution of MFC
There are two steps of execution:
1. Code compilation
2. Resource compilation

Code compilation: The source code is compiled along with the required libraries (windows headers and runtime header files) and an obj file is created.

Resource compilation: Resource files like images, icons, bitmaps etc compiled separately and a RES file is obtained.

The outputs of both the compilers (code and resource) are linked together and the final executable file is obtained.

MFC Base Classes
There are two main base classes in MFC:
1. cWinApp
2. cFrameWnd

cWinApp class: This is the base class from which a windows application object is derived. An application object consists of member functions for initializing applications. cWinApp class provides3 methods:

1. InitInstance(): This method is called when the application object is loaded into the memory.

2. Run(): This method acquires tand dispatches windows calls until the application receives the WM_QUIT (a constant), message. (ie. when “X” button is pressed)

3. ExitInstance(): It is called when an application object is removed from the memory


Just a sample program to create a frame and display a message box when InitInstance() is invoked

#include<afxwin.h>

class MyWindow : Public cFrameWnd
{
 Public:
  MyWindow()
  {
   // NULL is for icon
   create(NULL, “Sample window”);
}
~MyWindow()
{
 messageBox(“Sample window”, “cFrameWnd”);
}
};

class MyApp : Public cWinApp
{
 Public:
  // Override these methods
  BOOL InitInstance();
  BOOL ExitInstance();
}

BOOL MyApp :: InitInstance()
{
 :: messageBox(o, “Sample Window”, “InitInstance”, MB_OK | MB_ICONASTERIX);
 
 MyWindow mywinobj;
 Mywinobj = new MyWindow;

 // Speicify the main window
 M_PMainWnd = mywinobj;

 Mywinobj->showwindow(SW_SHOWMAXIMIZED);
return TRUE;
}

with 1 Comments