The first day:
WINDOWS application must be WinMain function, which is used to complete some specific task, like creating the main window, the main window code to handle the message. MFC WinMain hidden in the framework will not SDK to write programs as you can easily find it.
WINDOWS message handling mechanism used by the program to the framework, we do not have to worry about how to link these messages and codes. And some news WINDOWS defined, when the window When the system is created WM_CREATE message is sent when they click the left mouse button the system will send WM_LBUTTONDOWN message, when the user presses the keyboard, the system will send WM_CHAR message, when the user closes the window, the system will send a WM_CLOSE message, when the user menu options or click the button to send a WM_COMMAND message when the system back, do nothing the system will send WM_TIMER message. I will not speak anything else, first find out that these messages say. from MSDN can easily find on this several messages with the help of documentation.
WINDOWS provide a common graphical device interface (GUI), we have by calling (GDI) functions, and hardware to deal with, without regard to equipment, environment, WINDOWS environment, the device automatically mapped to the corresponding physical structure, equipment This should be the device-independent nature of it.
dynamic link library (DLL) should be a classic example of code reuse (do not know can not say), the number of modules, their own separate class of newly commissioned and compiled into a DLL, and increase the readability of the code also increased the flexibility of the program modules.
Developer Studio create the project with the Developer Studio will create a lot of intermediate files that still need to talk about the.
APS / / support ResourceView < br> BSC / / browser information file
CLW / / support ClassWizard
DSP / / project file can not be deleted, and with a text editor to edit
DSW / / work space files, can not be deleted, and with a text editor to edit
MAK / / create the file outside of
NCB / / support ClassView
OPT / / save the workspace configuration
PLG / / create the log file
the role of these documents have a certain , take CLW, if you have a new class, you need to add ClassWizard, in addition to the corresponding. h and. cpp added to the DSP CLW.PLG also need to recompile your project file records the configuration information.
VC + + of the source browser will enable us to class or function from the perspective of knowledge or editing program, rather than directly from the file start. in other people's source code to see if the source code of proficiency in the use of the browser will be less effective. Source The main program view the status of the browser are the following:
Definitions and Referencesmm select any functions, variables, types, macros can see it in the definition of the project, and where and where to use it. < br> Call Graph / Caller Graphmm for the selected function, given its function of calling and being called an icon.
Derived Class Graph / Base Class Graphmm gives graphical representation of class hierarchy, you can see the selected class and derived class and base class members.
File Outlinemm for the selected file, listing file classes, functions and data members, and also shows the location of the definition and use of their position.
visible Source Brower than the Class View to feature a lot more and more easy to use, before I do not know, because VC + + book in general did not say or no attention to this and see about the menu, user interface, the editor always skip thought, +6 using AppWizard generates an empty procedure, and then try to look at all those files, and their class hierarchy, functions, macros, structure definitions, I was so dry, not hands-on learning programming is not enough. < br> The next day: Microsoft Foundation Classes application framework
MFC is a C + +, Microsoft Windows API, if you want to develop applications WINDOWS course, VC / MFC is the development environment of choice.
MFC applications created a standardized structure. (I still can not see the advantage of this, please master guiding)
MFC application that produces short, fast running speed. It should be said is that it can easily create a dynamic connection, in fact, procedures still need a lot of DLL, but because there are a lot WINDOWS DLL so the application can be very short, I think so I do not know of any.
VC + + tool reduces the coding complexity. This is not to be questioned than the TC convenient too much.
MFC library functions is very rich. the book lists the MFC from 1.0 - 4.21 Some of the features, I do not talk nonsense, we should look at.
This section introduces the MFC advantage of the library, in fact, I think that in certain circumstances as a matter of fact that MFC is not necessarily as good. I want no one to do with pure VC MIS system, it, too tired. I understand this is programmed, If the school program as Weapons words, C / C + + language and programming ideas (OOP) is the internal strength, API is the basic skills (programming idea is that internal strength, API is the basic skills that apply to any WIN32 programming, regardless of Visual C + +, Delphi, C + + Builder, VB ......), VC / MFC martial arts one of them should be different, different learning methods are not effective as long as they make an effort can also be reached a certain level. the real master is to have a profound internal strength, solid basic skills, as martial arts, does not matter, not the small non-random one-stop eight-no flaws, no stroke victory has recruited. apologies to the topic.
C + + class library to be extended through, we can use in addition to compiler provided with the library, can also be easy to use library software companies sell products, or even their own development. and the application framework is a superset of the class library, which defines the structure of the program.
the following to the two sample programs (one is a book I wrote):
Raytheon Recommendation: Although a lot of books with CD, CD on the source code for all examples in the book, but still should be used in person at the VC6 hand and struck into it. This can enhance the impression and feel the correct compile debug programs running after the fun, because it is handmade. Ever since I knocked nearly a hundred code samples, due to clerical error in the BUG on the few, typing speed also increased. and preferably in the original sample code to make some changes on the basis of other monotony of such displays HELLO WORLD! for something else, this can also deepen the understanding of the sample program.
HELLO WORLD I used AppWizard Wizard to create a single document to display a string program, just typing one line statement, mainly to experience the power of MFC.
1, open the VC + +6 from the menu, select NEW, the project named .
2, select the MFC AppWizard [exe] option, in addition to select a single document outside STEP 1 STEP other default.
3, in the Class View selection CMyApp01View class OnDraw () member function in C + + double-click the compiler see the following
void CMyApp01View:: OnDraw (CDC * pDC)
{
CMyApp01Doc * pDoc = GetDocument ();
ASSERT_VALID (pDoc);
/ / TODO: add draw code for native data here
}
in the / / TODO: add draw code for native data here to increase the position of a line of code
void CMyApp01View:: OnDraw (CDC * pDC)
{
CMyApp01Doc * pDoc = GetDocument ();
ASSERT_VALID (pDoc);
pDC-> TextOut (10,10, add draw code for native data here
}
done, simple as that. compile and run. see that? WINDOWS This program has all the features of the program, such as menus, toolbars, status bar, maximize, close and even on the dialog, print preview ..... all, this is the MFC AppWizard created dynamically by an application. From this small example can be seen with the VC / MFC program designed WINDOWS how convenient. Now we see examples of the book in order to further understand the application framework.
book example:
1, first create a Win32 Application application.
2, select Project-> Add to project -> Files, create a file called MyApp.h respectively, and a file named MyApp.cpp.
3, add the code: (best according to the following code to the compiler knock, do not use Ctrl + C / Ctrl + V)
/ / MyApp.h
/ /
class CMyApp: public CWinApp / / see below ②
{
public:
virtual BOOL InitInstance ();
} ;
class CMyFrame: public CFrameWnd
{
public:
CMyFrame ();
protected:
afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
afx_msg void OnPaint ();
DECLARE_MESSAGE_MAP ()
};
/ / MyApp.cpp
/ /
# include br> CMyApp theApp; / / create a CMyAPP object below ②
BOOL CMyApp:: InitInstance ()
{
m_pMainWnd = new CMyFrame ();
m_pMainWnd-> ShowWindow (m_nCmdShow);
m_pMainWnd-> UpdateWindow ();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMyFrame, CFrameWnd)
ON_WM_LBUTTONDOWN ()
ON_WM_PAINT ()
END_MESSAGE_MAP ()
CMyFrame:: CMyFrame () {
Create (NULL, )
{
CPaintDC dc (this);
dc.TextOut (0,0, it? MFC had not added the support options in Project Setting General Properties page select the Some elements of the program.
① WinMain function: not only does not exist have been hidden within the framework of the application.
② CMyApp categories: CMyApp class object represents an application, CWinApp base class to determine its most behavior.
③ start the application: When running an application calls the WinMain function WINDOWS, WinMain will find the application of the global object theApp.
④ CMyApp:: InitInstance member function: that is automatically called after the overloaded theApp virtual function InitInstance to complete the construction of the main window and display work. Remember this function.
⑤ CWinApp:: Run member function: WinMain calls immediately after the call to InitInstance Run function, which is hidden in the base class is responsible for transfer applications The message to the matched window. (I understand it as if the window procedure function SDK do not know right)
⑥ CMyFrame class: This object represents the application's main window. It's base class constructor calls CFrameWnd the Create function to create a specific window structure.
⑦ CMyFrame:: OnLButtonDown function: Demo message handling mechanism, when the mouse button is pressed to sit in this event is mapped to CMyFrame the OnLButtonDown function, if you choose F5 to compile and run then you can see the TRACE macro in the debug window displays information similar to the following
Entering CMyFrame:: OnLButtonDown - 1,309,119
Entering CMyFrame:: OnLButtonDown - 1,408,221
⑧ CMyFrame:: OnPaint function: application to re-draw each time window needs to call this function, will display (this);
dc.TextOut (0,0,
{
TRACE (dc (this);
dc.TextOut (0,0, Minimize the window and then maximized Finally, remove the CMyApp object.
by the above example we see most of the features included in the program CWinApp and CFrameWnd base class, we just write a few functions, we can complete very complex functions. so the application framework is not only is only a library, it also defines the structure of the application, in addition to the base class also included the WinMain function, and to support the message processing, diagnosis, DLL, and so included in the application framework.
third : Message map and view class notes
after writing the two friends gave me a lot of letters, and I share some of the problems in the learning process. but I am also a beginner, some of the issues themselves are not sure Therefore, it is not pop star, but I will try to do my best to study with everyone, thank you for the encouragement and confidence.
at the beginning of the first to add content on a Tiange netizen's question and answer (if Tiange brother can see it). In order to simplify the code, we see a more simple, only one file, create a blank window, and do nothing. Note Well: I'm writing a program or to be divided. h and. cpp file more clear two.
front steps and 『』 Inside VC + + Study Notes (2) the same, the following is the code:
/ / MyApp.cpp
# include > {
public:
virtual BOOL InitInstance ();// overloaded virtual function InitInstance
};
class CMyFrame: public CFrameWnd / / inherited from CFrameWnd class
{}; < br> CMyApp myApp; / / finally activate the application's constructor
BOOL CMyApp:: InitInstance ()
{
m_pMainWnd = new CMyFrame;
/ / new class constructor activated CMyFrame CFrameWnd (), also used constructor tune CREATE ()
/ / m_pMainWnd defined in the MFC CWnd * m_pMainWnd; / / main window (usually same AfxGetApp () -> m_pMainWnd)
/ / m_pMainWnd save the window position
((CMyFrame *) m_pMainWnd) -> Create (NULL, screen
return TRUE;
}
see if the proposals do not understand or see MSDN master Hou layman. on this issue to this point, the last of the Tiange friends to pay tribute to the attitude of learning. < br> The following entry is the body, let us continue to look at MFC's message map.
MFC through some of the macro to a specific message alluding to the corresponding derived class member functions, the benefits of this system is to allow certain non- window class (such as document type) to control the command message. and does not require any expansion of C + +.
MFC control function requires the message to provide the function prototype, the function body and the message map entry. really a lot of trouble, better use Class Wizard can be easily added to the above mentioned things in our class. For example, as long as the Class Wizard to add WM_LBUTTONDOWN message, then the code will be added to the right place. how about it a lot easier.
In addition to the application framework that contains the application class, in general, but also contains the document and view classes. This document - view architecture is the core application framework. I've been MIS development, certainly not with the VC is used VB, so to see it I do not consciously think of the relationship between back-end database and interface, this example may not be appropriate, but it can very well help us to understand documents and views. documents like the background database, view the contents is displayed on the screen, the same database can be displayed with a different interface, but because of all the interface data content is derived from the database, so when the database changes, the interface displays all the relevant changes will follow. more do not think it appropriate, but can not think of other examples of the. our application is actually the view of the document through a series of operations, not just output. Here we look at the view class.
view is a class derived from CView class object on the screen is a display window. the object of the act entirely by class member functions and data members of the decision, and which includes a specific function in the derived class, including the standard base class function, understanding the structure of MFC class library and a variety of how important the standard member function ah, do not bother to read the source code for MFC recruited to see how many count the number, but certainly not the white look, this is the experience of Raytheon.
I remember the example of an up
1, open the VC + +6 from the menu, select NEW, the project named > MFC application framework at this time would help us to complete the application, you can run it, it will be displayed on the screen style of a typical WINDOWS blank window.
project we look at where the program files directory:
myapp01.dsp / / project file
myapp01.dsw / / work space file
myapp01.rc / / resource description file
myapp01View.cpp / / class member function contains CMyAppView view class implementation file Key
myapp01View.h / / class member function contains CMyAppView view class header file key
myapp01.opt / / binary file that tells what Developer Studio project file is the open, but also how to arrange the
readme.txt / / used to explain all the files generated, not listed myapp01Doc.cpp, myapp01Doc.h, StdAfx.cpp, StdAfx.h .... can be found here accordingly.
resource . h / / include # define constants defined in header files
closer look myapp01View.cpp and myapp01View.h files, programs, classes in the core CMyAppView defined in two documents, that we are to learn the view class. To see CMyAppView class picture, should be Source Browser view, select CMyApp01View, press Alt + F12, select the Base Class and Members option. we will see CMyAppView class hierarchies (where he comes from too), and all member functions, including inherited from the base class (not shown in the Class View in the parent class member functions). We found that in fact you have nothing to do to have a class with powerful features.
Here we look at CMyAppView OnDraw member of the class function, which is the role of virtual function to be redrawn whenever the window when the application framework will call it. Its prototype is like this (can be found in the myapp01View.h)
virtual void OnDraw (CDC * pDC); / / overridden to draw this view
parameter is a pointer to CDC class, WINDOWS and windows through the device associated with the environment (CDC class object is the device context) and display hardware for communication. With this pointer we can call the CDC class member function to do the work of a variety of drawing, as indicated in a used textout () and some Ellipse (), Polygon (), BitBlt () so long in the MSDN has an article, it is directly from CObject derived, well we started in the OnDraw () to add some graphics work.
3, in the Class View selection CMyApp01View class OnDraw () member function in C + + double-click the compiler sees the following
void CMyApp01View: : OnDraw (CDC * pDC)
{
CMyApp01Doc * pDoc = GetDocument ();
ASSERT_VALID (pDoc);
/ / TODO: add draw code for native data here
}
In the / / TODO: add draw code for native data here to increase the position of a line of code
void CMyApp01View:: OnDraw (CDC * pDC)
{
CMyApp01Doc * pDoc = GetDocument ();
ASSERT_VALID (pDoc);
pDC-> TextOut (10,10,
pDC-> Ellipse (CRect (20,40,120,140)); / / draw a circle, CRect is the MFC library provides a class that rectangle WINDOWS
pDC-> Rectangle (CRect (220,240,120,140)); / / draw a rectangle
/ / TODO: add draw code for native data here
}
compile and run, the success of it. first it right. Another future article will also be posted on the writing area.
s Four: Resources and compiled
resource files (that is, to apply the program name and extension. rc files) largely determines the application's user interface. In VC + +, resource files include the following:
Accelerator / / simulate the choice of menus and toolbars content
Dialog / / dialog box layout and content
Icon / / icon, there are two one is 16X16 one is 32X32.
Menu / / application belongs to the main menu and pop-up menu
String table / / string does not belong to C + + source code for part of the
Toolbar / / toolbar.
Version / / program description, version, supported languages information. < br> The above information are. rc file contains at the same time. rc file also contains the following statement:
# include is suitable for all applications to a number of common MFC library resources include it.
use on the resource editor is not to say, because its operation is very simple to note that although an ASCII code resource.h file can be edited with a text editor, one if you use a text editor to edit it, next time when using the resource editor edits may be lost, so we should try in the resource editor in the editing application of resources , the contents of the new resources added back automatically the corresponding position in our program, for example, instead of us worry about resource.h. This is why is it called Visual (visible) one of the reasons.
compile in VC + + two modes, one is the other one is the Release Build Debug Build. the difference between them is that, Release Build does not debug the source code, without regard to the diagnosis of MFC macros, using MFC Release database, compiled ten pairs of applications to optimize the speed, while the Debug Build, by contrast, it allows the source code for debugging, you can define and use the diagnosis of MFC macros, using MFC Debug library, not optimized for speed. so we should be in Debug mode, application development, In Release mode, and then publish the application. In our project there will be a folder Debug folder and a Release folder and intermediate output files were stored files.
diagnostic macro is to compile a program enabling the state tests tools, such as used on the two TRACE macro in the Debug window, you can get diagnostic information you need without having to set the dialog box like the method, at the time of Release of this information is automatically filtered out.
actually debugging an application is a very challenging job, and I believe we have a similar experience, recruited from the Internet or books for a code or source code, that point when we knocked them into the compilation after Deleloper Studio when all of a sudden there numerous errors and warnings, (some of the books of the source code is wrong) that needs patience and experience with VC + + debugging tools available, such as diagnostic macros, set breakpoints, single stepping, etc. We will save a lot of effort. As for the topic should really compile a lot to say, but because of Raytheon's own lack of experience can only speak on the book for a summary, large ...
No comments:
Post a Comment