230 likes | 386 Views
Windows Programming. C# Software Development. Overview. DLLs / PInvoke DirectX .NET Component Library Custom Controls Event Model (in C++) GUI Events Custom Filters Drawing, Painting, Printing. Using DLLs. To gain access to a DLL, use the DllImport attribute to tag the method
E N D
Windows Programming C# Software Development
Overview • DLLs / PInvoke • DirectX • .NET Component Library • Custom Controls • Event Model (in C++) • GUI Events • Custom Filters • Drawing, Painting, Printing
Using DLLs • To gain access to a DLL, use the DllImport attribute to tag the method • Use the extern keyword on the method • Return/parameter types don't necessarily need to match • only the datatype size needs to match DllImport("gdi32.dll")] public static extern int SetPixel(int hDC, int x, int y, int crColor);
PInvoke • Calling a dll method is called P-Invoke (platform invoke) • Simply call the method • Some types (hdc, handles, etc) are exposed in the .NET interface (See WindowsManipulation and SetPixel Demos)
DirectX • See Mike Go • Go Mike Go
.NET Component Library • System.Windows.Forms.Control • Base class for controls • Visual Components • Handles Keyboard and Mouse Input • Provides bounds for painting • Does not actually paint anything by default • Provides a Windows Handle • Has ambient properties • if not set, an ambient property uses its parent's value • Provides accessibility support (See MSDN)
Windows Forms • Properties • Text • Text on top of the form • BackColor • Background color • ForeColor • Text Color • Font • Font used by child controls • FormBorderStyle • Border Style • ControlBox • Minimize, Maximize and Close buttons • ShowInTaskbar • Indicates whether to show in the taskbar or not
Splash screen • On a Form, set: • Text = ""; • ControlBox = false; • Create a Panel with the BackgroundImage property set to your picture
Menus • Create a Context Menu or a MainMenu • Associate it with the Form (or Control)
Adding Controls Programmatically • Form (or any other container control) has a Controls property Button newBtn = new Button … this.Controls.Add(newBtn);
Custom Controls • Create a new UserControl file in your project • Default UserControl class inherits from UserControl • You can inherit from a pre-built control (like button) or from Control itself
UserControl Attributes • ToolboxBitmap • Sets the icon for the UserControl • Browsable • Pass it false to prevent the member from being displayed • Category • Places the control in the specified category • DefaultValue • Sets a default value for the member • Description • Sets the description associated with the member • DesignOnly • Specifies that the member can only be set at design time
Adding UserControls to Toolbox • Build the UserControl • Right-click the toolbox • Choose Add/Remove, browse to the built assembly (.dll or .exe file).
Painting • Subscribe to the Paint event • Gives access to the appropriate Graphics object • Call Refresh() • Invalidates the entire client area including child controls • Call Invalidate() • Invalidates the given region (client area by default) • Some controls have BeginUpdate and EndUpdate methods • BeginUpdate disables drawing • Call when you are changing/updating the control • EndUpdate enables drawing • Call when you are finished changing/updating the control
Paint Event • private void Form1_Paint(object sender, PaintEventArgs e) • PaintEventArgs contains: • ClipRectangle being painted • Graphics for the current object
Graphics object • Methods: • Clear() • DrawArc() • DrawBezier() • etc. (See DrawDemo)
Brushes • System.Drawing.Brush • abstract class • Subclasses: • HatchBrush • LinearGradientBrush • PathGradientBrush • SolidBrush • TextureBrush • Can get texture from an image • Represents a color, gradient, image etc. • Fills the interior region of an area • Must be disposed when you're finished using it • Limited resource on Win9x/Me (as are pens)
Brush example: • Brush brush = new SolidBrush(Color.Red); • Brush brush = new TextureBrush(new Bitmap("MyImage.gif")); • Brush brush = Brushes.Brown;
System.Drawing.Brushes • System-stored default brushes • All kinds of crazy colors • Do not dispose System-stored resources • SolidBrush type
Pens • System.Drawing.Pen • Represent a color and width • Used to draw lines and curves • Has an associated brush • Can set: • DashPattern • DashCap • StartCap (arrow, etc) • EndCap • etc. • Need to be disposed when finished using
System.Drawing.Pens • System-stored default pens • All kinds of crazy colors • Do not dispose System-stored resources • Also System.Drawing.SystemPens
Fonts • Immutable • To change a font, you must create a new one, then reassign the property
Color • Represents an ARGB color • Contains hundreds of Pre-defined colors