怎样在后台设置imageButton的背景图

怎样在后台设置imageButton的背景图,第1张

自定义背景图片.buttonClass

{

border-right: blue 1px solid

border-top: blue 1px solid

font-size: 9pt

background-image: url(button-back.gif)

vertical-align: middle

border-left: blue 1px solid

cursor: hand

color: #000000

border-bottom: blue 1px solid

font-family: "宋体"

text-align: center

}

对button加入这个样式单 想作一个带背景图片的TextBox,给个思路.txt

protected override void OnPaint(PaintEventArgs e)

{

Graphics g=textBox1.CreateGraphics()

Bitmap bt=new Bitmap(yourFile)

g.DrawImage(bt,0,0,textBox1.Width,textBox1.Height)

}

设置TextBox为Transparent,可以这样:

textBox1.BackColor= Color.FromArgb(0, textBox1.BackColor)

我大致做了一个,不过还是无法解决在编辑的时候也显示图片,代码如下:

public class DrawTextBox : System.Windows.Forms.UserControl

{

protected System.Windows.Forms.Label label

protected System.Windows.Forms.TextBox textBox

protected bool editing = false

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null

public DrawTextBox()

{

// This call is required by the Windows.Forms Form Designer.

InitializeComponent()

// TODO: Add any initialization after the InitForm call

}

public Label Label

{

get{return this.label}

}

public TextBox TextBox

{

get{return this.textBox}

}

public bool Editing

{

get{return this.editing}

set

{

if(this.editing != value)

{

if(!value)

this.EndEditing(value)

else

this.StartEditing()

}

}

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose()

}

}

base.Dispose( disposing )

}

#region Component Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.label = new System.Windows.Forms.Label()

this.textBox = new System.Windows.Forms.TextBox()

this.SuspendLayout()

//

// label

//

this.label.Dock = System.Windows.Forms.DockStyle.Fill

this.label.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle

this.label.Name = "label"

this.label.Size = new Size(88, 20)

this.label.TabIndex = 2

this.label.Text = "DrawTextBox1"

this.label.Visible = true

this.label.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Label_MouseUp)

//

// textBox

//

this.textBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle

this.textBox.Name = "textBox"

this.textBox.TabIndex = 2

this.textBox.Text = ""

this.textBox.Visible = false

this.textBox.Size = new Size(88, 20)

this.textBox.Location=new Point(0,0)

this.textBox.LostFocus += new System.EventHandler(this.TextBox_LostFocus)

//

// DrawTextBox

//

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.label,

this.textBox})

this.Name = "DrawTextBox"

this.ResumeLayout(false)

}

#endregion

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

{

if(Control.FromHandle(msg.HWnd) == this.textBox)

{

if(keyData == Keys.Enter)

{

this.EndEditing(true)

return true

}

else if(keyData == Keys.Escape)

{

this.EndEditing(false)

return true

}

}

return base.ProcessCmdKey(ref msg, keyData)

}

private void Label_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)

{

this.StartEditing()

}

protected virtual void StartEditing()

{

this.label.Visible=false

this.textBox.Modified = false

this.textBox.Visible = true

this.textBox.Text = this.label.Text

this.textBox.Focus()

this.textBox.SelectionStart = 0

this.textBox.SelectionLength = 0

this.editing = true

}

protected virtual void EndEditing(bool save)

{

if(!this.Editing)

return

if(save && this.textBox.Modified)

{

this.label.Text = this.textBox.Text

}

this.editing = false

this.textBox.Text = String.Empty

this.textBox.Visible = false

this.label.Visible=true

}

private void TextBox_LostFocus(object sender, System.EventArgs e)

{

this.EndEditing(true)

}

}

使用如下:

DrawTextBox myDrawText=new DrawTextBox()

myDrawText.Size=new Size(88,20)

myDrawText.Label.Image=Image.FromFile(@yourFile,false)

this.Controls.Add(myDrawText)

button.setBackgroundResource(R.drawable.beijing1)

上面是改变按钮背景的代码

可以做两组图片,分别为button1的选中和为选择状态、button2的选中和为选择状态,让后再button1和button2的点击事件中,对两个按钮的背景进行改变,就可以了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11921044.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存