国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

利用C#實現(xiàn)標注式消息提示窗口

2019-11-18 16:55:34
字體:
來源:轉載
供稿:網(wǎng)友

為了更加友好的將提示信息呈現(xiàn)給用戶,我們必須對標準的Windows消息提示窗口進行處理。我們大家在Windows xp下使用U盤、閃存等移動存儲設備,當插上或拔下這些設備時任務欄區(qū)域都會顯示一個淡黃色背景,且具有標注樣式的提示窗口彈出來,這樣的提示即友善又美觀,那么這到底是怎么實現(xiàn)的呢?其實道理并不復雜,該標注式提示窗口本身就是一個不規(guī)則窗體,當顯示時它會將標注窗口的箭頭指向不同控件。

  一、技術要點

  就像本文開頭所說的"標注式消息提示窗口"其實就是一個具有不規(guī)則外形的窗體,但卻具備了更加復雜的屬性和行為。標注的箭頭會根據(jù)不同控件指向不同的位置,當需要標注的控件過于接近屏幕的邊緣時,標注窗口還會自動調(diào)整顯示位置以及箭頭的長短和大小。

  我們?yōu)樾聞?chuàng)建的窗體取名為InfoWindow。在類的頭部定義intArc和intArrowHeight兩個私有變量,可以適當調(diào)整它們的值來微調(diào)提示窗口的位置和箭頭的大小與位置。

  提示窗口的箭頭位置無非具有左上、右上、左下和右下四個可能性,我們?yōu)榇硕x了枚舉類型的變量ArrowLocation,根據(jù)提示窗口位于屏幕的不同位置,GetArrowLocation可以計算提示窗口的位置并且返回適當?shù)腁rrowLocation,定義如下:

……
public enum ArrowLocation
{
 TopLeft,
 ToPRight,
 BottomLeft,
 BottomRight
}

  SetInfoWindowRegion函數(shù)非常重要,它在Form.Load事件即裝載和顯示提示窗體時被調(diào)用,當計算出新的提示窗口的位置和箭頭顯示位置后,調(diào)用SetBounds將更新后的位置和大小應用到提示窗口,gPath是GraphicsPath類型的私有變量,它表示標注式窗口的不規(guī)則圖形路徑,該圖行路徑也是根據(jù)提示窗口的位置和箭頭顯示的位置來創(chuàng)建,gPath.AddArc方法用來繪制提示窗口四個邊角的弧度部分,和AddLine方法一起描繪出提示窗口包括箭頭的輪廓,一切就緒后我們就用這個gPath對象傳遞給Region對象,當將這個Region對象賦給Form窗體的Region屬性后,窗體就具備了標注式提示窗口樣式的不規(guī)則外形了,部分代碼如下:

private void SetInfoWindowRegion()
{
 if (!this.IsHandleCreated)
  return;
 System.Drawing.Size windowSize = this.Size;
 Point[] ArrowPoints = new Point[3];
 Point topLeftPoint = Point.Empty;
 Point bottomRightPoint = (Point)windowSize;
 switch (this.GetArrowLocation)
 {
  case ArrowLocation.TopLeft:
   ……
  case ArrowLocation.TopRight:
   ……
  case ArrowLocation.BottomLeft:
   ……
  case ArrowLocation.BottomRight:
   ……
 }
 ……
 ……
 if ((this.GetArrowLocation == ArrowLocation.TopLeft) ||
(this.GetArrowLocation == ArrowLocation.TopRight))
 {
  gPath.AddArc(topLeftPoint.X, rectY2 - arcRadius, arcDia, arcDia, 90, 90);
  gPath.AddLine(topLeftPoint.X, rectY2, topLeftPoint.X, rectY1);
  gPath.AddArc(topLeftPoint.X, topLeftPoint.Y, arcDia, arcDia, 180, 90);
  gPath.AddLine(rectX1, topLeftPoint.Y, ArrowPoints[0].X, topLeftPoint.Y);
  gPath.AddLines(ArrowPoints);
  gPath.AddLine(ArrowPoints[2].X, topLeftPoint.Y, rectX2, topLeftPoint.Y);
  gPath.AddArc(rectX2 - arcRadius, topLeftPoint.Y, arcDia, arcDia, 270, 90);
  gPath.AddLine(bottomRightPoint.X, rectY1, bottomRightPoint.X, rectY2);
  gPath.AddArc(rectX2 - arcRadius, rectY2 - arcRadius, arcDia, arcDia, 0, 90);
  gPath.AddLine(rectX2, bottomRightPoint.Y, rectX1, bottomRightPoint.Y);
 }
 else
 {
  gPath.AddLine(rectX1, topLeftPoint.Y, rectX2, topLeftPoint.Y);
  gPath.AddArc(rectX2 - arcRadius, topLeftPoint.Y, arcDia, arcDia, 270, 90);
  gPath.AddLine(bottomRightPoint.X, rectY1, bottomRightPoint.X, rectY2);
  gPath.AddArc(rectX2 - arcRadius, rectY2 - arcRadius, arcDia, arcDia, 0, 90);
  gPath.AddLine(rectX2, bottomRightPoint.Y, ArrowPoints[0].X, bottomRightPoint.Y);
  gPath.AddLines(ArrowPoints);
  gPath.AddLine(ArrowPoints[2].X, bottomRightPoint.Y, rectX1, bottomRightPoint.Y);
  gPath.AddArc(topLeftPoint.X, rectY2 - arcRadius, arcDia, arcDia, 90, 90);
  gPath.AddLine(topLeftPoint.X, rectY2, topLeftPoint.X, rectY1);
  gPath.AddArc(topLeftPoint.X, topLeftPoint.Y, arcDia, arcDia, 180, 90);
 }
 gPath.CloseFigure();
 this.Region = new Region(this.gPath);
}

  ShowInfoWindow函數(shù)用來將提示窗口顯示出來,該函數(shù)需要將提示窗口附著的控件和需要顯示的文本傳遞過來。然后,AnchorPointFromControl根據(jù)控件的位置返回提示窗口的箭頭應該顯示的坐標,代碼如下:

public static Point AnchorPointFromControl(Control anchorControl)
{
 if (anchorControl == null)
 throw new ArgumentException();
 Point controlLocation = anchorControl.Location;
 System.Drawing.Size controlSize = anchorControl.Size;

 if (anchorControl.Parent != null)
  controlLocation = anchorControl.Parent.PointToScreen(controlLocation);
 return controlLocation + new Size(controlSize.Width / 2, controlSize.Height / 2);
}

  PointToScreen表明將工作區(qū)點的位置映射成屏幕坐標統(tǒng)一進行計算。上述代碼最后以行說明提示窗口的箭頭顯示在附著控件的中點。
  將提示窗口的背景顏色設置成Info

  我們發(fā)現(xiàn)這樣的外觀有點別扭,沒錯!因為提示窗口缺少黑色邊框!所以,還需要在窗體的OnPaint事件中添加代碼,如下:

protected override void OnPaint(PaintEventArgs e)
{
 Pen p = new Pen(Color.Black , 2);
 e.Graphics.DrawPath(p, gPath);
 base.OnPaint(e);
}
 
 
二、程序?qū)崿F(xiàn)

  啟動Visual Studio 2005,新建Visual C#的Windows應用程序項目,并取名為ShowInfoWindow,添加4個Button組件、1個Label組件、1個textBox組件和3個Panel組件,其中3個Button用來顯示標注式消息提示窗口并分別附著在三個組件之上,代碼如下:

……
private InfoWindow iw;
……
private void button1_Click(object sender, EventArgs e)
{
 iw = new InfoWindow();
 iw.ShowInfoWindow(label1, "關于標簽組件的提示說明。");
}
private void button3_Click(object sender, EventArgs e)
{
 iw = new InfoWindow();
 iw.ShowInfoWindow(button2, "關于按鈕組件的提示說明。");
}

private void button4_Click(object sender, EventArgs e)
{
 iw = new InfoWindow();
 iw.ShowInfoWindow(textBox1, "關于文本框組件的提示說明。");
}

  然后,我們在項目中添加新Windows窗體,取名為InfoWindow,將InfoWindow的BackColor設為Info,F(xiàn)ormBorderStyle設為None,將ShowIcon和ShowInTaskbar都設為False,在窗體上放置1個Label組件和1個Button組件,分別用來顯示消息內(nèi)容和關閉提示窗口的操作。具體實現(xiàn)請參見文章附帶的源碼,這里不再詳述。

  三、總結

  本文演示了標注式消息提示窗口的創(chuàng)建和顯示,利用GraphicsPath對象、Region對象以及屏幕坐標映射等方法有效的實現(xiàn)了提示窗口的外觀和樣式,提示窗口可以自動附著在相應控件之上,并且根據(jù)附著控件在屏幕上的位置自動調(diào)整提示窗口箭頭的位置和大小。演示程序在Windows XP SP2以及.Net 框架 2.0環(huán)境下運行通過。
http://www.survivalescaperooms.com/tanghuawei/archive/2006/10/16/530513.html


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 云龙县| 兴安盟| 长丰县| 怀集县| 五河县| 伊川县| 嘉定区| 彭阳县| 临颍县| 西藏| 抚顺市| 栾城县| 甘洛县| 城市| 上林县| 田东县| 大悟县| 江门市| 长垣县| 南皮县| 哈密市| 贵溪市| 同仁县| 桦南县| 盐边县| 蛟河市| 个旧市| 鄂伦春自治旗| 邛崃市| 平安县| 西林县| 六盘水市| 旌德县| 文安县| 康定县| 安远县| 万州区| 海城市| 冀州市| 高邑县| 杭锦旗|