c# picturebox控件的使用方法介绍
以TIM軟件為例,“微信登陸”圖標默認顯示為灰色。
鼠標移動到圖標,“預覽”顯示的圖標變化為高亮狀態。
點擊登陸圖標,登陸圖標高亮顯示。再次移動鼠標,不發生“預覽”變化效果。
下面使用c#語言編程,說明實現過程:
1、打開VS2012軟件,新建一個窗體應用程序;
2、“公共控件”,選擇“picturebox”控件,拖動到窗體。
? ?? ??
3、選擇“picturebox”控件,image屬性,添加兩張本地圖片,默認顯示為灰色圖片(未選中狀態)
? ? ? 圖片可以從“阿里巴巴矢量庫網站下載”網址:iconfont-阿里巴巴矢量圖標庫,搜索“收藏”即可獲取各種類型的收藏圖標,選擇喜歡的圖片,以png格式下載即可。
4、選擇“picturebox”控件,在事件中添加“MouseMove”鼠標移動到圖標函數、“MouseLeave”鼠標離開圖標函數。如下所示添加代碼。
? ? ? 可以實現,未選中狀態下,鼠標移動到圖標出現的“高亮”顯示效果。
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? pictureBox1.Image = Properties.Resources.blue;
? ? ? ? }
? ? ? ? private void pictureBox1_MouseLeave(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? ? ? pictureBox1.Image = Properties.Resources.gray;
? ? ? ? }
?5、添加一個布爾型的變量,緩存是否選中狀態,
? ? ? 添加,如下代碼。實現未選中狀態才出現預覽效果。
?public partial class Form1 : Form
? ? {
? ? ? ? bool Bcollect = false;
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? pictureBox1.Image = Properties.Resources.blue;
? ? ? ? }
? ? ? ? private void pictureBox1_MouseLeave(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (!Bcollect)
? ? ? ? ? ? ? ? pictureBox1.Image = Properties.Resources.gray;
? ? ? ? }
6、雙擊“picturebox”控件,添加單擊觸發函數。
? ? 如果鼠標左鍵點擊“picturebox”控件,“收藏”功能選中狀態變化,同時切換顯示圖標。
? ? ? ? private void pictureBox1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (!Bcollect)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Bcollect = true;
? ? ? ? ? ? ? ? pictureBox1.Image = Properties.Resources.blue;
? ? ? ? ? ? }
? ? ? ? ? ? else if (Bcollect)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Bcollect = false;
? ? ? ? ? ? ? ? pictureBox1.Image = Properties.Resources.gray;
? ? ? ? ? ? }
? ? ? ? }
9、視頻教程連接:
c#Picturebox控件的使用方法介紹-C#文檔類資源-CSDN下載
c#picturebox控件的使用方法介紹-C#文檔類資源-CSDN下載
總結
以上是生活随笔為你收集整理的c# picturebox控件的使用方法介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常见的PCA、tSNE、UMAP降维及聚
- 下一篇: 飞桨高阶使用教程:自定义CPU算子的实现