BinaryReader
The BinaryReader class, much like the BinaryWriter class, relies on a FileStream object for accessing files. To demonstrate the BinaryReader class, examine the application in Listing 24-2, which reads the binary header information from a bitmap file. From this header information, you can determine both the horizontal and vertical size of the image file as well as the color depth in bits. Listing 24-2 BitmapSize Application long bmpWidth 0 long bmpHeight 0 int bmpPlanes 0 int bmpBitCount 0...
Deriving from Base Interfaces
Interfaces can derive from base interfaces, just as classes derive from base classes. Base interfaces are listed after the colon that follows the derived interface name. Unlike base classes, interfaces can derive from more than one base interface. Multiple base interface names are separated by commas, as shown in the following example interface Interface1 void Method1 interface Interface2 void Method2 interface Interface3 Interface , Interface2 Deriving from a base interface is useful when an...
RemoveMessageFilter
The RemoveMessageFilter method removes a message filter installed by the AddMessageFilter method. It removes the message filter from the message pump of the application. The RemoveMessageFilter method takes one argument an implementation of an interface called IMessageFilter. This argument should reference a class that implements IMessageFilter and has already been used in a call to AddMessageFilter . Listing 21-6 shows how this method works. Listing 21-6 Removing an Installed Message Filter...
Output parameters
Output parameters are parameters whose values are not set when the method is called. Instead, the method sets the values and returns the values to the caller through the output parameter. Suppose, for example, that you want to write a method to count the number of records in a database table. Suppose that you'd also like to specify whether or not the operation was successful. The operation may be unsuccessful, for instance, if the database table is not available. You now have two pieces of...
AddMessageFilter
The AddMessageFilter method adds a message filter to your application to monitor Windows messages as they are routed to their destinations. The message filter that you install into your application receives the Windows messages before they are sent along to your form. A message filter installed by AddMessageFilter can choose to handle a message that is sent to it, and the filter can choose whether the message should be forwarded to your form. Listing 21-4 shows how a message filter can be used...
Using iteration statements to execute embedded statements
Iteration statements execute embedded statements multiple times. The expression associated with the iteration statements controls the number of times that an embedded statement executes. The while statement executes an embedded statement list as long as the while expression evaluates to true. The Boolean expression that controls the while statement is enclosed in parentheses that follow the while keyword. The statements to be executed while the Boolean expression evaluates to true follow the...
Using special characters in strings
C enables you to use a special syntax to embed special characters in your string. These special characters are listed in Table 3-6. The special characters t embed a tab into the string. A string defined as hello tthere is stored in memory with a tab character between the words hello and there The special characters r embed a carriage return into the string. A string defined as hello rthere is stored in memory with a carriage return character between the words hello and there. The carriage...
ConstructionEnabled
The ConstructionEnabled attribute enables COM object construction. The COM object construction mechanism enables a string to be passed as a con- structor string to instantiated object instances. The attribute does not specify the string instead, it merely enables the COM object construction support. The attribute accepts a Boolean as a parameter, which should be True if COM object construction should be enabled for the class, and False otherwise. C classes that support object construction must...
UserAppDataPath
The string UserAppDataPath property references a path on the file system that the application can use to store file-based data that should be available to the network user currently logged into the machine. This property is read-only and cannot be set. It is used for local users with operating system profiles on the network. Users who have local machine profiles that are not used across the network use a separate property, called LocalUserAppDataPath, to specify where their application data...
CommonAppDataPath
The string CommonAppDataPath property references a path on the file system that the application can use to store file-based data that should be available to all users of the application. This property is read-only and cannot be set. The application data path is stored beneath the Windows documents folder path used for all users, which is usually a path such as C Documents and Settings All Users Application Data. The actual application path points to a folder beneath this all users document path...
Understanding OLE DB and SQL Server Support
ADO.NET contains two sets of similar classes. One set is a generic set of classes that can be used to access all databases that have OLE DB providers. A second set of classes has been fine-tuned for Microsoft's flagship database, SQL Server. The names of the generic classes all start with OleDb. The SQL Server-specific classes all begin with Sql. Each generic class has a corresponding SQL Server-specific class. For example, the class you use to execute SQL statements against SQL Server is...
Working with Graphics
When working with GDI in .NET, the main object that you need to work with is the Graphics object. The Graphics object is the actual surface that you use to paint shapes, work with images, or display text. Visual Basic 6 and earlier offered limited built-in support for working with graphics, making it difficult for VB developers to write custom graphics applications. The one thing that VB did do was keep track of how forms and the objects on forms were painted on the screen. The AutoRedraw...
Understanding Application properties
The Application class supports several properties that you can use in your C applications. The following sections describe how to use each of these properties. The Boolean AllowQuit property specifies whether your code can programmatically end the application. The property returns True if your code can instruct the application to exit and False otherwise. This property is read-only and cannot be set. Ordinarily, users can end applications by closing the application's main form. Some...
DoEvents
The DoEvents method processes all messages currently in the application's Windows message queue. The method does not take any arguments and does not return a value. You may want to call this method if you want to make sure that waiting Windows messages are sent along to your form while you are doing other work. Suppose, for example, that you create a form that performs a lengthy calculation. If another window is moved in front of your form while the calculation is performed, Windows sends your...
SafeTopLevelCaptionFormat
The string SafeTopLevelCaptionFormat property references a format string that the runtime applies to top-level window captions when the applications execute from an unsafe context. Security is an integral part of the .NET Framework and the Common Language Runtime CLR . The CLR honors the different security zones set up in Internet Explorer Internet, Local Intranet, Trusted Sites, and Restricted Sites , and restricts runtime services to applications that run in untrusted zones. WindowsForms...
ExitThread
The ExitThread method exits the message loop and closes all forms on the current thread. The method does not take any arguments and does not return a value. If your WindowsForms application contains only one thread and most do , then calling ExitThread is the same as calling Exit . If, however, your application uses multiple threads, then the two methods behave differently. The ExitThread method shuts down one thread but allows the other threads to continue running. The Exit method, however,...
Using constructor methods
A structure may include a special method called a constructor. A constructor method executes when a variable declaration using the structure type is executed at runtime. Structures may have zero or more constructors. Structure constructor declarations are much like class method declarations, with the following exceptions Constructors do not return any values. Do not use a return type keyword when writing a structure constructor not even void. Constructor identifiers have the same name as the...
JustInTimeActivation
The JustInTimeActivation attribute enables or disables just-in-time JIT activation for a class. The attribute accepts a Boolean as a parameter, which should be True if JIT activation should be enabled for the class, and False otherwise. JIT activation must always be enabled for objects that participate in transactions. The JustInTimeActivation attribute must be applied at the class level, as in the following code snippet The JustInTimeActivation attribute does not accept any parameters. False...
ApplicationID
The ApplicationID attribute can be used to specify the GUID to be assigned to the COM application created to hold the COM class. The GUID is specified using its string representation, which is supplied to the attribute's constructor. The ApplicationID attribute must be applied at the assembly level, as in the following code snippet The attribute takes a string as a parameter that describes the application's GUID. ApplicationName You use the ApplicationName attribute to specify the name to be...
OnThreadException
The OnThreadException method raises a ThreadException event. The event can be caught by an OnThreadException event handler installed into the Application object. Listing 21-5 shows how a thread exception can be used in a WindowsForms application. Listing 21-5 Working with Thread Exceptions public class BlockLeftMouseButtonMessageFilter IMessageFilter public bool PreFilterMessage ref Message m LeftButtonDownException new Exception The left mouse button was pressed. return true public class...
AssemblyCulture
The AssemblyCulture attribute enables you to specify culture information for the assembly. Culture information specifies the language and country information that the assembly uses to do its work. The attribute takes a string parameter in its constructor that specifies the culture information, as shown in the following example assembly AssemblyCulture us-en Culture strings are defined by an Internet standard called RFC1766. The standard is titled Tags for the Identification of Languages and is...
LocalUserAppDataPath
The string LocalUserAppDataPath property references a path on the file system that the application can use to store file-based data that should be available to the user currently logged into the machine. This property is read-only and cannot be set. It is used for local users with operating system profiles on the local machine. Users who have roaming profiles that are used across the network use a separate property, called UserAppDataPath, to specify where their application data should be...
Using the remainder operator
The value of an expression using the remainder operator is the remainder of a division operation. The percent character is used as the division operator see Listing 4-14 . When this code is executed, the MyInteger variable has value of 1, because dividing 7 by 3 results in a quotient of 2 and a remainder of 1. If you are calculating a remainder using a variable and placing the result in the same variable, you can write a shortcut statement to perform the remainder operation. Writing a percent...
Using the Pen class
The Pen class enables you to draw lines and curves on a graphics surface. The namespace that contains the features used by the Pen and Brush classes is the System.Drawing.Drawing2D namespace, so be sure to add this with the using statement in your class files. By setting various properties on an instance of a Pen, you can alter the outcome of the pen display. By calling methods in the Graphics class, you can dictate the type of shape you want to output. The following code sets the Color and...
AssemblyTrademark
The AssemblyTrademark attribute enables you to specify trademark information for the assembly. The attribute takes a string parameter in its constructor that specifies the trademark information, as shown in the following example assembly AssemblyTrademark Windows is a trademark of Microsoft Corporation. The assembly product name is written to the assembly's manifest it is also available in the Legal Trademarks field of the compiled file's version information block.


