This quick reference lists 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.
Bitmap
| Brush | Brushes
| Color | Font
| FontFamily | Graphics
| Image | Pen
| Pens | Point
| Rectangle | Size
| SolidBrush |
StringAlignment |
StringFormat
Application
| Control | FileDialog
| Form | Keys
| MenuStrip | MessageBox
| OpenFileDialog
| SaveFileDialog
| ScrollableControl
| TextBox | Timer
| ToolStripMenuItem
The pixel data for a graphics image.
Bitmap (int width, int height);
Bitmap
with the given width and height
in pixels.Bitmap (string filename);
Bitmap
from a file containing an image in one of
the following formats: BMP, GIF, EXIF, JPG, PNG or TIFF.
Bitmap
when you are finished using it.flags
should be ImageLockMode.ReadWrite
if you wish to read and write the pixel data. Typically format
will be the image's own pixel
format, which you can read from the PixelFormat
property.A color and/or pattern used to fill the interiors of graphical shapes.
A class with read-only Brush
objects
representing various solid colors.
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);
static Color Black, Blue, Brown, Cyan, Gold, Gray, Green,
Magenta, Maroon, Olive, Orange, Pink, Plum, Purple, Red, Silver,
Tan, Violet, White, Yellow { get; }
A font family (such as "Times New Roman") plus size and style attributes.
Font (FontFamily family, float emSize);
Dispose()
to dispose the font's resources when you are
finished using it.A typeface such as Times New Roman.
static FontFamily GenericMonospace { get;
}
static FontFamily GenericSansSerif { get; }
static
FontFamily GenericSerif { get; }
A drawing surface. Typically you
will use a Graphics
object to draw on a
window in the
OnPaint
handler of a Form subclass.
void DrawEllipse (Pen pen,
int
x,
int
y,
int
width,
int
height);
void DrawEllipse (Pen pen,
Rectangle
rect
);
void DrawImage (Image image,
int
x,
int
y);
void DrawImage (Image image,
Point
p
);
void DrawImage (Image image,
Rectangle r);
Rectangle
is passed, the image is scaled to fit in the given rectangle.void DrawLine (Pen pen,
int
x1,
int
y1,
int
x2, i
nt
y2);
void DrawLine (Pen pen,
Point
p1, Point p2
);
void DrawPolygon (Pen pen, Point[] points);
void DrawRectangle (Pen pen,
int
x,
int
y,
int
width,
int
height);
void DrawRectangle (Pen pen,
Rectangle
rect
);
void DrawString (string s, Font font, Brush brush,
Rectangle
rect, StringFormat format
);
Rectangle
. The parameter
format
contains
options indicating how text should be aligned in the rectangle.void FillEllipse (Brush brush,
int
x,
int
y,
int
width,
int
height);
void FillEllipse (Brush brush,
Rectangle
rect
);
void FillPolygon (Brush brush, Point[] points);
void FillRectangle (Brush brush,
int
x,
int
y,
int
width,
int
height);
void FillRectangle (
Brush
brush
, Rectangle rect);
Graphics
object that will draw on the given
Image
.DrawImage()
. The default mode Bilinear
is
relatively fast but produces poor results when
shrinking an image below 50% of its original size.
HighQualityBilinear
is slower but can shrink images
more nicely.void RotateTransform (float angle);
Graphics
object's
transformation matrix.void ScaleTransform (float sx, float sy);
Graphics
object's transformation matrix.void TranslateTransform (float dx, float dy);
Graphics
object's
transformation matrix.An abstract base class for images such as Bitmap
objects.
rotateFlipType
is a
constant such as Rotate90FlipX
specifying the rotation
and/or flip to perform; see the API
documentation for a list of possible types.A color and/or pattern used to draw lines and curves. Call Dispose() to dispose the pen's resources when you are finished using it.
Pen (Color color, float width);
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 integers representing a geometric point.
Point (
int
x,
int
y);
Point
.int
X { get; set; }
int
Y { get; set; }
Point
.Point
by a given Size
.Size
from a Point
.A geometric rectangle with a location and size.
Rectangle
with the given upper-left corner and
size.Rectangle
contains the given point.A pair of integers representing a width and height.
Size (int width, int height);
Size
.int Height { get; set; }
int Width { get; set; }
Size
.Size
objects.Size
objects.A brush that draws in a single color.
public SolidBrush (Color color);
SolidBrush
.An enumeration of values indicating how text
should be aligned along a particular axis. The
value StringAlignment.Center
specifies
that text should be centered.
A StringFormat
object holds
various options indicating how text should be drawn.
StringFormat
.Pixel data from a bitmap image.
unsafe
C# language features that let you access unmanaged data directly.A enumeration of formats such as Jpeg
or Png
; see the API
documentation for a complete list.
An enumeration of ways in which pixel data can be
locked by the LockBits
method in the Bitmap
class. Typically you will use ImageLockMode.ReadWrite
.
An enumeration of value indicating formats in
which low-level pixel data may be represented as bytes. For a
photographic JPEG image, typically this will be Format24bppRgb
,
meaning that
each pixel is stored in 24 bits, with 8 bits (= 1 byte) for each of
red, blue, and green, and no alpha (transparency) channel. On Intel
architectures color bytes appear in the order blue, green, red.
A class with static methods for managing a graphical application.
static void Exit ();
static void Run (Form mainForm);
A component with a visual representation.
Color BackColor { get; set; }
Control.ControlCollection Controls { get; }
ControlCollection
includes
these methods:void Add (Control value);
Add a control to the collection.
void
Remove
(Control value);
Remove a control from the collection.
bool Focus ();
Font Font { get; set; }
void Invalidate ();
OnPaint()
in the near future.Point Location { get; set; }
Shift
,
Control
) are currently pressed.OnKeyDown
) or released
(OnKeyUp
) a key. KeyEventArgs
includes the
following properties:Keys KeyCode { get; }
KeyPressEventArgs
includes the following properties:bool Handled { get; set; }
If you set this field to true, then further event handlers will ignore the key press event since your code has already handled it.
char KeyChar
{ get; set; }
T
he
character corresponding to the key that the user has pressed.
MouseEventArgs
includes the following properties:MouseButtons Button { get; }
The mouse button that was pressed. MouseButtons
is an
enumeration including values Left
, Middle
and Right
.
Point Location { get; }
The mouse position, relative to the control reporting this event.
int X { get; }
The mouse's x-coordinate, relative to the control reporting this event.
int Y { get; }
The mouse's y-coordinate, relative to the control reporting this event.
PaintEventArgs
includes
the following property:Graphics Graphics { get; }
A Graphics
object
that should be used for painting.
virtual void OnResize (EventArgs e);
Size Size { get; set; }
string Text { get; set; }
Form
,
this is the text that appears in the title bar.)A standard dialog box for opening or saving a
file. Subclasses include OpenFileDialog
and
SaveFileDialog
. Call Dispose()
to free a
dialog box and its resources when you are finished using it.
DialogResult
is an
enumeration including values OK
and Cancel
.A window or dialog box.
The Text
property (inherited from
Control
) holds the text in the window's title bar.
Form ();
Size ClientSize { get; set; }
OnKeyDown
even if another control has focus.DialogResult ShowDialog ();
FormStartPosition StartPosition { get; set; }
FormStartPosition
is an
enumeration with values CenterScreen
(the form appears
in the center of the display) and Manual
(the form's
position is determined by its Location
property).An enumeration with codes for each key on the keyboard. Some values include
A, B,
..., Z
- letter keys
Back
- the Backspace key
Delete
- the Delete key
LControlKey
,
RControlKey
- the left and right control keys
Left
,
Right
, Up
, Down
- the arrow
keys
Space
- the space bar
Shift
, Control
, Alt
- modifier keys
See the API documentation for a complete list.
An form's main menu.
MenuStrip
.ToolStripMenuItem
to the collection by calling
Items.Add(item)
.A dialog box that displays a message to the user.
static void Show (string text);
A standard dialog box that prompts the user to open a file.
OpenFileDialog
.A standard dialog box that prompts the user to save a file.
Save
FileDialog
.A control that supports scrolling and can display scrollbars automatically.
virtual bool AutoScroll { get; set; }
true
to enable scrolling.Size AutoScrollMinSize { get; set; }
Point AutoScrollPosition { get; set; }
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 TextBox displays one or more lines of text that
the user can edit. Use the Text
property (found in the
Control
superclass) to get or set the text in the box.
A Timer fires an event periodically.
Timer ();
Timer
.bool Enabled { get; set; }
int Interval { get; set; }
event EventHandler Tick;
A ToolStripMenuItem
is an item that
appears in a menu. It may itself be a submenu, in which case it will
contain a set of child
ToolStripMenuItem
objects.
ToolStripMenuItem
that will run the given
EventHandler
when selected. image
can be an image to display in this
menu item, or null for none.ToolStripMenuItem
[]
items);ToolStripMenuItem
that represents a submenu
and contains the given set of child menu items. image
can be an image to display in this menu item, or null for none.