溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中怎么創(chuàng)建一個(gè)動(dòng)態(tài)圖像按鈕

發(fā)布時(shí)間:2021-07-07 16:16:22 來源:億速云 閱讀:198 作者:Leah 欄目:編程語(yǔ)言

本篇文章為大家展示了C#中怎么創(chuàng)建一個(gè)動(dòng)態(tài)圖像按鈕,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

我們要?jiǎng)?chuàng)建的控件簡(jiǎn)單說就是一個(gè)動(dòng)態(tài)漸變的C#動(dòng)態(tài)圖像按鈕

這個(gè)button控件依舊繼承自UserControl,UserControl是制作自定義UI控件絕對(duì)的父類,這里不再細(xì)說。我們?yōu)檫@個(gè)button取名為DynamicImageButton。制作圖像按鈕當(dāng)然離不開繪制,所以還得用到GDI+,我曾經(jīng)寫過多篇關(guān)于界面元素的文章,比如”利用.Net繪圖技術(shù)制作水晶按鈕控件”、” 利用C#實(shí)現(xiàn)任務(wù)欄通知窗口”、” 利用C#為數(shù)碼照片添加拍照日期”、” C#實(shí)現(xiàn)運(yùn)行時(shí)拖動(dòng)控件并調(diào)整控件大小”等等,其中都會(huì)涉及到GDI+的諸多方面,可見GDI+在設(shè)計(jì)制作UI上是多么的重要?。?/p>

對(duì)于按鈕圖片透明度漸變的操作則比較有技巧,采取了個(gè)人認(rèn)為比較另類卻極其高效的方法。制作圖片按鈕肯定是要為這個(gè)button賦值一個(gè)圖像文件的,我們需要公開一個(gè)屬性,代碼如下:

public Bitmap Image  {  get { return bmp[0]; }  set  {  bmp[0] = value;  bmp[1] = returnAlpha(value, 60);  bmp[2] = returnAlpha(value, 120); ;  bmp[3] = returnAlpha(value, 180); ;  draw();  }  }

大家注意到,當(dāng)給這個(gè)DynamicImageButton的 image屬性賦值一幅圖片后,立即就會(huì)對(duì)這個(gè)原始圖片經(jīng)過4種不同的alpha過濾后分別存放到bmp位圖數(shù)組下。bmp[0]保存原始圖像,bmp[3]的圖像則最透明。這就是本程序的特點(diǎn)所在,也就是在運(yùn)行時(shí)是不進(jìn)行圖像透明度漸變計(jì)算的,在給image屬性賦值時(shí)計(jì)算工作同時(shí)也已經(jīng)完成了,這樣可以省下鼠標(biāo)移動(dòng)事件的巨大計(jì)算量。

returnAlpha方法就是將原始圖像中的每一個(gè)像素按照相應(yīng)的alpha值進(jìn)行重新繪制后保存在bmp數(shù)組中,不同透明度的圖像作為bmp數(shù)組的不同元素進(jìn)行保存。代碼如下:

public static Bitmap returnAlpha(Bitmap bmp, int alpha)  {  Color col;  Bitmap bmp2 = new Bitmap(bmp);  for (int i = 0; i < bmp.Width; i++)  for (int j = 0; j < bmp.Height; j++)  {  col = bmp.GetPixel(i, j);  if (col.A > 0)  bmp2.SetPixel(i, j, Color.FromArgb(min(col.A - alpha), col.R, col.G, col.B));  }  return bmp2;  }

到這里大家可能就已經(jīng)明白我的用意了,沒錯(cuò)!從原始圖像到最終圖像的透明漸變我只設(shè)計(jì)了4幀!其實(shí),這對(duì)于一個(gè)小小的button動(dòng)畫來說已經(jīng)完全足夠了。對(duì)于圖像的繪制方法我們?nèi)耘f采用雙緩沖區(qū)繪制,也就是內(nèi)存復(fù)制,實(shí)際上就是雙bitmap對(duì)象交替使用,這樣可以更好的防止圖像閃爍(參見我的另一篇文章” .NET框架下使用雙緩沖技術(shù)繪圖”)。相關(guān)代碼如下:

private void DynamicImageButton_Paint(object sender, System.EventArgs e)  {  g2 = Graphics.FromImage(dblbuffer);  g2.Clear(this.BackColor);  curx = (int)((double)Width) / 6;  cury = (int)((double)Height) / 6;  curwidth = (int)((double)Width) / 3 * 2;  curheight = (int)((double)Height) / 3 * 2;  itvwidth = (Width - curwidth) / 2;  g2.DrawImage(bmp[3], curx, cury, curwidth, curheight);  g.DrawImageUnscaled(dblbuffer, 0, 0);  }

然后就是對(duì)C#動(dòng)態(tài)圖像按鈕大小漸變的控制了,如下圖所示:

C#中怎么創(chuàng)建一個(gè)動(dòng)態(tài)圖像按鈕

a、b、c和d四個(gè)矩形代表不同大小的4個(gè)幀,a幀是裝載圖像時(shí)的默認(rèn)大小,就是bmp[0]的圖像,也是4幀中***的一幀,width=75和 height=72是我們示例程序控件的大小,網(wǎng)友可以隨意對(duì)長(zhǎng)寬進(jìn)行重新設(shè)定。這兩個(gè)數(shù)值是基礎(chǔ),bmp[0]的圖像會(huì)完全填充到這個(gè)區(qū)域內(nèi),在這個(gè)給定的長(zhǎng)和寬的基礎(chǔ)上我們計(jì)算出b、c和d三幀的圖像大小和位置。然后創(chuàng)建一個(gè)計(jì)數(shù)器,當(dāng)鼠標(biāo)Enter或者Leave我們創(chuàng)建的這個(gè) DynamicImageButton時(shí)對(duì)圖像的透明度和大小的漸變進(jìn)行控制。相關(guān)代碼如下:

private void timer1_Tick(object sender, EventArgs e)  {  if (mp == enumMousePosition.Enter)  {  if ((curx <= 0) || (cury <= 0) || (Width == curwidth) || (Height == curheight))  {  return;  }  g2 = Graphics.FromImage(dblbuffer);  g2.Clear(this.BackColor);  //g2.Clear(Color.White);  if (curx >= itvwidth-2)  {  g2.DrawImage(bmp[3], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  else if ((curx>= itvwidth / 3 * 2) && (curx < itvwidth))  {  g2.DrawImage(bmp[2], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  else if ((curx >= itvwidth / 3) && (curx <= itvwidth / 3 * 2))  {  g2.DrawImage(bmp[1], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  else if ((curx >= 0) && (curx <= itvwidth / 3))  {  g2.DrawImage(bmp[0], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  g.DrawImageUnscaled(dblbuffer, 0, 0);  curx--;  cury--;  curwidthcurwidth = curwidth + 2;  curheightcurheight = curheight + 2;  if ((curx <= 0) || (cury <= 0) || (Width == curwidth) || (Height == curheight))  {  timer1.Stop();  }  }  else if (mp == enumMousePosition.Leave)  {  if ((curx >= (int)((double)Width) / 6) ||  (cury >= (int)((double)Height) / 6) ||  (curwidth <= (int)((double)Width) / 3 * 2) ||  (curheight == (int)((double)Height) / 3 * 2))  {  return;  }  g2 = Graphics.FromImage(dblbuffer);  g2.Clear(this.BackColor);  //g2.Clear(Color.White);  if (curx >= itvwidth-2)  {  g2.DrawImage(bmp[3], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  else if ((curx >= itvwidth / 3 * 2) && (curx < itvwidth))  {  g2.DrawImage(bmp[2], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  else if ((curx >= itvwidth / 3) && (curx <= itvwidth / 3 * 2))  {  g2.DrawImage(bmp[1], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  else if ((curx >= 0) && (curx <= itvwidth / 3))  {  g2.DrawImage(bmp[0], curx - 1, cury - 1, curwidth + 2, curheight + 2);  }  g.DrawImageUnscaled(dblbuffer, 0, 0);  curx++;  cury++;  curwidthcurwidth = curwidth - 2;  curheightcurheight = curheight - 2;  if ((curx >= (int)((double)Width) / 6) ||  (cury >= (int)((double)Height) / 6) ||  (curwidth <= (int)((double)Width) / 3 * 2) ||  (curheight == (int)((double)Height) / 3 * 2))  {  timer1.Stop();  }  }  }

上述內(nèi)容就是C#中怎么創(chuàng)建一個(gè)動(dòng)態(tài)圖像按鈕,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI