图片按钮CF.net

本文关键字:net CF 按钮 | 更新日期: 2023-09-27 18:28:04

我在windows mobile上为我的应用程序创建了一个图像按钮。不过,有一个小问题,当我有不同的屏幕分辨率时,例如更大的屏幕分辨率,图像不会自动调整大小以适应控件。如何做到这一点?

提前谢谢。

这是我的onPaint函数代码

        Dim gxOff As Graphics
        'Offscreen graphics
        Dim imgRect As Rectangle
        'image rectangle
        Dim backBrush As Brush
        'brush for filling a backcolor
        If m_bmpOffscreen Is Nothing Then
            'Bitmap for doublebuffering
            m_bmpOffscreen = New Bitmap(Me.Width, Me.Height)
        End If
        gxOff = Graphics.FromImage(m_bmpOffscreen)
        gxOff.Clear(Me.BackColor)
        If Not bPushed Then
            backBrush = New SolidBrush(Parent.BackColor)
        Else
            backBrush = New SolidBrush(Color.Black)
            'change the background when it's pressed
        End If
        gxOff.FillRectangle(backBrush, Me.ClientRectangle)
        If m_image IsNot Nothing Then
            'Center the image relativelly to the control
            Dim imageLeft As Integer = (Me.Width - m_image.Width) / 2
            Dim imageTop As Integer = (Me.Height - m_image.Height) / 2
            If Not bPushed Then
                imgRect = New Rectangle(imageLeft, imageTop, m_image.Width, m_image.Height)
            Else
                'The button was pressed
                'Shift the image by one pixel
                imgRect = New Rectangle(imageLeft + 1, imageTop + 1, m_image.Width, m_image.Height)
            End If
            'Set transparent key
            Dim imageAttr As New Imaging.ImageAttributes()
            imageAttr.SetColorKey(BackgroundImageColor(m_image), BackgroundImageColor(m_image))
            'Draw image
            gxOff.DrawImage(m_image, imgRect, 0, 0, m_image.Width, m_image.Height, GraphicsUnit.Pixel, imageAttr)
        End If
        If bPushed Then
            'The button was pressed
            'Prepare rectangle
            Dim rc As Rectangle = Me.ClientRectangle
            rc.Width -= 1
            rc.Height -= 1
            'Draw rectangle
            gxOff.DrawRectangle(New Pen(Color.Black), rc)
        End If
        'Draw from the memory bitmap
        e.Graphics.DrawImage(m_bmpOffscreen, 0, 0)
        MyBase.OnPaint(e)

图片按钮CF.net

您需要在OnPaint期间缩放图像,例如:

if (this._image != null)
{                    
  int x = (this.Width - this._image.Width) / 2;
  int y = (this.Height - this._image.Height) / 2;
  var imgRect = new Rectangle(x, y, this._image.Width, this._image.Height);
  var imageAttr = new ImageAttributes();
  Color clr = this._image.GetPixel(0, 0);
  imageAttr.SetColorKey(clr, clr);
  gr2.DrawImage(this._image, imgRect, 0, 0, this._image.Width, this._image.Height, GraphicsUnit.Pixel, imageAttr);
}

上面的例子是如何绘制基于第一个像素的透明颜色的图像。现在你需要修改第二个3行:

bool qvga = Screen.PrimaryScreen.Bounds.Height == 240 || Screen.PrimaryScreen.Bounds.Width == 240; 
int x = (this.Width - (qvga ? this._image.Width : this._image.Width * 2)) / 2;
int y = (this.Height - (qvga ? this._image.Height : this._image.Height * 2)) / 2;
var imgRect = new Rectangle(x, y, (qvga ? this._image.Width : this._image.Width * 2), (qvga ? this._image.Height : this._image.Height * 2));

[编辑]

看到您的代码更改是直接的。就像我在评论中描述的:对于两个主要的窗口移动分辨率,QVGA和VGA,我们假设图像是QVGA大小。请你自己把我的代码转换成VB。因此,如我所写,设置qvga标志:

bool qvga = Screen.PrimaryScreen.Bounds.Height == 240 || Screen.PrimaryScreen.Bounds.Width == 240; 

现在基于flag ammend以下基于我上面的代码:

Dim imageLeft As Integer = (Me.Width - m_image.Width) / 2
Dim imageTop As Integer = (Me.Height - m_image.Height) / 2

然后是这个:

If Not bPushed Then
     imgRect = New Rectangle(imageLeft, imageTop, m_image.Width, m_image.Height)
Else
     'The button was pressed
     'Shift the image by one pixel
imgRect = New Rectangle(imageLeft + 1, imageTop + 1, m_image.Width, m_image.Height)

结束如果