簡體   English   中英

圖片按鈕CF.net

[英]Image button CF.net

我在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)

您需要在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);
}

上面的示例是如何基於第一個像素繪制具有透明顏色的圖像。 現在,您需要修改第二三行:

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));

[編輯]

看到代碼更改很簡單。 就像我在評論中描述的那樣:對於兩個主要的Windows移動分辨率,QVGA和VGA,我們假定該圖像適用於QVGA大小。 請自行將我的代碼轉換為VB。 所以在我寫的時候設置qvga標志:

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

現在基於我上面的代碼基於以下標記amend:

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)

萬一

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM