SendMessage向消息循環發送標題欄被按下的消息來模擬窗體的拖動,ShowWindow用來將特定句柄的窗體顯示出來,注意第二個參數nCmdShow,它表示窗體應該怎樣顯示出來,而我們需要窗體不獲得焦點顯示出來,SW_SHOWNOACTIVATE可以滿足我們要求,繼續在WinUser.h文件中搜索找到該常量對應的值為4,於是我們就可以這樣調用來顯示窗體了:
ShowWindow(this.Handle, 4);
我們創建了一個自定義函數ShowForm用來封裝上面的ShowWindow用來是顯示窗體,同時傳遞了所用到的幾個Rectangle矩形區域對象,最後調用ShowWindows函數將窗體顯示出來,代碼片段如下:
public void ShowForm(string ftitletext, string fcontenttext, Rectangle fRegionofFormTitle, Rectangle fRegionofFormTitlebar, Rectangle fRegionofFormContent, Rectangle fRegionofCloseBtn)
{
titleText = ftitletext;
contentText = fcontenttext;
WorkAreaRectangle = Screen.GetWorkingArea(WorkAreaRectangle);
this.Top = WorkAreaRectangle.Height + this.Height;
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowstate.Normal;
this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
CurrentState = 1;
timer1.Enabled = true;
TitleRectangle = fRegionofFormTitle;
TitlebarRectangle = fRegionofFormTitlebar;
ContentRectangle = fRegionofFormContent;
CloseBtnRectangle = fRegionofCloseBtn;
ShowWindow(this.Handle, 4); //#define SW_SHOWNOACTIVATE 4
}
CurrentState變量表示窗體的狀態是顯示中、停留中還是隱藏中,兩個計時器根據窗體不同狀態對窗體的位置進行更改,我們會使用SetBounds來執行該操作:
this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height);
當窗體需要升起時將窗體的Top屬性值不斷減少,而窗體回落時將Top屬性值增加並超過屏幕的高度窗體就消失了,雖然原理很簡單但仍需精確控制。
SetBackgroundBitmap函數首先將窗體背景圖像保存到BackgroundBitmap變量中,然後根據該位圖圖像輪廓和透明色創建Region,BitmapToRegion就用於完成Bitmap到Region的轉換,程序再將這個Region付值給窗體的Region屬性以完成不規則窗體的創建。
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;
}