winform消息提⽰框
摘⾃:
public partial class AlertForm : Form
{
/*
* 在类AlertForm定义的下⽅,
* 我们创建⽤于显⽰的字符串和其颜⾊的变量,
* 再定义⼏个Rectangle对象的变量⽤于放置标题、
* 提⽰内容以及可以拖动窗体的区域和关闭按钮的区域。
* 然后,我们需要保存窗体在浮动时的⾼度以便计算移动后的新⾼度,
* intervalValue变量⽤来确定窗体显⽰和隐藏的速度。
* 进⾏平台调⽤时我们需要提前定义好常量的值⽤来传递给函数,
* WM_NCLBUTTONDOWN和HT_CAPTION常量⽤于拖动窗体,
* 他们的值都保存在WinUr.h头⽂件中,
* 所对应的动态链接库名为:ur32.dll。
* 我们⽤到的Win32API为:SendMessage、ReleaCapture和ShowWindow,
* 通过使⽤DllImportAttribute可以导⼊相应的函数并在程序中重新进⾏定义,如下:
*/
public Bitmap BackgroundBitmap = null;
private string titleText;
private string contentText;
private Color normalTitleColor = Color.FromArgb(0, 0, 0);
private Font normalTitleFont = new Font("宋体", 12, FontStyle.Regular, GraphicsUnit.Pixel);
private Color normalContentColor = Color.FromArgb(0, 0, 0);
private Font normalContentFont = new Font("宋体", 12, FontStyle.Regular, GraphicsUnit.Pixel);
public Rectangle TitleRectangle;
public Rectangle TitlebarRectangle;
public Rectangle ContentRectangle;
public Rectangle CloBtnRectangle;
private Rectangle WorkAreaRectangle;
private int SavedTop;
private int currentTop = 1;
private int intervalValue = 2;
up clo
public const int WM_NCLBUTTONDOWN = 0x00A1; //消息:左键点击 winur.h
easyjet
public const int HT_CAPTION = 0x0002; //标题栏
[DllImportAttribute("ur32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); //发送消息 //winur.h 中有函数原型定义 [DllImportAttribute("ur32.dll")]
public static extern bool ReleaCapture(); //释放⿏标捕捉 winur.h
/*
* SendMessage向消息循环发送标题栏被按下的消息来模拟窗体的拖动,
* ShowWindow⽤来将特定句柄的窗体显⽰出来,
* 注意第⼆个参数nCmdShow,它表⽰窗体应该怎样显⽰出来,
* ⽽我们需要窗体不获得焦点显⽰出来,
* SW_SHOWNOACTIVATE可以满⾜我们要求,
* 继续在WinUr.h⽂件中搜索找到该常量对应的值为4,
* 于是我们就可以这样调⽤来显⽰窗体了:
*/
[DllImportAttribute("ur32.dll")] //winur.h
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
public int CurrentState=0; //0=hide 1=uptoshow 2=showing 3=downtohide
广州mba辅导班
private AlertForm()
{
InitializeComponent();
SetBackgroundBitmap(snnus.Properties.Resources.popup2, Color.FromArgb(255, 0, 255));
}
public static void Show(string ftitletext, string fcontenttext)
{
AlertForm alert = new AlertForm();
光棍歌
alert.ShowForm(ftitletext, fcontenttext);
}
/*
* 当窗体需要升起时将窗体的Top属性值不断减少,
* ⽽窗体回落时将Top属性值增加并超过屏幕的⾼度窗体就消失了,
* 虽然原理很简单但仍需精确控制。
* SetBackgroundBitmap函数⾸先将窗体背景图像保存到BackgroundBitmap变量中,
* 然后根据该位图图像轮廓和透明⾊创建Region,BitmapToRegion就⽤于完成Bitmap到Region的转换, * 程序再将这个Region付值给窗体的Region属性以完成不规则窗体的创建。
*/
protected void SetBackgroundBitmap(Image image, Color transparencyColor)
{
BackgroundBitmap = new Bitmap(image);
Width = BackgroundBitmap.Width;
Height = BackgroundBitmap.Height;
TitleRectangle = new Rectangle(2, 1, 70, 25);
TitlebarRectangle = new Rectangle(1, 1, Width, 25);
ContentRectangle = new Rectangle(1, 25, Width, Height - 25);
CloBtnRectangle = new Rectangle(Width - 25, 1, 25, 25);
Region = BitmapToRegion(BackgroundBitmap, transparencyColor);
}
protected 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.Dispo();
return region;
}
protected void DrawText(Graphics grfx)
{
if (titleText != null && titleText.Length != 0)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.NoWrap;
sf.Trimming = StringTrimming.EllipsisCharacter;
grfx.DrawString(titleText, normalTitleFont, new SolidBrush(normalTitleColor), TitleRectangle, sf);
}
if (contentText != null && contentText.Length != 0)
{
StringFormat sf = new StringFormat();
wftsf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
sf.Trimming = StringTrimming.Word;
grfx.DrawString(contentText, normalContentFont, new SolidBrush(normalContentColor), ContentRectangle, sf);
}
能飞英语网}
/*
* 通知窗体背景以及⽂字的绘制在重载的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);
}
/*
* CurrentState变量表⽰窗体的状态是显⽰中、停留中还是隐藏中,
* 两个计时器根据窗体不同状态对窗体的位置进⾏更改,
* 我们会使⽤SetBounds来执⾏该操作:
* this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height); */
protected void ShowForm(string ftitletext, string fcontenttext)
{
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;
ShowWindow(this.Handle, 4); //#define SW_SHOWNOACTIVATE 4
}
private void timer1_Tick(object nder, EventArgs e)
{
if (CurrentState == 1)
{
this.SetBounds(WorkAreaRectangle.Width - this.Width, WorkAreaRectangle.Height - currentTop, this.Width, this.Height); currentTop = currentTop + intervalValue;
if (this.Top <= WorkAreaRectangle.Height - this.Height)
{
timer1.Enabled = fal;
CurrentState = 2;
timer2.Enabled = true; //显⽰停留计时
currentTop = 1;
}
}
el if (CurrentState==3)
{
this.SetBounds(WorkAreaRectangle.Width - this.Width, SavedTop + currentTop, this.Width, this.Height);
currentTop = currentTop + intervalValue;
if (this.Top >= WorkAreaRectangle.Height)
{
timer1.Enabled = fal;
this.Clo();
CurrentState = 0;
currentTop = 1;honolulu
}
}
}
/*
上述代码⾸先返回窗体绘制表⾯的Graphics并保存在变量grfx中,
* 然后创建⼀个内存Graphics对象offScreenGraphics和内存位图对象offscreenBitmap,
* 将内存位图对象的引⽤付值给offScreenGraphics,
* 这样所有对offScreenGraphics的绘制操作也都同时作⽤于offscreenBitmap,
* 这时就将需要绘制到通知窗体表⾯的背景图像BackgroundBitmap绘制到内存的Graphics对象上,
* DrawText函数根据需要显⽰⽂字的⼤⼩和范围调⽤Graphics.DrawString将⽂字显⽰在窗体的特定区域。
* 最后,调⽤Graphics.DrawImage将内存中已经绘制完成的图像显⽰到通知窗体表⾯。
* 我们还需要捕获窗体的⿏标操作,有三个操作在这⾥进⾏,
* 1、处理拖动窗体操作,
* 2、处理通知窗体的关闭操作,
* 3、内容区域的单击操作。
* 三个操作都需要检测⿏标的当前位置与每个Rectangle区域的包含关系,
* 只要单击落在特定区域我们就进⾏相应的处理,代码如下
*/
private void TaskbarForm_MouDown(object nder, MouEventArgs e)
{
if (e.Button == MouButtons.Left)
{
if (TitlebarRectangle.Contains(e.Location)) //单击标题栏时拖动
{
ReleaCapture(); //释放⿏标捕捉
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); //发送左键点击的消息⾄该窗体(标题栏) }
if (CloBtnRectangle.Contains(e.Location)) //单击Clo按钮关闭
{
this.Clo();
currentTop = 1;
}
}
}
private void timer2_Tick(object nder, EventArgs e)
{
timer2.Enabled = fal;
CurrentState = 1;
currentTop = 1;
SavedTop = this.Top;
CurrentState = 3;
timer1.Enabled = true;
}
private void TaskbarForm_MouMove(object nder, MouEventArgs e)
{
if (ContentRectangle.Contains(e.Location))
{
Cursor = Cursors.Hand;
}
el
Cursor = Cursors.Default;
physics}
}