* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download BPB PUBLICATIONS - Escorts Library Catalogue
Survey
Document related concepts
Transcript
Mastering Delphi 2 f o r Windows® 9 5 / N T Marco Cantu BPB PUBLICATIONS B-14, CONNAUGHT PUCE, NEW DELHI-110001 TABLE OF CONTENTS Introduction Visual Programmiing and Language 1 A Form Is a Window Creating Your First Form Adding a Htle Saving the Form Using Components Changing Properties Responding to Events Compiling and Running a Program Changing Properties at Run-Time Adding Code to the Program ATwo-WayTool Looking at the Source Code The Textual Description of the Form The Project File Summary 2 Highlights of the Delphi Environment Different Versions of Delphi Asking for Help Delphi Menus and Commands Using the Menus The File Menu The Edit Menu The Search Menu The View Menu The Project Menu The Run Menu The Component Menu xxxv 1 3 4 5 6 7 9 10 15 16 17 19 20 23 25 26 27 28 29 30 30 30 31 35 36 36 38 38 xiii TABLE OF CONTENTS The Database Menu The Tools Menu The Help Menu The SpeedBar The SpeedMenus Working with Forms The Components Palette The Object Inspector The Alignment Palette Writing Code Managing Projects The Project Manager Setting Project Options Compiling a Project Exploring a Compiled Program The Integrated Debugger The Object Browser Additional Delphi Tools The Files Produced by the System Summary 3 The Object Repository and the Experts The Object Repository The Pages of the Object Repository The New Page The Current Project Page The Forms Page The Dialogs Page The Data Modules Page The Projects Page Delphi Experts The Database Form Expert The Application Expert The Dialog Box Expert Customizing the Object Repository Adding New Application Templates XIV 57 58 58 59 60 60 62 62 62 64 65 67 69 70 70 Table of Contents Adding New Form Objects to the Object Repository The Object Repository Options Summary 4 The Pascal Language Pascal Data Types Predefined Data Types Ordinal Types The RANGE Example Real Types Specific Windows Types Typecasting and Type Conversions The Variant Data Type User-Defined Data Types Subranges Enumerations Sets Arrays Records Pointers Files Pascal Strings Long Pascal Strings C-Like Character Arrays String Conversions Long Strings Conversion Blues Coding Style Comments Use of Uppercase White Spaces Pretty-Printing Syntax Highlighting Pascal Statements Expressions and Operators Set Operators Simple and Compound Statements 72 73 74 75 76 11 n 78 81 82 83 84 86 87 88 89 90 92 93 94 95 95 97 98 98 99 100 101 101 102 103 104 104 106 107 XV TABLE OF CONTENTS Pascal Conditional Statements If Statements Case Statements Pascal Loops For Statements While and Repeat Statements The With Statement Pascal Procedures and Functions Reference Parameters Constant Parameters Open Array Parameters Delphi 2 Calling Conventions What Is a Method? Forward Declarations External Declarations Procedural Types Summary 5 Object Pascal as an OOP Language Introducing Classes and Objects The Delphi Object Model The First Delphi Example with Classes Declaring a Constructor Classes and Information Hiding Private, Protected, and Public Public and Published Classes and Units The Interface of a Class Units and Scope Encapsulating Changes A Date Unit Units and Programs Advanced Topics Relating to Methods and Classes The Self Keyword Class Methods and Class Data Method Pointers XVI 108 108 110 110 110 111 113 115 116 116 117 117 117 118 119 120 123 125 126 128 130 132 133 134 135 136 139 140 141 142 145 146 146 147 148 Table of Contents Class References Inheriting from Existing Types Inheritance and Type Compatibility Virtual and Dynamic Methods Overriding Virtual and Static Methods Virtual versus Dynamic Methods Message Handlers Abstract Methods Run-Ume Type Information Handling Exceptions An Example of the Use of Exceptions Exceptions and the Stack The Finally Block Summary 6 Tiie Visual Component Library The Conceptual Foundation The VCL Hierarchy Components Objects Exceptions Using Components and Objects Properties The Name Property Properties Related to Component Size and Position The Enabled, Visible, and Showing Properties The Tag Property Properties Related to Color and Font Component Methods Component Events Delegation Is the Key Events Are Properties Using Delphi Collections The VCL Source Code Summary 150 153 155 158 161 162 163 163 167 168 171 174 176 178 181 182 185 185 189 190 192 193 197 198 199 200 201 203 205 206 206 207 208 209 XVII TABLE OF CONTENTS Using Components 7 A Tour of the Basic Components Windows' Own Components Clicking a Button The BUTTONS Example Clicking the Mouse on a Component Adding Colored Text to a Form The LABEL.CO Example The Standard Color Dialog Box Dragging from One Component to Another The Code for the DRAGGING Example Accepting Input from the User Handling the Input Focus Entering Numbers Sophisticated Input Schemes Creating a Simple Editor The Font Dialog Box Creating a Rich Editor Selecting a Choice Grouping Radio Buttons The PHRASES Example A List with Many Choices The Form for PHRASES2 Working with the List Boxes Removing a Selected String from the Other List Box Allowing Multiple Selections The Third Version of PHRASES Many Lists, Little Space Choosing a Value in a Range The Scroll Color Example Summary XVIII 211 213 214 215 215 218 219 219 220 221 222 223 224 226 230 232 233 233 236 237 238 240 241 242 243 247 248 251 253 253 256 Table of Contents 8 Creating and Handling Menus The Structure of a Menu Different Roles of Menu Items Changing Menu Items at Run-Time Editing a Menu with the Menu Designer The Standard Structure of a Menu Menu Shortcuts Using the Predefined Menu Templates Responding to Menu Commarnds The Code Generated by the Menu Designer The Code of the MENU^ONE Example Checking and Disabling Menus Changing Menu Items Changing Pull-Down Menus Using Radio Menu Items Changing the Menu Structure at Run-Time Short and Long Menus Customizing the Menu Check Mark Changing the System Menu The NOTES Program Menu Loading and Saving Files Pop-Up Menus (and the Right Mouse Button) The LOCAL Example Changing a Pop-Up Menu When It Is Activated Handling Pop-Up Menus Manually Summary 9 Bade to tiie Foim Forms versus Wbndows Overlapped, Pop-Up, and Child Windows The Application Is a Wmdow Setting Form Styles Creating Topmost Forms Avoiding Topmost Flickering 257 258 259 260 261 262 262 263 263 264 265 266 266 268 269 275 276 277 280 282 284 288 288 290 291 292 293 294 296 297 298 298 299 xix TABLE OF CONTENTS The Border Style The Effect of the BorderStyle Property The BORDERS Example The Border Icons Setting More Windows Styles Forms in Different Screen Resolutions Manual Form Scaling Automatic Form Scaling Setting the Form's Position and Size The Size of a Form and Its Client Area The Maximum and Minimum Size of a Form Automatic Form Creation Closing a Form Getting Mouse Input Using Windows without a Mouse The Mouse Events Drawing on the Form The Drawing Tools Drawing Shapes Drawing and Painting in Windows Painting a Single Shape Painting a Number of Shapes Delphi Output Components Summary 10 Graphical Components Improving the User Interface with Graphics A Bitmap in a Button A Car in a Button An Animated Bitmap in a Button A Two-State Button Many Images in a Bitmap The Rotating World A List of Bitmaps, the Use of Resources, and a PaintBox An Image \^ewer XX 301 302 303 304 305 306 307 309 310 313 314 316 318 320 321 321 325 325 326 328 330 331 334 334 337 338 339 340 343 344 345 347 349 352 Table of Contents Drawing in a Bitmap Drawing Shapes Graphical Lists The Outline of the Book A Tree of Chapters The Nodes of the Outline A Graphical List Building Grids A Grid of Fonts Mines in a Grid Choosing Colors Summary 11 A Toolbar and a Status Bar Grouping Controls with Panels Building a Toolbar A Simple Toolbar Enabling and Disabling Toolbar Buttons Adding Hints to the Toolbar Adding Hints to a Form Customizing Hints Adding Features to a Toolbar A Combo Box in a Toolbar A Toolbar You Can Drag Creating a Status Bar Menu Hints in the Status Bar Speed Button Hints in the Status Bar Summary 12 Multiple Forms and Dialog Boxes Dialog Boxes versus Forms Adding a Second Form to a Program Modal and Modeless Forms Two Forms, Two Menus Merging Form Menus 353 354 356 357 359 362 364 367 369 372 377 378 379 380 381 381 384 387 388 389 390 390 392 396 397 398 400 401 402 403 406 408 409 XXI TABLE OF CONTENTS Creating a Dialog Box Modal Dialog Boxes Closing a EKalog Box A Modeless Dialog Box Using Predefined Dialog Boxes \A^dows Common Dialogs Message Boxes Parade A Dialog Box as a Main V^ndow Extensible Dialog Boxes Special About Boxes Using the System About Box Building a Custom Hidden Screen Visual Form Inheritance Inheriting from a Base Form Polymorphic Forms Summary 13 Scrolliiig and Multiiiage RMins When Forms Are Too Big Scrolling a Form The Scroll Testing Example Automatic Scrolling Scrolling an Image Scrolling and Form Coordinates Building Notebooks with Delphi TabControl PageContiol, and TabSheets ANotebook with a Tab Set Changing the Page of a Notebook Tabbed Notebooks Notebooks witiiout Tabs and Tabs without Notebooks A Presentation in a Notebook An Image Viewer with Tabs An Image Browser with Tabs A Multipage Toolbar Summary XXII Table of Contents 14 Splitting Windows Form Splitting Techniques Splitting with a Header Using the HeaderControl Component Using the Header Component Setting a Maximum and Miiumtun Size A Header behind the Scenes Splitting with Panels Drawing a Split Line Direct Mouse Splitting Dragging the Mouse The Dragging Code A Custom File Manager Components Used in the File Manager Adding a Preview Pane to the File Manager Summary 15 Creating MDI Applications MDI in Windows: A Technical Overview Frame and Child Windows in Delphi A First Delphi MDI Demo Building a Complete \Afedow Menu Building a Child Window MDI Applications with Different Child Windows Adding a Boimcing Shape The Menu Bar of the New Child Form Changing the Main Form A Fast Start with MDI Summary 16 Using OLE Controls What Is an OLE Control? OLE Controls versus Delphi Components OLE Controls in Delphi Installing a New OLE Control 463 464 465 465 468 469 470 471 474 477 479 479 481 481 484 488 489 490 493 493 496 498 500 500 502 503 504 506 507 508 510 511 511 xxiii TABLE OF CONTENTS The TOLEControl Class Using OLE Controls Building a Chart But What about Writing OLE Controls in Delphi? Summary 17 Building Database Applications Data, Files, Databases, and Tables What Is a Table? Operations on Database Data Delphi Database Components Tables and Queries Other Data-Access Components Delphi Data-Aware Components Building Database Applications by Hand A Grid of Countries Customizing the DBGrid Navigating through Countries Using a Query A Query with Parameters Using the Database Form Expert Accessing the Fields of a Table or Query The TField Components An Example of a Calculated Field Using Fields to Manipulate a Table Looking for Records in a Table Changing the Data and Computing New Values Exploring the Tables of a Database Choosing a Database and a Table at Run-Time A Table Viewer A Field Editor An Outline for a Table Browser Creating a Graphical Table The Form and Its Startup Code Creating a New Table XXIV Table of Contents Choosing a Table with the Proper Fields Adding or Removing Records A Multi-Record Grid Moving Control Grid Panels Building a Master Detail Form with the Expert Making a Join with SQL Queries Providihg a Closed Selection in a Combo Box A Lookup in a Grid Summary 18 Client/Seiver and Advanced Dataiiase Applications Accessing a SQL Server A First InterBase Application Accessing a SQL Server InterBase Server Tools Server Manager Wmdows ISQL Database Explorer and Database Desktop Upsizing ari Existing Program Copying a Table Porting the Application Using the Visual Query Builder From Porting to Upsizing Joining Tables with the Visual Query Builder A Join with Three Tables A Join with More Tables From a Two-Her to a Three-Tier Architecture Creating a Data Module A Data Module for Multiple Views Setting Fields Properties and Initial Values Standard Tables Filtering Custom Table Filtering Custom Filtering and Client/Server Development Delphi Data Dictionary The Data Dictionary and the Fields Editor 568 570 571 572 573 576 577 578 580 581 582 583 584 585 585 585 587 588 588 590 590 592 593 594 595 598 599 600 602 603 605 607 608 608 XXV TABLE OF co^f^E^f^s What's in an Attribute Set? Exploring the Data Dictionary Summary Advanced Delphi Programming 1 9 MsccMfWiiittlieApiMicMiaiiSlffiici^ Using the Application Object Showing the Application Window Checking for Multiple Instances of an Application Testing to See if Another Instance Is Running Activating the Previous Instance Handling User-Defined \^ndows Messages Searching the Windows List Programming without Components The Smallest Delphi Program? Reading the Command Line Using the Command Show Parameter Events, Messages, and Multitasking in Windows Event-Driven Programming VNtodows Message Delivery Building a Clock with a Hmer Behind the Tuners: The Clock A FuU-Scale Graphical Clock Painting the Seccmds Hand with Raster Operations Windows Raster Modes The New Version of the Graphical Clock Idle Computing and Multitasking Background Processing Computing Prin^ Numbers the Dumb Way The Hourglass Apf^roach Background Proo^sing with timers Processiiitg Messages in the Baclqgiound Idle-Time Compufetg Multithreading in DdpM The TThread Class XXVI 610 612 613 615 #17 618 620 621 622 623 624 625 627 628 629 630 633 634 635 636 636 638 642 642 643 644 644 645 646 648 649 650 651 651 Table of Contents A First Example Synchronization Alternatives Setting Threads Priorities Synchronizing Threads A Final Multithreading Example Using the Screen Object Getting Screen Information Handling the Forms List Saving Status Information Using Windows INI Files Using the Registry Summary 20 Exploring the Behavior of a Program I>ebugging with the Debugger E)ebug Information Setting Breakpoints Inspecting Values More on Breakpoints Tracing through the VCL Source Code Alternative E)ebugging Techniques Debugging with a Terminal VWndow Debug and Release Versions \^ewing a Compiled Program with the Object Browser Exploring the Message Row Using V>^nSight The Class List and the WNDCLASS Structure The \>^dows Tree: Parents and Children Message How and Delphi Events A Look at Posted Messages The Memory Image of an Application Windows System Memory Free System Memory Summary 652 654 654 657 659 663 663 664 667 667 670 676 677 678 678 680 683 684 686 687 688 689 691 693 694 695 696 698 699 703 705 706 708 xxvii TABLE OF CONTENTS 2 1 Using Resources Resources in Windows Using Resource Editors Loading Resources in Delphi Loading an Icon or Bitmap as a Property The Manual Approach to Loading Resources The Icons for Applications and Forms Adding Alternative Icons for Different Conditions Using the Cursor in Delphi Designing a Custom Cursor A Flexible Cursor Using String Table Resources Translating the Strings into Another Language Summary 2 2 Adding Printing Capabilities to Delphi Applications Printing a Whole Form A Custom Print Dialog Box The Standard Print Dialog Boxes Accessing the Printer Object Printing Graphics and Using Print Preview Painting to the Printer Printing Text Printing Database Records and Tables The QuickReport Components A Quick Example Creating ReportSmith Reports Building a Custom Report Writing a ReportSmith Macro Quicker than ReportSmith Summary xxviii 709 710 711 714 714 715 716 718 720 720 723 726 730 732 733 734 735 736 739 740 743 745 747 750 751 754 756 759 761 764 Table of Contents 23 Adding File Support to Applications Files and the Pascal Language Handling Text Files A Text File Converter Saving Generic Data File Support in Delphi Components File System Components Streaming Data The Idea of Persistency Summary 24 Exchanging Data What Is the Clipboard? The Clipboard in Delphi Copying and Pasting Text Copying and Pasting Bitmaps Copying Delphi Components to the Clipboard Copying Custom Data to the Clipboard Dynamic Data Exchange (DDE): A Technical Overview DDE Conversations Roles of the Server and Client DDE in Delphi A Simple Example of DDE Building a Simple Server Building a Simple Client Design-Ume DDE Coimections Copying and Pasting DDE Links Copying Link Data to the Clipboard DDE with Timers and Graphics The Automatic Server A Graphical DDE Client Simimary 765 766 768 771 776 780 781 783 785 788 791 792 794 794 797 800 803 807 808 808 809 809 810 810 812 813 813 815 815 817 820 xxix TABLE OF CONTENTS 25 The Power Of OLE What Is an OLE Object? The Flavors of OLE OLE behind the Scenes OLE Support in I^elphi Building the Minimvim OLE Container The Minimal Menu of an OLE Container Visual Editing and Toolbars The OLE Standard Dialog Boxes Loading and Saving Objects in Files Multiple OLE Containers Creating OLE Containers at Run-Time Dragging Components and Activating OLE Containers OLE Automation Sending Data to Word Writing an OLE Automation Server Automating an MDI Application Registering the Automation Object Writing a Test Program The Future of OLE Summary 26 Multimedia Fun \^ftndows Default Sounds Every Box Has a Beep From Beeps to Music The Media Player Component Playing Sound Files Rimning Videos A Video in a Form Working with a CD Drive Simunary XXX 821 Table of Contents Creating CcHnponents and Libraries 867 27 CrMthig CompcNieiits 869 Extending the VCL Etelphi Components Are Classes Static Linking Components Rules for Writing Components Introducing Some Simple Components The Fonts Combo Box Using the Fonts Combo Box Creating a Tabbed List Box TestingttieTab List Component Building Braiul-New Components A Qrafrfucal Componait Built Sfc^ by Step Defining an Enumerated Property Drawing the Arrow Component listing the Arrow The Point of the Arrow, Plusa Pen and a Brush Testing the Second Version of fiie Arrow Defiiung a New Custom Event Testing AeOnArrowDblClick Event Adding a Bitmap forttieComponents Palette The Knai Test of the Arrow Component Ccxrecting Bup in the Arrow Component Arrows »id Sttxpes The Clock Compoitents The Digital Clock The Analog Clock A First Test InstallingttieClock Compcments Defining an Array Property The New Tabbed List 870 870 871 871 872 872 876 877 879 880 881 881 883 885 886 891 892 895 896 897 898 905 906 907 908 913 914 914 915 XMXi TABLE OF CONTENTS Using Dialog Box Units Installing the Component A Header and a Tabbed List Box Writing a Property Editor Forms Are Components, Too A Dialog Box in a Component Using the Nonvisual Component Summary 28 Dynamic Link Libraries The Role of DLLs in Windows What Is Dynamic Linking? What Are DLLs For? The System DLLs Differences between DLLs and EXEs Rules for DLL Writers Winl6 and Wm32 DLLs Using Existing DLLs Calling a C++DLL The C++ DLL Declaring the DLL Functions in Delphi Creating a DLL in Delphi Routines, Methods, Objects, and DLLs A First Simple Delphi DLL Placing a Form in a DLL Calling the DLL Form from Delphi Calling a Delphi DLL from Other Languages A DLL of Icons Loading the Icons from the DLL Calling a DLL Function at Run-Time A DLL in Memory Summary XXXII 916 9X7 918 919 925 925 928 929 931 Table of Contents 957 APPENDICES A A Short Ovenriew of OOP Concepts Abstraction in Programming Languages Classes Inheritance Polymorphism A Definition of OOP OOP Languages Object-Oriented Analysis and Design Summary B An Introduction to SQL What Is SQL? The Select Statement The Where Clause Avoiding Duplicates Making a Join More Select Clauses Choosing an Order Computing Values Defining Groups Beyond Select Summary Index 959 960 960 961 962 963 963 964 964 965 966 966 967 967 968 969 969 969 969 970 970 971 XXXIII