public void SetBackgroundBitmap(Image image, Color transparencyColor) { BackgroundBitmap = new Bitmap(image); Width = BackgroundBitmap.Width; Height = BackgroundBitmap.Height; Region = BitmapToRegion(BackgroundBitmap, transparencyColor); } public Region BitmapToRegion(Bitmap bitmap, Color transparencyColor) { if (bitmap == null) throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!"); int height = bitmap.Height; int width = bitmap.Width; GraphicsPath path = new GraphicsPath(); for (int j = 0; j < height; j++) for (int i = 0; i < width; i++) { if (bitmap.GetPixel(i, j) == transparencyColor) continue; int x0 = i; while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor)) i++; path.AddRectangle(new Rectangle(x0, j, i - x0, 1)); } Region region = new Region(path); path.Dispose(); return region; } |
通知窗体背景以及文字的绘制在重载的OnPaintBackground方法中完成,而且利用了双重缓冲区技术来进行绘制操作,代码如下:
protected override void OnPaintBackground(PaintEventArgs e) { Graphics grfx = e.Graphics; grfx.PageUnit = GraphicsUnit.Pixel; Graphics offScreenGraphics; Bitmap offscreenBitmap; offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height); offScreenGraphics = Graphics.FromImage(offscreenBitmap); if (BackgroundBitmap != null) { offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, BackgroundBitmap.Width, BackgroundBitmap.Height); } DrawText(offScreenGraphics); grfx.DrawImage(offscreenBitmap, 0, 0); }
|
上述代码首先返回窗体绘制表面的Graphics并保存在变量grfx中,然后创建一个内存Graphics对象offScreenGraphics和内存位图对象offscreenBitmap,将内存位图对象的引用付值给offScreenGraphics,这样所有对offScreenGraphics的绘制操作也都同时作用于offscreenBitmap,这时就将需要绘制到通知窗体表面的背景图像BackgroundBitmap绘制到内存的Graphics对象上,DrawText函数根据需要显示文字的大小和范围调用Graphics.DrawString将文字显示在窗体的特定区域。最后,调用Graphics.DrawImage将内存中已经绘制完成的图像显示到通知窗体表面。
我们还需要捕获窗体的鼠标操作,有三个操作在这里进行,1、处理拖动窗体操作,2、处理通知窗体的关闭操作,3、内容区域的单击操作。三个操作都需要检测鼠标的当前位置与每个Rectangle区域的包含关系,只要单击落在特定区域我们就进行相应的处理,代码如下:
private void TaskbarForm_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (TitlebarRectangle.Contains(e.Location)) //单击标题栏时拖动 { ReleaseCapture(); //释放鼠标捕捉 SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); //发送左键点击的消息至该窗体(标题栏) } if (CloseBtnRectangle.Contains(e.Location)) //单击Close按钮关闭 { this.Hide(); currentTop = 1; } if (ContentRectangle.Contains(e.Location )) //单击内容区域 { System.Diagnostics.Process.Start("http://www.Rithia.com"); } } }
|
结论
该程序可以很好的进行通知窗体的显示、停留和隐藏操作,并且具备简单的换肤机制,在利用了双重缓冲区绘图技术后,可以保证窗体的绘制平滑且没有闪烁。
上一页 [1] [2] [3]

【责编:John】