This quick reference lists useful classes and methods that you can use to develop simple programs in Windows Forms. See the official API documentation (System.Drawing, System.Windows.Forms) for a complete reference.
All methods below are public unless otherwise specified.
The pixel data for a graphics image.
Bitmap (string filename);
Load a Bitmap
from a file
containing an image in one of the following formats: BMP, GIF, EXIF,
JPG, PNG or TIFF. You must call Dispose
to dispose the
Bitmap
when you are finished using it.
A color and/or pattern used to fill the interiors of graphical shapes.
A class with read-only Brush
objects.
static Brush Black, Blue, Brown, Cyan, Gold, Gray, Green,
Magenta, Maroon, Olive, Orange, Pink, Plum, Purple, Red, Silver, Tan,
Violet, White, Yellow { get; }
Brush
objects
for standard colors. This is just a small selection; see the API
documentation for a full list.
An RGB (red, green, blue) color including an alpha (transparency) component.
static Color
FromArgb
(int red, int green, int blue);
static Color FromArgb (int
alpha, int red, int green, int blue);
Construct a component from red, green, blue, and optional alpha components. Each component ranges from 0 to 255.
static Color Black, Blue, Brown, Cyan, Gold, Gray, Green,
Magenta, Maroon, Olive, Orange, Pink, Plum, Purple, Red, Silver, Tan,
Violet, White, Yellow { get; }
Standard colors. This is just a small selection; see the API documentation for a full list.
A font family (such as "Times New Roman") plus size and style attributes.
Font (FontFamily family, float emSize);
Create a font with the given family
and em-size in points. You should call Dispose()
to
dispose the font's resources when you are finished using it.
A typeface such as Times New Roman.
static FontFamily GenericSansSerif { get; }
static
FontFamily GenericSerif { get; }
Generic serif and sans serif typefaces.
A drawing surface.
void DrawEllipse (Pen pen, float x, float y, float width,
float height);
Draw an ellipse defined by a bounding rectangle specified by coordinates.
void DrawImage (Image image, float x, float y);
Draw an image at the specified location.
void DrawLine (Pen pen, float x1, float y1, float x2, float
y2);
Draw a line between two points specified by coordinates.
void DrawPolygon (Pen pen, PointF[] points);
Draw a polygon whose vertices are given by an array of points.
void DrawRectangle (Pen pen, float x, float y, float width,
float height);
Draw a rectangle specified by coordinates.
void DrawString (string s, Font font, Brush brush, float x,
float y);
Draw a string. (x, y) will be the upper-left corner of the text that is drawn.
void FillEllipse (Brush brush, float x, float y, float
width, float height);
Fill an ellipse defined by a bounding rectangle specified by coordinates.
void FillPolygon (Brush brush, PointF[] points);
Fill a polygon whose vertices are given by an array of points.
void FillRectangle (Brush brush, float x, float y, float
width, float height);
Fill a rectangle specified by coordinates.
void RotateTransform (float angle);
Prepend a rotation in degrees
to this Graphics
object's transformation matrix.
void ScaleTransform (float sx, float sy);
Prepend a scaling transformation to
this Graphics
object's transformation matrix.
void TranslateTransform (float dx, float dy);
Prepend a translation to this Graphics
object's transformation matrix.
An abstract base class for images such as Bitmap
objects.
A color and/or pattern used to draw lines and curves. You should call Dispose() to dispose the pen's resources when you are finished using it.
Pen (Color color, float width);
Create a new Pen
with the
given color and width. If width
is 0, the pen will draw
as if the width were 1, even in the presence of a scaling
transformation.
A class with read-only Pen
objects.
static Pen Black, Blue, Brown, Cyan, Gold, Gray, Green,
Magenta, Maroon, Olive, Orange, Pink, Plum, Purple, Red, Silver, Tan,
Violet, White, Yellow { get; }
Pen
objects
for standard colors. This is just a small selection; see the API
documentation for a full list.
A pair of numbers representing a geometric point.
PointF (float x, float y);
Create a PointF
.
float X { get; set; }
float Y { get; set; }
Get or set the X- or Y-coordinate of
this PointF
.
A pair of integers representing a width and height.
Size (int width, int height);
Create a Size
.
int Height { get; set; }
int Width { get; set; }
Get or set the width or height of this
Size
.
A brush that draws in a single color.
public SolidBrush (Color color);
Create a SolidBrush
.
An enumeration of values that specify how a control anchors to the
edges of its container. Values include Left
, Right
,
Top
, Bottom
, and None
.
A class with static methods for managing a graphical application.
static void Exit ();
Exit the application.
static void Run (Form mainForm);
Display the given form and run the application's main loop.
A push button.
public Button ();
Create a new Button
.
A component with a visual representation.
AnchorStyles Anchor { get; set; }
A bitwise combination of AnchorStyles
values indicating the set of container edges that the control is
bound to. The default value is AnchorStyles.Top |
AnchorStyles.Left
.
If a control is bound to opposite
edges (e.g. both Left
and Right
) then it
will resize with its parent in that direction.
Color BackColor { get; set; }
Get or set the control's background color.
Control.ControlCollection Controls { get; }
Get the set of controls contained within the control.
void Invalidate ();
Ask for the control to be redrawn.
Point Location { get; set; }
Get or set the coordinates of the upper-left corner of the control relative to its parent.
Size Size { get; set; }
Get or set the height and width of the control.
string
Text { get; set; }
Get or set the text associated with the control.
event EventHandler Click;
An event that occurs when the control is clicked.
event KeyEventHandler KeyDown;
An event that occurs when a key is pressed while the control has focus.
event KeyPressEventHandler KeyPress;
An event that occurs when a key is pressed while the control has focus.
event KeyEventHandler KeyUp;
An event that occurs when a key is released while the control has focus.
event MouseEventHandler MouseDown;
An event that occurs when the mouse pointer is over the control and a mouse button is pressed.
event MouseEventHandler MouseMove;
An event that occurs when the mouse pointer is moved over the control.
event MouseEventHandler MouseUp;
An event that occurs when the mouse pointer is over the control and a mouse button is released.
event PaintEventHandler Paint;
An event that occurs when the control is redrawn.
A collection of Control objects.
void Add (Control value);
Add a control to the collection.
An enumeration of possible return values from a dialog box.
Values include OK
and Cancel
.
A window or dialog box.
The Text
property (inherited from Control
)
holds the text in the window's title bar.
Form ();
Create a new form, i.e. a window.
Size ClientSize { get; set; }
Get or set the size of the form's client area.
DialogResult ShowDialog ();
Show a form as a modal dialog box.
FormStartPosition StartPosition { get; set; }
The form's initial position.
An enumeration that specifies the initial position of a form. Values include
CenterScreen
- the form appears in the center of
the display
Manual
- the form's position is determined by
its Location
property
Data for a KeyDown
or KeyUp
event.
Keys KeyCode { get; }
Get the code of the key that was pressed or released.
A method that handles a KeyDown
or KeyUp
event.
delegate void KeyEventHandler(object sender, KeyEventArgs
e);
Data for a KeyPress
event.
char KeyChar { get; }
Get the character corresponding to the key that was pressed.
A method that handles a KeyPress
event.
delegate void KeyPressEventHandler(object sender,
KeyPressEventArgs e);
An enumeration with codes for each key on the keyboard. Some values include
A, B, ..., Z
- letter keys
LControlKey
, RControlKey
- the left and right control keys
Left
, Right
, Up
, Down
- the arrow keys
Space
- the space bar
See the API documentation for a complete list.
A dialog box that displays a message to the user.
static void Show (string text);
Display a message box with the specified text.
An enumeration with values representing mouse buttons. Values
include Left
, Middle
and Right
.
Data for a MouseDown
, MouseUp
or
MouseMove
event.
MouseButtons
Button
{ get; }
Get the mouse button that was pressed.
Point Location { get; }
Get the mouse position.
int X { get; }
Get the mouse's x-coordinate.
int Y { get; }
Get the mouse's y-coordinate.
A method that handles a MouseDown
, MouseUp
or MouseMove
event.
delegate void MouseEventHandler(object sender,
MouseEventArgs e);
Data for a Paint
event.
Graphics Graphics { get; }
A Graphics object used for painting.
A method that handles a control's Paint
event.
delegate void PaintEventHandler(object sender,
PaintEventArgs e);
A control that supports scrolling and can display scrollbars automatically.
virtual bool AutoScroll { get; set; }
Set this property to true
to enable scrolling.
Size AutoScrollMinSize { get; set; }
Set the size the virtual area through which the user can scroll.
Point AutoScrollPosition { get; set; }
Get or set the current scroll
position. You must offset by this position when drawing in a
ScrollableControl
. You can do that as follows:
// Paint
event handler
void
onPaint(
object
sender, PaintEventArgs args) {
Graphics g = args.Graphics;
g.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
…
A Timer fires an event periodically.
Timer ();
Create a Timer
.
bool Enabled { get; set; }
True if the timer is currently running. The default value is false.
int Interval { get; set; }
The timer interval in milliseconds.
event EventHandler Tick;
Occurs when the timer interval has elapsed.