Programming visual c++ NET core reference 6th ed 2003

1.4K 157 0
Programming visual c++ NET core reference 6th ed 2003

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2003 by George Shepherd All rights reserved No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher Library of Congress Cataloging-in-Publication Data Shepherd, George Programming with Microsoft Visual C++ NET, Sixth Edition (Core Reference) p cm Includes index ISBN 0-7356-1549-7 2002 Printed and bound in the United States of America 1 2 3 4 5 6 7 8 9 QWT 7 6 5 4 3 2 Distributed in Canada by H.B Fenn and Company Ltd A CIP catalogue record for this book is available from the British Library Microsoft Press books are available through booksellers and distributors worldwide For further informa-tion about international editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly at fax (425) 936-7329 Visit our Web site at www.microsoft.com/mspress Send comments to mspinput@microsoft.com Active Directory, ActiveX, FrontPage, Links, Microsoft, Microsoft Press, MSDN, Outlook, PivotChart, PivotTable, PowerPoint, SharePoint, Visio, Visual Basic, Windows, and Windows NT are either regis-tered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries Other product and company names mentioned herein may be the trademarks of their respective owners The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious No association with any real company, organization, product, domain name, e-mail address, logo, person, place, or event is intended or should be inferred Acquisitions Editors: Juliana Aldous and Danielle Bird Project Editor: Denise Bankaitis Technical Editor: Julie Xiao Dedicated to Sandy Daston and Ted Shepherd Acknowledgments This part of book writing is always the best—everybody involved is nearly done with the manuscript and all that's left to do is to thank everybody Because the author's name appears on the cover, it's sometimes easy to forget all the other folks involved in a project as large as this Many other folks gave their time and energy to this project, and I wish to thank you Thank you Sandy Daston and Ted Shepherd—my family, for your support while I wrote this book Thank you, Denise Bankaitis As the project editor, you kept me going by reminding me of the importance of this project (a key C++ reference for NET) and by coordinating the efforts of the rest of the team, which includes Julie Xiao, Ina Chang, Danielle Bird, Juliana Aldous, Joel Panchot, Carl Diltz, and Gina Cassill Thank you, Julie Xiao, for keeping the manuscript accurate Thank you, Ina Chang, for making my sentences readable Thank you, Danielle Bird and Juliana Aldous As acquisition editors, you got this project rolling and kept it on track Thank you, Joel Panchot, for making sure the art in this book looks good Thank you, Carl Diltz and Gina Cassill, for composing the manuscript and making it look great I would also like to thank the folks at DevelopMentor, for providing a wonderful environment and community for thinking and learning about modern computing You guys are wonderful Introduction The release of the Microsoft Visual Studio NET (and Visual C++ NET in particular) has underscored Microsoft’s increasing focus on Internet technologies, which are at the heart of the Microsoft NET architecture In addition to supporting the NET initiative, Visual C++ NET keeps all the productivity-boosting features you’re familiar with, such as Edit And Continue, IntelliSense, AutoComplete, and code tips Visual C++ NET also includes many new features such as managed code extensions for NET programming, support for attributed code, and a more consistent development environment These features take Visual C++ NET to a new level This book will get you up to speed on the latest technologies introduced into Visual C++ NET, MFC, and ATL The technology churn we face these days is pretty impressive We went from no computers on our office desktops to nearly everyone having a computer running MS-DOS in the 1980s to nearly everyone running Microsoft Windows by the mid-1990s The technology wheel is about to turn again In the late 1990s, everyone was developing Web sites by hand using tools such as raw Hypertext Markup Language (HTML), Common Gateway Interface (CGI), Internet Server Application Programming Interface (ISAPI) DLLs, Java, and Active Server Pages (ASP) In July 2000, Microsoft announced to the world that it would change all that by betting the company on a new technology direction named NET The current thrust of Microsoft is indeed NET For a number of years, it’s been possible to build a Web site by setting up a server somewhere, getting an IP address, and putting up some content Anyone with the URL of your site can surf there and check it out Commercial enterprises have been taking advantage of the Web by posting information that’s useful to customers The Web has also become an invaluable research tool and efficient news broadcast medium The computing world of the near future will involve the Web heavily However, rather than just having human eyeballs look at Web sites, computers themselves will look at Web sites That is, Web sites will be programmable through Web services The NET vision also pushes the responsibility of providing a rich user interface out to the server With so much emphasis on Web services and server-based user interfaces, it might seem that standalone applications and client-side user interface scenarios —normally the realm of tools such as the Microsoft Foundation Class Library (MFC)—will be left in the dust But the need for rich client-side user interfaces is unlikely to go away Many thought that the advent of the PC and distribution technologies would spell the end of centralized processing on mainframes and minicomputers It turns out that PCs and distribution technologies only added to the available computing arsenal The NET vision of Web services and rich user interfaces provided by the server only adds to the options available to software developers Rich client-side user interfaces will continue to be viable for many types of applications, running alongside other applications that use other kinds of user interfaces (such as server-generated user interfaces) MFC is a mature and well-understood technology that’s accompanied by a host of third-party extensions For at least a little while longer, MFC represents the most effective way to write full-featured standalone applications A good portion of this book will focus on MFC-style development, but we’ll also cover Windows Forms—the NET way to write client-side user interfaces Of course, the next question is: Where does this leave COM? COM has solved many problems related to distributed processing, but it has some serious shortcomings—mostly centered around component versioning and type information Microsoft’s NET vision is based on the common language runtime The runtime takes the place of COM as the interoperability standard within NET We’ll cover NET and the common language runtime in depth in Part VI of this book COM and the common language runtime represent different approaches to component architecture, but Microsoft has taken great care to ensure a seamless coexistence The interoperability path between COM and the runtime is smooth in most cases Within the NET world, you probably won’t find yourself using COM as a component architecture However, you might find yourself using Active Template Library (ATL) Server, which is a high-performance means of writing Web sites I’ve updated the coverage of ATL and MFC in this edition of the book because you’ll still find it very useful More important, I’ll show you how to leverage your heritage code (sounds better than “legacy code,” doesn’t it?) as you move into the NET world Managed C++ vs C# The NET platform has introduced a new C++-like language named C# C# is a curly-brace-oriented language without all the headaches of C++ Much of C#’s appeal is due to the fact that it’s missing some of the more problematic elements of C++ (such as raw pointer management) while maintaining the useful features (such as virtual functions) The C# compiler eventually emits managed code— the kind that runs under the common language runtime However, the entire world isn’t going to switch over to C# overnight There’s just too much C++ code out there to convert Also, it will take a bit of time for developers to become fully comfortable with C# In the meantime, NET has introduced extensions to C++ for producing managed code (code that runs under the common language runtime) Managed Extensions for C++ will help ease the burden of developing software for the NET platform because they allow you to quickly update existing C++ code to work with NET Getting the managed code features in C++ means sprinkling your code with various keywords In the end, C# and managed C++ boil down to the same executable code once the compilers are done with it In the NET world, you’ll probably find yourself writing new components using C# while using managed C++ to add NET features to your existing code base NET vs the Java Platform In recent years, we’ve seen a great deal of interest in the Java programming language and platform Java became a great boon for Internet developers by providing a useful means of distributing client user interfaces (through Java applets) and by providing enterprise solutions through Java Enterprise Edition Now, NET has become the best Internet development platform available today Unlike the Java platform, which requires that you write all your code using the Java syntax, NET often lets you use multiple syntaxes to arrive at the same machine instruction set You can use C++ (the main focus of this book) and its managed extensions, Visual Basic NET, C#, and even a host of third-party NET languages to write your programs Once you develop your source code, it is compiled to intermediate language and then eventually machine code before it runs Because NET code is managed by a runtime, you get benefits such as garbage collection and better code security ON_WM_RENDERALLFORMATS() afx_msg void OnRenderAllFormats(); ON_WM_RENDERFORMAT() afx_msg void OnRenderFormat(UINT); ON_WM_SETCURSOR() afx_msg BOOL OnSetCursor(CWnd*, UINT, UINT); ON_WM_SETFOCUS() afx_msg void OnSetFocus(CWnd*); ON_WM_SETTINGCHANGE() afx_msg void OnSettingChange(UINT, LPCTSTR); ON_WM_SHOWWINDOW() afx_msg void OnShowWindow(BOOL, UINT); ON_WM_SIZE() afx_msg void OnSize(UINT, int, int); ON_WM_SIZECLIPBOARD() afx_msg void OnSizeClipboard(CWnd*, HGLOBAL); ON_WM_SIZING() afx_msg void OnSizing(UINT, LPRECT); ON_WM_SPOOLERSTATUS() afx_msg void OnSpoolerStatus(UINT, UINT); ON_WM_STYLECHANGED() afx_msg void OnStyleChanged(int, LPSTYLESTRUCT); ON_WM_STYLECHANGING() afx_msg void OnStyleChanging(int, LPSTYLESTRUCT); ON_WM_SYSCHAR() afx_msg void OnSysChar(UINT, UINT, UINT); ON_WM_SYSCOLORCHANGE() afx_msg void OnSysColorChange(); ON_WM_SYSCOMMAND() afx_msg void OnSysCommand(UINT, LPARAM); ON_WM_SYSDEADCHAR() afx_msg void OnSysDeadChar(UINT, UINT, UINT); ON_WM_SYSKEYDOWN() afx_msg void OnSysKeyDown(UINT, UINT, UINT); ON_WM_SYSKEYUP() afx_msg void OnSysKeyUp(UINT, UINT, UINT); ON_WM_TCARD() afx_msg void OnTCard(UINT, DWORD); ON_WM_TIMECHANGE() afx_msg void OnTimeChange(); ON_WM_TIMER() afx_msg void OnTimer(UINT); ON_WM_VKEYTOITEM() afx_msg int OnVKeyToItem(UINT, CListBox*, UINT); ON_WM_VKEYTOITEM_REFLECT() afx_msg int VKeyToItem(UINT, UINT); ON_WM_VSCROLL() afx_msg void OnVScroll(UINT, UINT, CScrollBar*); ON_WM_VSCROLL_REFLECT() afx_msg void VScroll(UINT, UINT); ON_WM_VSCROLLCLIPBOARD() afx_msg void OnVScrollClipboard(CWnd*, UINT, UINT); ON_WM_WINDOWPOSCHANGED() afx_msg void OnWindowPosChanged (WINDOWPOS*); ON_WM_WINDOWPOSCHANGING() afx_msg void OnWindowPosChanging (WINDOWPOS*); ON_WM_WININICHANGE() afx_msg void OnWinIniChange(LPCTSTR); Table 2-4 User-Defined Message Codes Map Entry Function Prototype ON_MESSAGE(,) afx_msg LRESULT memberFxn(WPARAM, LPARAM); ON_REGISTERED_MESSAGE (,) afx_msg LRESULT memberFxn(WPARAM, LPARAM); ON_REGISTERED_THREAD MESSAGE (, ) afx_msg void memberFxn(WPARAM, LPARAM); ON_THREAD_MESSAGE (, ) afx_msg void memberFxn(WPARAM, LPARAM); Appendix B MFC Library Runtime Class Identification and Dynamic Object Creation Long before runtime type information (RTTI) was added to the C++ language specification, the MFC library designers realized that they needed runtime access to an object’s class name and to the position of the class in the hierarchy Also, the document-view architecture (and, later, COM class factories) demanded that objects be constructed from a class specified at run time So the MFC team created an integrated macro-based class identification and dynamic creation system that depends on the universal CObject base class And in spite of the fact that the Visual C++ NET compiler supports the ANSI RTTI syntax, the MFC library continues to use the original system, which actually has more features This appendix explains how the MFC library implements the class identification and dynamic creation features You’ll see how the DECLARE-_DYNAMIC, DECLARE_DYNCREATE, and associated macros work, and you’ll learn about the RUNTIME_CLASS macro and the CRuntimeClass structure Getting an Object’s Class Name at Run Time If you want only an object’s class name, you’ll have an easy time, assuming that all your classes are derived from a common base class, CObject (Note that this example does not use the real MFC CObject class.) Here’s how you get the class name: class CObject { public: virtual char* GetClassName() const { return NULL; }; class CMyClass : public CObject { public: static char s_lpszClassName[]; virtual char* GetClassName() const { return s_lpsz }; char CMyClass::s_szClassName[] = "CMyClass"; Each derived class overrides the virtual GetClassName function, which returns a static string You get an object’s actual class name even if you use a CObject pointer to call GetClassName If you need the class name feature in many classes, you can save yourself some work by writing macros A DECLARE_CLASSNAME macro might insert the static data member and the GetClassName function in the class declaration, and an IMPLEMENT_CLASSNAME macro might define the class name string in the implementation file The MFC CRuntimeClass Structure and the RUNTIME_CLASS Macro In a real MFC program, an instance of the CRuntimeClass structure replaces the static s_lpszClassName data member shown above This structure has data members for the class name and the object size; it also contains a pointer to a special static function, CreateObject, that’s supposed to be implemented in the target class Here’s a simplified version of CRuntimeClass: struct CRuntimeClass { // Attributes LPCSTR m_lpszClassName; int m_nObjectSize; UINT m_wSchema; // Schema number of the loaded cla CObject* (PASCAL* m_pfnCreateObject)(); // NULL => #ifdef _AFXDLL CRuntimeClass* (PASCAL* m_pfnGetBaseClass)(); #else CRuntimeClass* m_pBaseClass; #endif // Operations CObject* CreateObject(); BOOL IsDerivedFrom(const CRuntimeClass* pBaseClass // Dynamic name lookup and creation static CRuntimeClass* PASCAL FromName(LPCSTR lpszC static CRuntimeClass* PASCAL FromName(LPCWSTR lpsz static CObject* PASCAL CreateObject(LPCSTR lpszCla static CObject* PASCAL CreateObject(LPCWSTR lpszCl // Implementation void Store(CArchive& ar) const; static CRuntimeClass* PASCAL Load(CArchive& ar, UI // CRuntimeClass objects linked together in simple CRuntimeClass* m_pNextClass; // Linked list of r const AFX_CLASSINIT* m_pClassInit; }; NOTEThe real MFC CRuntimeClass structure has additional data members and functions that navigate through the class’s hierarchy This navigation feature is not supported by the official C++ RTTI implementation This structure supports not only class name retrieval but also dynamic creation Each class you derive from CObject has a static CRuntimeClass data member, provided you use the MFC DECLARE_DYNAMIC, DECLARE_DYNCREATE, or DECLARE_SERIAL macro in the declaration and the corresponding IMPLEMENT macro in the implementation file The name of the static data member is, by convention, class If your class were named CMyClass, the CRuntimeClass data member would be named classCMyClass If you want a pointer to a class’s static CRuntimeClass object, you use the MFC RUNTIME_CLASS macro, defined as follows: #define _RUNTIME_CLASS(class_name)\ ((CRuntimeClass*)(&class_name::class##class_name)) #ifdef _AFXDLL #define RUNTIME_CLASS(class_name) (class_name::GetThis #else #define RUNTIME_CLASS(class_name) _RUNTIME_CLASS(class #endif Here’s how you use the macro to get the name string from a class name: ASSERT(RUNTIME_CLASS(CMyClass)->m_lpszClassName == "CM If you want the class name string from an object, you call the virtual CObject::GetRuntimeClass function The function simply returns a pointer to the class’s static CRuntimeClass object, just as earlier the GetClassName function returned the name string Here’s the function for CMyClass: virtual CRuntimeClass* GetRuntimeClass() const { return &classCMyClass; } And here’s how you call it: ASSERT(pMyObject->GetRuntimeClass()->m_lpszClassName = Dynamic Creation You’ve learned that the DECLARE and IMPLEMENT macros add a static CRuntimeClass object to a class If you use the DECLARE_DYNCREATE or DECLARE-_SERIAL macro (and the corresponding IMPLEMENT macro), you get an additional static member function CreateObject (which is distinct from CRuntimeClass::CreateObject) in your class Here’s an example: CObject* CMyClass::CreateObject() { return new CMyClass; } Obviously, CMyClass needs a default constructor This constructor is declared protected in wizard-generated classes that support dynamic creation Now look at the (slightly abbreviated) code for the CRuntime-Class::CreateObject function: CObject* CRuntimeClass::CreateObject() { return (*m_pfnCreateObject)(); } This function makes an indirect call to the CreateObject function in the target class Here’s how you dynamically construct an object of class CMyClass: CRuntimeClass* pRTC = RUNTIME_CLASS(CMyObject); CMyClass* pMyObject = (CMyClass*)pRTC->CreateObject(); Now you know how document templates work A document template object has three CRuntimeClass* data members initialized at construction to point to the static CRuntimeClass data members for the document, frame, and view classes When CWinApp::OnFileNew is called, the framework calls the CreateObject functions for the three stored pointers A Sample Program Here’s the code for a command-line program that dynamically constructs objects of two classes This isn’t real MFC code—the CObject class is a simplified version of the MFC library CObject class You can find this code in the dyncreat.cpp file in the \vcppnet\appendb folder on the companion CD // dyncreat.cpp : Defines the entry point for the cons // #include "stdafx.h" #include #define RUNTIME_CLASS(class_name) (&class_name::class# class CObject; struct CRuntimeClass { char m_lpszClassName[21]; int m_nObjectSize; CObject* (*m_pfnCreateObject)(); CObject* CreateObject(); }; // Not a true abstract class because there are no pure // virtual functions, but user can't create CObject o // because of the protected constructor class CObject { public: // not pure because derived classes don't necessar // implement it virtual CRuntimeClass* GetRuntimeClass() const { r // We never construct objects of class CObject, bu // use this to get class hierarchy information static CRuntimeClass classCObject; // DYNAM virtual ~CObject() {}; // gotta have it protected: CObject() { printf("CObject constructor\n"); } }; CRuntimeClass CObject::classCObject = { "CObject", sizeof(CObject), NULL }; CObject* CRuntimeClass::CreateObject() { return (*m_pfnCreateObject)(); // indirect functio } class CAlpha : public CObject { public: virtual CRuntimeClass* GetRuntimeClass() const { return &classCAlpha; } static CRuntimeClass classCAlpha; // DYNAMI static CObject* CreateObject(); // DYNCRE protected: CAlpha() { printf("CAlpha constructor\n"); } }; CRuntimeClass CAlpha::classCAlpha = { "CAlpha", sizeof(CAlpha), CAlpha::CreateObject }; CObject* CAlpha::CreateObject() // static function { return new CAlpha; } class CBeta : public CObject { public: virtual CRuntimeClass* GetRuntimeClass() const { return &classCBeta; } static CRuntimeClass classCBeta; // DYNAMI static CObject* CreateObject(); // DYNCRE protected: CBeta() { printf("CBeta constructor\n"); } }; CRuntimeClass CBeta::classCBeta = { "CBeta", sizeof(CBeta), CBeta::CreateObject }; CObject* CBeta::CreateObject() // static function { return new CBeta; } int main() { printf("Entering dyncreate main\n"); CRuntimeClass* pRTCAlpha = RUNTIME_CLASS(CAlpha); CObject* pObj1 = pRTCAlpha->CreateObject(); printf("class of pObj1 = %s\n", pObj1->GetRuntimeClass()->m_lpszClassName); CRuntimeClass* pRTCBeta = RUNTIME_CLASS(CBeta); CObject* pObj2 = pRTCBeta->CreateObject(); printf("class of pObj2 = %s\n", pObj2->GetRuntimeClass()->m_lpszClassName); delete pObj1; delete pObj2; return 0; } About the Author When George Shepherd isn't writing NET components for Syncfusion (http://www.syncfusion.com), he teaches short courses with DevelopMentor (http://www.develop.com) George is a contributing editor for MSDN magazine, and the coauthor of several other books on working with Microsoft technologies George now prefers to play his Hamer Artist between compiles (although NET's new JIT compiling doesn't leave as much time for that) ... NET is a suite of developer tools that includes Visual C++ NET The Visual Studio NET integrated development environment (IDE) is shared by several tools, including Visual C++ NET, Microsoft Visual C#, and Microsoft Visual Basic NET The IDE has come a long way from the original Visual. .. (RTTI) are fully supported in Visual C++ NET The C++ Standard Template Library (STL) is also included, although it is not integrated into the MFC library The Source Code Editor Visual C++ NET includes a sophisticated source code editor that supports many... project, you tell Visual C++ NET to open the SLN file, and then you can edit and build the project Visual C++ NET creates some intermediate files too Table 1-1 lists the files that Visual C++ NET generates in the solution

Ngày đăng: 25/03/2019, 16:05

Mục lục

  • Programming with Microsoft Visual C++ .NET, Sixth Edition (Core Reference)

    • LOC

    • Dedication

    • Acknowlegments

    • Introduction

      • .NET, MFC, and ATL

      • Managed C++ vs. C#

      • .NET vs. the Java Platform

      • Who This Book is For

      • What's Not Covered

      • How to Use This Book

      • How This Book is Organized

      • Win32 vs. Win16

      • System Requirements

      • Sample Files

      • Visual Studio .NET Professional Trial Version

      • Microsoft Press Support Information

      • Part I - Windows, Visual C++ .NET, and Application Framework Fundamentals

        • Chapter 1 - Windows and Visual C++ .NET

          • The Windows Programming Model

          • Visual C++ .NET Components

          • Chapter 2 - The Microsoft Foundation Class Library Application Framework

            • Why Use This Application Framework?

            • What's an Application Framework?

            • MFC Library Message Mapping

Tài liệu cùng người dùng

Tài liệu liên quan