Академический Документы
Профессиональный Документы
Культура Документы
PROGRAMMING
WINDOWS FORMS & GDI+
WINDOWS FORMS
INTRODUCTION
WINDOWS FORMS (WINFORMS)
Part of .NET Framework for creating desktop applications:
System.Windows.Forms namespace
effectively a wrapper over part of Windows API
Categories of classes:
core infrastructure (e.g. Application, Form)
controls derived from the Control class (e.g. Button, TextBox)
components not derived from the Control class (e.g. Timer,
ToolTip)
common dialog boxes (e.g. OpenFileDialog, PrintDialog)
Properties:
Region, Bounds, DesktopBounds, ClientRectangle
Left, Top, Width, Height
Right = Left + Width
Bottom = Top + Height
StartPosition
Location, DesktopLocation
Size, ClientSize
MinimumSize, MaximumSize
AutoSize, AutoSizeMode
WindowState, TopMost
Events:
ClientSizeChanged, SizeChanged
LocationChanged
MaximumSizeChanged, MinimumSizeChanged
Resize, ResizeBegin, ResizeEnd
ShowModal() Show()
ShowDialog()
AcceptButton, Close()
CancelButton, FormClosing, FormClosed
DialogResult
System.Windows.Forms
ButtonBase, Label, ListView, PictureBox, ProgressBar, ScrollBar,
TreeView, ...
ScrollableControl
Panel, ToolStrip
ContainerControl
PropertyGrid, SplitContainer, ToolStripContainer, ToolStripPanel, ...
UserControl
Form
Z-order
BringToFront(),
SendToBack()
Focus
ControlStyles.Selectable
AutoScrollPosition
VScroll, HScroll visibility of scroll bars
Scroll event notification of scrolling
LinkLabel
derived from Label
can display hyperlinks
DomainUpDown
Sorted
Wrap
UpButton(), DownButton()
CheckBox
Can have 2 or 3 check states (ThreeState)
ComboBox
single selection
possibility to enter a new value
NotifyIcon
Icon
ContextMenu
Text for icons tooltip
Visible
GroupBox
frame and caption around a group of controls
often used to group radio buttons
SplitContainer
two panels separated by a movable splitter
ContextMenu
MainMenu
StatusBar
ToolBar
Mnemonics
Accelerators (independent of mnemonics):
ShortcutKeys, ShowShortcutKeys,
ShortcutKeyDisplayString
ToolStripManager, ToolStripRenderer
ContainerControl
AutoValidate
Validate(), ValidateChildren()
ErrorProvider
SetIconAlignment, SetIconPadding
BlinkStyle, BlinkRate
ContainerControl
(when used with data-bound control)
SetError(control, message)
Complex binding:
DataGridView
DataSource
DataMember
ListBox, ComboBox,
DataSource
DisplayMember, ValueMember, SelectedValue
ContainerControl.BindingContext
manages collection of instances of BindingManagerBase:
PropertyManagers for simple binding
CurrencyManagers for complex
binding
this.BindingContext[DataItems].Position += 1;
Custom control
inherited from one of the base control classes
Component, Control, ScrollableControl, ContainerControl
the most flexible (and the most time-consuming) way to create controls
Type editors
provide graphical interfaces for setting complex type values
For properties:
DefaultValueAttribute
EditorAttribute
LocalizableAttribute
TypeConverterAttribute
Keys enum
e.g. Keys.Q, Keys.F5, Keys.LShiftKey
Activating controls:
CanSelect
Select(), SelectNextControl()
Form.ActiveControl
.resx
the XML format
support for both strings and other objects such as images
.resources
the binary format
a binary equivalent of the XML file
the only format that can be embedded in an assembly, the other formats
must be converted
w.AddResource("Next", "Nastpny");
w.Generate();
w.Close();
Using C#
ResourceWriter rw =
new ResourceWriter(@"C:\myRes.resources");
w.Generate();
w.Close();
ResourceManager rm =
new ResourceManager("myApp.myRes",
Assembly.GetExecutingAssembly());
MessageBox.Show(rm.GetString("HelloWorld"));
pictureBox.Image =
(Bitmap)rm.GetObject("background");
rm.ReleaseAllResources();
Editing resources
Visual Studio built-in editors
the binary editor, image editor
external editors (e.g. the Paint for image files)
other applications can be associated with types of resources
Adding resource:
add file to the project
in its Properties, set Build Action: Embedded Resource
Retrieving resource:
Assembly.GetManifestResourceStream
type-specific methods
e.g. one of Bitmap class constructors
APPLICATION SETTINGS
APPLICATION SETTINGS
THE OLD WAY
.config files (XML)
special naming convention [executableFileName].config
can be read and modified by applications
<configuration>
<appSettings>
<add key="welcome" value="Hello, user!"/>
<add key=bye" value=Farewell!"/>
</appSettings>
</configuration>
<configuration>
...
<userSettings>
<Tests.Settings1>
<setting name="Setting1" serializeAs="String">
<value>test</value>
</setting>
</Tests.Settings1>
</userSettings>
<applicationSettings>
<Tests.Settings1>
<setting name="Setting2" serializeAs="String">
<value>test2</value>
</setting>
</Tests.Settings1>
</applicationSettings>
</configuration>
Cardinal splines:
DrawCurve(), DrawClosedCurve()
FillClosedCurve()
Bezier splines:
DrawBezier(), DrawBeziers()
Graphics.DrawPath()
Graphics.FillPath()
try {
ConvertImage(bird.jpg, bird.png, ImageFormat.Png);
}
catch (Exception exc) { }
Karol Waldzik - Windows Programming 99
TEXT
Graphics.DrawString()
at specified location
in a rectangle
StringFormat argument:
Alignment, LineAlignment
SetTabStops()
FormatFlags
TextRenderingHint allows switching on antialiasing
gr.DrawRectangle(pen,-60,-30,120,60);