Re: Изображение на кнопке
|
От: |
NortiBoy
|
|
| Дата: | 24.11.03 10:52 |
|
Оценка: |
|
Имеется ввиду проблема при Button.FlatStyle = System.Windows.Forms.FlatStyle.System ?
Если да, то задача решается только ручками примерно так:
public class ButtonWithImage : Button
{
private bool m_bIsButtonPressed;
public ButtonWithImage() : base()
{
}
protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
if ( m.Msg == (int)Msg.WM_PAINT || m.Msg == (int)Msg.WM_ENABLE )
{
this.PaintImage( m );
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown (e);
m_bIsButtonPressed = true;
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp (e);
m_bIsButtonPressed = false;
}
protected void PaintImage( Message m )
{
Image objImage = this.Image;
if ( objImage != null )
{
int ix = 0;
int iy = 0;
switch( this.ImageAlign )
{
case ContentAlignment.BottomCenter:
ix = (int)( this.Size.Width — objImage.Width )/2;
iy = this.Size.Height — objImage.Height;
break;
case ContentAlignment.BottomLeft:
ix = 0;
iy = this.Size.Height — objImage.Height;
break;
case ContentAlignment.BottomRight:
ix = this.Size.Width — objImage.Width;
iy = this.Size.Height — objImage.Height;
break;
case ContentAlignment.MiddleCenter:
ix = (int)( this.Size.Width — objImage.Width )/2;
iy = (int)( this.Size.Height — objImage.Height )/2;
break;
case ContentAlignment.MiddleLeft:
ix = 0;
iy = (int)( this.Size.Height — objImage.Height )/2;
break;
case ContentAlignment.MiddleRight:
ix = this.Size.Width — objImage.Width;
iy = (int)( this.Size.Height — objImage.Height )/2;
break;
case ContentAlignment.TopCenter:
ix = (int)( this.Size.Width — objImage.Width )/2;
iy = 0;
break;
case ContentAlignment.TopLeft:
ix = 0;
iy = 0;
break;
case ContentAlignment.TopRight:
ix = this.Size.Width — objImage.Width;
iy = 0;
break;
}
if ( this.Enabled )
{
if ( m_bIsButtonPressed )
{
ix += 1;
iy += 1;
}
using ( Graphics g = Graphics.FromHwnd( m.HWnd ) )
{
g.DrawImage( objImage, new Point( ix, iy ) );
}
}
else
{
using ( Graphics g = Graphics.FromHwnd( m.HWnd ) )
{
ControlPaint.DrawImageDisabled( g, objImage, ix, iy, Color.Black );
}
}
}
}
}
Пока на собственное сообщение не было ответов, его можно удалить.