在Winform的窗體中,存在背景圖的情況下,不想在畫面上加入按鈕,只希望利用窗體自身的幾個事件來實現按鈕按下的效果,怎麼樣實現呢?
其實這也不難,只需要按照下列步驟即可實現,首先確定要按下的幾個區域的坐標點和大小,准備好按下和彈出的圖片,將這些坐標點和大小
組成一個個區域(使用GraphicsPath來完成)。然後使用幾個Flage標志,標識是否區域的按下。具體實現的代碼如下:
/// <summary>
/// 實時監控,酒位展示,酒櫃介紹,網絡訂購4個按鈕的位置參數
/// </summary>
int[,] BtnLocation = new int[4, 2]{{637,161},{683,269},{714,374},{637,478}};
/// <summary>
/// 4個按鈕的Size參數
/// </summary>
Size BtnSize = new Size(285,92);
/// <summary>
/// 實時監控按鈕是否按下的標志
/// </summary>
bool FlageMouseDownRealTime = false;
/// <summary>
/// 酒位展示按鈕是否按下的標志
/// </summary>
bool FlageMouseDownWinePos = false;
/// <summary>
/// 酒櫃介紹按鈕是否按下的標志
/// </summary>
bool FlageMouseDownCupIntrol = false;
/// <summary>
/// 網絡訂購按鈕是否按下的標志
/// </summary>
bool FlageMouseDownNetPurchase = false;
/// <summary>
/// 實時監控按鈕的位置范圍
/// </summary>
GraphicsPath gpRealTime = new GraphicsPath();
/// <summary>
/// 酒位展示按鈕的位置范圍
/// </summary>
GraphicsPath gpWinePos = new GraphicsPath();
/// <summary>
/// 酒櫃介紹按鈕的位置范圍
/// </summary>
GraphicsPath gpCupIntrol = new GraphicsPath();
/// <summary>
/// 網絡訂購按鈕的位置范圍
/// </summary>
GraphicsPath gpNetPurchase = new GraphicsPath();
Graphics dc = null;
/// <summary>
/// 獲取4個按鈕的區域范圍
/// </summary>
private void GetButtonGraphicPath()
{
FlageMouseDownRealTime = false;
FlageMouseDownWinePos = false;
FlageMouseDownCupIntrol = false;
FlageMouseDownNetPurchase = false;
Point[] pt = new Point[]{
new Point(BtnLocation[0,0],BtnLocation[0,1]),
new Point(BtnLocation[0,0]+BtnSize.Width,BtnLocation[0,1]),
new Point(BtnLocation[0,0]+BtnSize.Width,BtnLocation[0,1]+BtnSize.Height),
new Point(BtnLocation[0,0],BtnLocation[0,1]+BtnSize.Height)
};
gpRealTime.AddLines(pt);
pt = new Point[]{
new Point(BtnLocation[1,0],BtnLocation[1,1]),
new Point(BtnLocation[1,0]+BtnSize.Width,BtnLocation[1,1]),
new Point(BtnLocation[1,0]+BtnSize.Width,BtnLocation[1,1]+BtnSize.Height),
new Point(BtnLocation[1,0],BtnLocation[1,1]+BtnSize.Height)
};
gpWinePos.AddLines(pt);
pt = new Point[]{
new Point(BtnLocation[2,0],BtnLocation[2,1]),
new Point(BtnLocation[2,0]+BtnSize.Width,BtnLocation[2,1]),
new Point(BtnLocation[2,0]+BtnSize.Width,BtnLocation[2,1]+BtnSize.Height),
new Point(BtnLocation[2,0],BtnLocation[2,1]+BtnSize.Height)
};
gpCupIntrol.AddLines(pt);
pt = new Point[]{
new Point(BtnLocation[3,0],BtnLocation[3,1]),
new Point(BtnLocation[3,0]+BtnSize.Width,BtnLocation[3,1]),
new Point(BtnLocation[3,0]+BtnSize.Width,BtnLocation[3,1]+BtnSize.Height),
new Point(BtnLocation[3,0],BtnLocation[3,1]+BtnSize.Height)
};
gpNetPurchase.AddLines(pt);
}
private void InitialButtonsStyle()
{
GetButtonGraphicPath();
pxControl.Location = new Point(0,0);
pxControl.Size = new Size(1024,768);
SetBackGroudImg();
this.Controls.Add(pxControl);
dc = pxControl.CreateGraphics();
pxControl.MouseDoubleClick +=new MouseEventHandler(this.Main_MouseDoubleClick);
pxControl.MouseDown+=new MouseEventHandler(this.pxControl_MouseDown);
pxControl.MouseUp += new MouseEventHandler(this.pxControl_MouseUp);
pxControl.MouseMove += new MouseEventHandler(this.pxControl_MouseMove);
pxControl.MouseClick += new MouseEventHandler(this.pxControl_MouseClick);
}
/// <summary>
///按下4個按鈕時的狀態變化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pxControl_MouseDown(object sender,MouseEventArgs e)
{
if (gpRealTime.IsVisible(e.Location))
{
using(Image imgTemp = Image.FromFile(strWineRealStatusDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width,BtnSize.Height);
FlageMouseDownRealTime = true;
}
}
if (gpWinePos.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWinePosQueryDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownWinePos = true;
}
}
if (gpCupIntrol.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWineCupIntroDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownCupIntrol = true;
}
}
if (gpNetPurchase.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWinePurchaseDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownNetPurchase = true;
}
}
}
/// <summary>
///離開4個按鈕時的狀態變化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pxControl_MouseMove(object sender, MouseEventArgs e)
{
if (gpRealTime.IsVisible(e.Location))
{
if (FlageMouseDownRealTime)
{
using (Image imgTemp = Image.FromFile(strWineRealStatusDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
}
}
}
else
{
if (FlageMouseDownRealTime)
{
using (Image imgTemp = Image.FromFile(strWineRealStatusUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
}
FlageMouseDownRealTime = false;
}
}
if (gpWinePos.IsVisible(e.Location))
{
if (FlageMouseDownWinePos)
{
using (Image imgTemp = Image.FromFile(strWinePosQueryDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
}
}
}
else
{
if (FlageMouseDownWinePos)
{
using (Image imgTemp = Image.FromFile(strWinePosQueryUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
}
FlageMouseDownWinePos = false;
}
}
if (gpCupIntrol.IsVisible(e.Location))
{
if (FlageMouseDownCupIntrol)
{
using (Image imgTemp = Image.FromFile(strWineCupIntroDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
}
}
}
else
{
if (FlageMouseDownCupIntrol)
{
using (Image imgTemp = Image.FromFile(strWineCupIntroUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
}
FlageMouseDownCupIntrol = false;
}
}
if (gpNetPurchase.IsVisible(e.Location))
{
if (FlageMouseDownNetPurchase)
{
using (Image imgTemp = Image.FromFile(strWinePurchaseDownPath))
{
dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
}
}
}
else
{
if (FlageMouseDownNetPurchase)
{
using (Image imgTemp = Image.FromFile(strWinePurchaseUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
}
FlageMouseDownNetPurchase = false;
}
}
}
/// <summary>
///松開4個按鈕時的狀態變化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pxControl_MouseUp(object sender, MouseEventArgs e)
{
if (gpRealTime.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWineRealStatusUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownRealTime = false;
}
}
if (gpWinePos.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWinePosQueryUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownWinePos = false;
}
}
if (gpCupIntrol.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWineCupIntroUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownCupIntrol = false;
}
}
if (gpNetPurchase.IsVisible(e.Location))
{
using (Image imgTemp = Image.FromFile(strWinePurchaseUpPath))
{
dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
FlageMouseDownNetPurchase = false;
}
}
}
/// <summary>
/// 按下4個按鈕時的事件的變化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pxControl_MouseClick(object sender, MouseEventArgs e)
{
if (gpRealTime.IsVisible(e.Location))
{
btnRealStatus_Click(null, null);
}
else if (gpWinePos.IsVisible(e.Location))
{
btnWinePosQuery_Click(null, null);
}
else if (gpCupIntrol.IsVisible(e.Location))
{
btnWineCupIntro_Click(null, null);
}
else if (gpNetPurchase.IsVisible(e.Location))
{
btnPurchase_Click(null, null);
}
}