2 // System.Windows.Forms.PicureBox
5 // stubbed out by Hossein Safavi (hsafavi@purdue.edu)
6 // Dennis Hayes (dennish@raytek.com)
7 // Aleksey Ryabchuk (ryabchuk@yahoo.com)
9 // (C) 2002 Ximian, Inc
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.ComponentModel
;
35 namespace System
.Windows
.Forms
{
39 public class PictureBox
: Control
{
42 PictureBoxSizeMode sizeMode
;
43 BorderStyle borderStyle
;
48 sizeMode
= PictureBoxSizeMode
.Normal
;
49 borderStyle
= BorderStyle
.None
;
50 SetStyle ( ControlStyles
.UserPaint
, true );
51 SetStyle ( ControlStyles
.Selectable
, false );
54 public BorderStyle BorderStyle
{
55 get { return borderStyle; }
57 if ( !Enum
.IsDefined ( typeof(BorderStyle
), value ) )
58 throw new InvalidEnumArgumentException( "BorderStyle",
62 if ( borderStyle
!= value ) {
75 if ( sizeMode
== PictureBoxSizeMode
.AutoSize
&& image
!= null )
76 SetBounds ( 0, 0, 0, 0, BoundsSpecified
.None
);
82 [EditorBrowsable (EditorBrowsableState
.Never
)]
83 public new ImeMode ImeMode
{
84 get { return base.ImeMode; }
85 set { base.ImeMode = value;}
88 protected override CreateParams CreateParams
{
90 RegisterDefaultWindowClass ( );
92 CreateParams createParams
= base.CreateParams
;
94 createParams
.ClassName
= Win32
.DEFAULT_WINDOW_CLASS
;
96 createParams
.Style
|= (int) (
97 WindowStyles
.WS_CHILD
|
98 WindowStyles
.WS_VISIBLE
|
99 WindowStyles
.WS_CLIPCHILDREN
|
100 WindowStyles
.WS_CLIPSIBLINGS
);
102 switch ( BorderStyle
) {
103 case BorderStyle
.Fixed3D
:
104 createParams
.ExStyle
|= (int)WindowExStyles
.WS_EX_CLIENTEDGE
;
106 case BorderStyle
.FixedSingle
:
107 createParams
.Style
|= (int) WindowStyles
.WS_BORDER
;
114 protected override ImeMode DefaultImeMode
{
115 get { return ImeMode.Disable; }
118 protected override Size DefaultSize
{
119 get { return new Size(100,50); }
122 public override string ToString()
124 return GetType( ).FullName
.ToString ( ) + ", SizeMode: " + SizeMode
.ToString ( );
127 public PictureBoxSizeMode SizeMode
{
128 get { return sizeMode; }
130 if ( !Enum
.IsDefined ( typeof(PictureBoxSizeMode
), value ) )
131 throw new InvalidEnumArgumentException( "SizeMode",
133 typeof( PictureBoxSizeMode
) );
135 if ( sizeMode
!= value ) {
138 if ( sizeMode
== PictureBoxSizeMode
.AutoSize
)
139 SetBounds ( 0, 0, 0, 0, BoundsSpecified
.None
);
141 SetStyle ( ControlStyles
.AllPaintingInWmPaint
, sizeMode
== PictureBoxSizeMode
.StretchImage
);
144 OnSizeModeChanged ( EventArgs
.Empty
);
151 protected override void Dispose(bool disposing
) {
152 base.Dispose(disposing
);
156 protected override void OnEnabledChanged(EventArgs e
)
159 base.OnEnabledChanged(e
);
162 protected override void OnPaint(PaintEventArgs pe
)
164 if ( Image
!= null ) {
165 switch ( SizeMode
) {
166 case PictureBoxSizeMode
.StretchImage
:
167 pe
.Graphics
.DrawImage ( Image
, ClientRectangle
);
169 case PictureBoxSizeMode
.CenterImage
:
170 int dx
= (ClientRectangle
.Width
- Image
.Width
)/2;
171 int dy
= (ClientRectangle
.Height
- Image
.Height
)/2;
172 pe
.Graphics
.DrawImage ( Image
, dx
, dy
);
175 pe
.Graphics
.DrawImage ( Image
, 0, 0 );
182 protected override void OnParentChanged(EventArgs e
)
184 if ( Parent
!= null ) {
185 BackColor
= Parent
.BackColor
;
189 base.OnParentChanged(e
);
192 protected override void OnResize(EventArgs e
)
194 if ( SizeMode
== PictureBoxSizeMode
.CenterImage
)
196 else if ( SizeMode
== PictureBoxSizeMode
.StretchImage
&& IsHandleCreated
)
197 Win32
.InvalidateRect ( Handle
, IntPtr
.Zero
, 0 );
202 protected virtual void OnSizeModeChanged(EventArgs e
)
204 if ( SizeModeChanged
!= null )
205 SizeModeChanged ( this, e
);
209 protected override void OnVisibleChanged(EventArgs e
)
211 base.OnVisibleChanged ( e
);
215 //this should be inherited.
216 protected override void OnPaintBackground (PaintEventArgs e
) {
217 if ( SizeMode
!= PictureBoxSizeMode
.StretchImage
)
218 base.OnPaintBackground ( e
);
221 protected override void SetBoundsCore(int x
,int y
,int width
,int height
,BoundsSpecified specified
)
223 if ( SizeMode
== PictureBoxSizeMode
.AutoSize
&& Image
!= null ) {
225 height
= Image
.Height
;
226 specified
= BoundsSpecified
.Size
;
229 base.SetBoundsCore(x
, y
, width
, height
, specified
);
232 public event EventHandler SizeModeChanged
;