Winform弹出类似"QQ"新闻_广告

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Map
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Win32
        {
            public const Int32 AW_HOR_POSITIVE = 0x00000001;//自左向右显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志 
            public const Int32 AW_HOR_NEGATIVE = 0x00000002;//自右向左显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志
            public const Int32 AW_VER_POSITIVE = 0x00000004;//自顶向下显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志  
            public const Int32 AW_VER_NEGATIVE = 0x00000008;//自下向上显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志
            public const Int32 AW_CENTER = 0x00000010;//若使用了AW_HIDE标志,则使窗口向内重叠;否则向外扩展
            public const Int32 AW_HIDE = 0x00010000;//隐藏窗口
            public const Int32 AW_ACTIVATE = 0x00020000;//激活窗口,在使用了AW_HIDE标志后不要使用这个标志 
            public const Int32 AW_SLIDE = 0x00040000;//使用滑动类型动画效果,默认为滚动动画类型,当使用AW_CENTER标志时,这个标志就被忽略 
            public const Int32 AW_BLEND = 0x00080000;// 使用淡入淡出效果
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool AnimateWindow(
                IntPtr hwnd,//  Windows的句柄  
                int dwTime, //动画的持续时间, 数值越大动画效果的时间越长
                int dwFlags //动画类型组合 
            );
            [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern bool SetWindowPos(
                IntPtr hwnd,
                int hWndInsertAfter,
                int x,//横坐标
                int y,//纵坐标
                int cx,//屏幕高度
                int cy,//屏幕宽度
                int wFlags//动画类型组合
            );
        }
        private void Form1_Load(object sender, EventArgs e)
        {   
            int x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - this.Width - 10;
            int y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - this.Height - 10;
            this.SetDesktopLocation(x, y);//窗体弹出的位置
            Win32.SetWindowPos(this.Handle, 100, Screen.PrimaryScreen.Bounds.Width - this.Width, Screen.PrimaryScreen.Bounds.Height - this.Height - 30,50,50,5);//调用WindowsAPI
            Win32.AnimateWindow(this.Handle, 1000, Win32.AW_VER_NEGATIVE);//调用WindowsAPI        
        }
    }
}
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 弹出广告
{
    ///
    /// 枚举,描述消息窗口加载的形式
    ///
    public enum LoadMode
    {
        ///
        /// 警告
        ///
        Warning,
       
        ///
        /// 错误
        ///
        Error,
        ///
        /// 提示
        ///
        Prompt
    }
    ///
    /// 消息提示窗口
    ///
    public partial class FormMessageBox : Form
    {
        ///
        /// 构造方法
        ///
        public FormMessageBox()
        {
            InitializeComponent();
        }

        #region ***********************字 段***********************
        ///
        /// 窗体加载模式
        ///
        private static LoadMode FormMode = LoadMode.Prompt;
        ///
        /// 显示的消息正文
        ///
        private static string ShowMessage = null;
        ///
        /// 关闭窗口的定时器
        ///
        private Timer Timer_Close = new Timer();

        [DllImportAttribute("user32.dll")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);   // 该函数可以实现窗体的动画效果
        public const Int32 AW_HOR_POSITIVE = 0x00000001;   // 自左向右显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时,该标志将被忽略
        public const Int32 AW_HOR_NEGATIVE = 0x00000002;   // 自右向左显示窗口。当使用了 AW_CENTER 标志时该标志被忽略
        public const Int32 AW_VER_POSITIVE = 0x00000004;   // 自顶向下显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时,该标志将被忽略
        public const Int32 AW_VER_NEGATIVE = 0x00000008;   // 自下向上显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时,该标志将被忽略
        public const Int32 AW_CENTER = 0x00000010;         // 若使用了AW_HIDE标志,则使窗口向内重叠;若未使用AW_HIDE标志,则使窗口向外扩展
        public const Int32 AW_HIDE = 0x00010000;           // 隐藏窗口,缺省则显示窗口
        public const Int32 AW_ACTIVATE = 0x00020000;       // 激活窗口。在使用了AW_HIDE标志后不要使用这个标志
        public const Int32 AW_SLIDE = 0x00040000;          // 使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时,这个标志就被忽略
        public const Int32 AW_BLEND = 0x00080000;          // 使用淡入效果。只有当hWnd为顶层窗口的时候才可以使用此标志
        #endregion*************************************************

        #region ***********************方 法***********************
        ///
        /// 构造方法
        ///
        /// 加载模式
        /// 消息正文
        public static void Show(LoadMode loadMode, string message)
        {
            FormMode = loadMode;
            ShowMessage = message;
            FormMessageBox box = new FormMessageBox();
            box.Show();
        }
 
        #endregion*************************************************

        #region ***********************事 件***********************
        ///
        /// 窗体加载事件
        ///
        ///
        ///
        private void FormMessageBox_Load(object sender, EventArgs e)
        {
            this.lblTitle.Text = "提示";
            if (FormMode == LoadMode.Error)
            {
                this.lblTitle.Text = "错误";
                this.plShow.BackgroundImage = global::弹出广告.Properties.Resources.error;    // 更换背景
            }
            else if (FormMode == LoadMode.Warning)
            {
                this.lblTitle.Text = "警告";
                this.plShow.BackgroundImage = global::弹出广告.Properties.Resources.warning;  // 更换背景
            }
            else
            {
                this.plShow.BackgroundImage = global::弹出广告.Properties.Resources.Prompt;   // 更换背景
            }
            this.lblMessage.Text = ShowMessage;
            int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            int top = height - 35 - this.Height;
            int left = width - this.Width - 5;
            this.Top = top;
            this.Left = left;
            this.TopMost = true;
            AnimateWindow(this.Handle, 500, AW_SLIDE + AW_VER_NEGATIVE);//开始窗体动画
            this.ShowInTaskbar = false;
            Timer_Close.Interval = 10000;
            Timer_Close.Tick += new EventHandler(Timer_Close_Tick);
            Timer_Close.Start();
        }
        ///
        /// 关闭窗口的定时器响应事件
        ///
        ///
        ///
        private void Timer_Close_Tick(object sender, EventArgs e)
        {
            Timer_Close.Stop();
            this.Close();
        }
        ///
        /// 窗口已经关闭
        ///
        ///
        ///
        private void FormMessageBox_FormClosed(object sender, FormClosedEventArgs e)
        {
            AnimateWindow(this.Handle, 500, AW_SLIDE + AW_VER_POSITIVE + AW_HIDE);
            Timer_Close.Stop();
            Timer_Close.Dispose();
        }
        ///
        /// 鼠标移动在消息框上
        ///
        ///
        ///
        private void plShow_MouseMove(object sender, MouseEventArgs e)
        {
            this.Timer_Close.Stop();
        }
        ///
        /// 鼠标移动离开消息框上
        ///
        ///
        ///
        private void plShow_MouseLeave(object sender, EventArgs e)
        {
            this.Timer_Close.Start();
        }
        #endregion*************************************************
       

    }
}

 

 

posted @ 2016-07-15 10:05  动灵  阅读(357)  评论(0编辑  收藏  举报