線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2755
推到 Plurk!
推到 Facebook!

VC++ Windows Programming

 
AB
高階會員


發表:166
回覆:262
積分:125
註冊:2003-08-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-02 00:02:06 IP:61.64.xxx.xxx 未訂閱
http://www.rit.edu/~pnveme/    http://www.rit.edu/~pnveme/Visual_C++/VC_Windows1.html We will attempt to do basic programming for Windows in C++ using the additional resources in VC++.   This is not easy, and the learning curve is steep.  The knowledge  is not easily acquired in a crash course setting.  If you can do it to a reasonable degree,  companies should be chasing you.  Our objective is to explore the features of program development for Windows using VC++.      If you remember we used VB for doing this.  If your target user environment is Win32 ( > Win 95 or  NT )  then it worth exploring if you can use VB for your development.  If you are targetting Windows, Mac, some flavors of Unix(MIPS) then you have no choice but C/C++.  Microsoft has made the task easier by providing a complete development environment through the VS package including the Visual C++ compiler.      In Windows application you need to package your application with  GUI elements and windows.  If are also serious about making money of your application, you need to be aware of the guidelines provided by Microsoft so that your application will produce similar effects when using standard elements or windows terminology.  This is to provide consistent UI response for all windows application to avoid need for user to relearn about the same activities.  Lets remember Java.  You could develop a program in Java that ran in its own windows because there was a Java API  ( Application Programming Interface).  This is a collection of libraries (that included many classes) that made creating windows and user interface elements easy for the developer of the application.  Imagine otherwise if you had to write hard code for creating windows and UI elements each time you used it in your program, leave alone the code necessary for the processing involved in the application.  If Sun did not provide this API there would be very few people programming in Java, since many have already spent an enormous amount of time developing for the window platform.         To do the same in Windows there needs to be a Windows API and there is.  These API libraries make it easier for the developer to use the graphical resources because they are function calls.  In fact you should be able to get the source code for these libraries so you can exploit them for your development if necessary.  When we programmed in VB, we did not use the Windows API directly.  VB is another layer built on top of the Windows API that makes it still easier  for the developer.  When we placed a command button on the form, VB would call several API functions to get the job done (unknown to us).   There are situations in VB when you must use the functions from the API directly ( probably in an advanced course).    Even a simple appliaction created with standard API function calls is a nightmare to develop.         In Visual C++, Microsoft provides a Class Library called the Microsoft Foundation Class ( MFC).  These are set of OOP tools for Win32 applications.  This is once again a layer between the developer and the API , and provides a powerful  toolkit for OOP for windows.     From VS Documentation:     The Win32 application programming interface (API) defines the 32-bit members of the Windows operating system family from the programmer's point of view. Some members of the Windows family use the entire Win32 API, while others use subsets. For details, see Windows 95 System Limitations.  The Microsoft Foundation Class Library (MFC) encapsulates, or "wraps," much of (but not all of) the Win32 API. MFC versions 2.x and earlier encapsulated the 16-bit Windows API. In general, MFC supplies classes representing key Windows objects, such as windows, dialog boxes, brushes, pens, and fonts. The member functions of these classes wrap most of the important Win32 API functions associated with the encapsulated object. That is, the MFC class member function calls the Win32 API function (it may do other things as well).     With Visual C++, you can program for Windows using either C or C++ and the Win32 API, or using C++ and MFC. Visual C++ includes documentation for writing in C or C++ and for MFC. It also adds the Win32 SDK documentation, which is your source for most general information about programming for Windows. Aside from the Win32 SDK documents, the Visual C++ documentation focuses on what Visual C++ adds to the package: C++, MFC, and ATL. Most of the documentation you are reading is about C++ and MFC, but it frequently references the Win32 SDK for Windows concepts and techniques. If you are new to programming for Windows, reading a good third-party book is recommended.    MFC Microsoft is not the only game in town for Class libraries to use windows.  Several other compiler vendors also provide a windows toolkit.   The MFC  has some key advantages      Complete support for all windows functions      Same names as in Windows API for same task      Message map macros.   All messages are mapped to member functions      Extensive exception handling      Small code and almost as fast  as a C implemetation      Support for COM (Component Object model) Objects     We will accept the above even if we do not understand some of the advantages ( beyond the scope).   The guidelines in the development of MFC were [Murray and Pappas]:         Utilize the C++ language without inflicting pain on the programmer      Keep the transition from API calls to the use of Class libraries simple      Allow simultaneous use of API calls and MFC  library      Allow for library migration to evolving platforms     MFC library basically translates  the Windows API for OOP design.  It encapsulates code and data;  allows inheritence;  eliminates function and variable name collisions.  The resulting classes appear as natural extensions to the language and is usually accompanied by reduced class size.     From VS Documentation:     The Microsoft Foundation Class Library (MFC) is an "application framework" for programming in Microsoft Windows. Written in C++, MFC provides much of the code necessary for managing windows, menus, and dialog boxes; performing basic input/output; storing collections of data objects; and so on. All you need to do is add your application-specific code into this framework. And, given the nature of C++ class programming, it's easy to extend or override the basic functionality the MFC framework supplies.  The MFC framework is a powerful approach that lets you build upon the work of expert programmers for Windows. MFC shortens development time; makes code more portable; provides tremendous support without reducing programming freedom and flexibility; and gives easy access to "hard to program" user-interface elements and technologies, like ActiveX, OLE, and Internet programming. Furthermore, MFC simplifies database programming through Data Access Objects (DAO) and Open Database Connectivity (ODBC), and network programming through Windows Sockets. MFC makes it easy to program features like property sheets ("tab dialogs"), print preview, and floating, customizable toolbars.       The following is the compact hiearchy of the MFC classes.  The grand Daddy is the CObject       Expansion of some of the Classes above (consult documentation for the complete hierarchy chart)            The documentation should provide the deatils of each class and its implementation.  You can extend these classes and  come up with your own.       First Windows Program We will develop our first windows program using MFC.  We will throw up a window on the screen and write a sentence in it.  We will also create and use a header file.      Start VS and Under File Menu    =>    New    =>    Project (name, location)    =>     Win32 Application         Under Project Menu    =>    Add to Project     =>    New  = >  C/C++ Header File (name : BasicWin.h)         Type in the following Code:(BasicWin.h)      
 // The header file contains usually 
// class definition and function protitypes. 
// In C/C   the compiler must know of the function 
// prototype even if the actual procedure is defined later. 
// hacked from [Murray and Pappas] 
// 
  
class CMainWnd : public CFrameWnd 
{ 
// CmainWnd is derived from CFrameWnd  - a Framed window 
// Notice class definition starts with a Capital C 
//      public: 
 CMainWnd();  // constructor 
 afx_msg void OnPaint(); // OnPaint() procedure 
                         // identified also as a windows message 
                         // returns void 
 DECLARE_MESSAGE_MAP();  // macro identifying that 
                         // a message map will be 
                         // identified in the application 
}; 
      //  class COurApp is derived from CWinApp class 
class COurApp : public CWinApp 
{     public:      BOOL InitInstance(); 
// for windows application this 
// serves as main () 
}; 
         Under Project Menu    =>    Add to Project     =>    New  = >  C    Source  File (name : FirstWin.h) 
        and type the follwing code: 
/* 
// Our First Window 
// Throw up a window and write a line of Text 
// 
// hacked from Murray and Pappas 
*/ 
#include    // include MFC library 
#include "BasicWin.h" // include the header file we created earlier     //  Note the system header files are in angle brackets 
//  user defined are in "  quotes  "     COurApp App; 
// instantiate an object of the class CourApp called App 
      CMainWnd::CMainWnd()  // constructor for the CMainWnd class 
{ 
 Create(NULL, "The Window", WS_OVERLAPPEDWINDOW, \ 
  rectDefault, NULL, NULL);     // this will create a framed rectangle overlapped window with the 
// title " The Window" of default dimensions 
}     void CMainWnd::OnPaint() 
// windows paint function ( remember Java) 
// causes Graphics to be drawn on the window 
{ 
 CPaintDC dc(this);  // DC stands for Device Context 
 dc.TextOut(50,50,"This is our First Windows Programming Exercise", 46); 
// outputs quoted text at 50,50 character location 
// of length 46 charcaters 
}     // this is the message map  in MFC 
// every function can have a message map 
// messages are the way objects communicate 
// the OnPaint() function is associated 
// with the windows message PAINT 
// ON_WM_PAINT() - WM - windows message     BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd) 
 ON_WM_PAINT() 
END_MESSAGE_MAP()     BOOL COurApp:: InitInstance()// the InitInstance method is being 
                             // overidden 
{ 
 m_pMainWnd = new CMainWnd; // an object of class CMainWnd 
                            // is created 
// it is important to pay attention to variable naming 
// convention since some of the member variables are 
// automatically created and NAMED - from the 
// parent classes like for m_pmainWnd, m_nCmdShow 
// the convention: 
// m   stands for member 
// p   stands for a pointer 
// 
// n    stands for integer 
  
 m_pMainWnd->ShowWindow(m_nCmdShow);     // The window must be drawn on the screen to display it 
// corresponds to show method in Java and VB     //    if m_pMainWnd was a reference to an actual object then 
//    we can execute its method with a object.method() construction 
// 
//    since it is a pointer the methods can be activated 
//     only using the arrow operator      m_pMainWnd->UpdateWindow(); // repaint() in Java     // this takes care of repainting the window when necessary      return TRUE; 
}     
Choose Use MFC Libraries if requested. Build and Execute A screen shot of the Window
系統時間:2024-05-01 12:28:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!