一、.实现弹窗淡入淡出等效果即弹窗自动关闭
技术要点:
1.弹窗效果(淡入淡出,自下而上滑入)使用WIN API实现
2.弹出的窗体在一定时间后,自动关闭使用一个timer实现,弹窗开始是,打开timer,到设置时间后,触发timer关闭窗体
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test007
{
public partial class Message : Form
{
public Message()
{
InitializeComponent();
}
\[DllImport("user32")\]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
//下面是可用的常量,根据不同的动画效果声明自己需要的
private const int AW\_HOR\_POSITIVE = 0x0001;//自左向右显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志
private const int AW\_HOR\_NEGATIVE = 0x0002;//自右向左显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志
private const int AW\_VER\_POSITIVE = 0x0004;//自顶向下显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志
private const int AW\_VER\_NEGATIVE = 0x0008;//自下向上显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志该标志
private const int AW\_CENTER = 0x0010;//若使用了AW\_HIDE标志,则使窗口向内重叠;否则向外扩展
private const int AW\_HIDE = 0x10000;//隐藏窗口
private const int AW\_ACTIVE = 0x20000;//激活窗口,在使用了AW\_HIDE标志后不要使用这个标志
private const int AW\_SLIDE = 0x40000;//使用滑动类型动画效果,默认为滚动动画类型,当使用AW\_CENTER标志时,这个标志就被忽略
private const int AW\_BLEND = 0x80000;//使用淡入淡出效果
private void Message\_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
//弹窗
int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
this.Location = new Point(x, y);
//自下而上弹出窗体
AnimateWindow(this.Handle, 3000, AW_SLIDE | AW_ACTIVE | AW_VER_NEGATIVE);
}
private void timer1\_Tick(object sender, EventArgs e)
{
//关闭窗体,淡入淡出
AnimateWindow(this.Handle, 1000, AW\_BLEND | AW\_HIDE);this.Close();
timer1.Enabled = false;
this.Close();
this.Dispose();
}
}
}
二、连接数据库
技术要点:
1.连接数据库需要使用Oracle.ManagedDataAccess.dll,具体如何安装,请看前面的文章
2.使用App.config文件来存放数据库连接字符串,方便后面更改
3.OracleHelper辅助类
代码:
App.config
获取配置代码
public static string connstrSMTP = ConfigurationManager.AppSettings["ConnectionStringSMTP"];
OracleHelper.cs
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test007
{
public static class OracleHelper
{
public static string connstrSMTP = ConfigurationManager.AppSettings["ConnectionStringSMTP"];
public static string connstrTest = ConfigurationManager.AppSettings["ConnectionStringTest"];
/// <summary>
/// 执行数据库非查询操作,返回受影响的行数
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前操作影响的数据行数</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
OracleCommand cmd = new OracleCommand();
using (OracleConnection conn = new OracleConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 执行数据库事务非查询操作,返回受影响的行数
/// </summary>
/// <param name="transaction">数据库事务对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前事务操作影响的数据行数</returns>
public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 执行数据库非查询操作,返回受影响的行数
/// </summary>
/// <param name="connection">Oracle数据库连接对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前操作影响的数据行数</returns>
public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
if (connection == null)
throw new ArgumentNullException("当前数据库连接不存在");
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 执行数据库查询操作,返回OracleDataReader类型的内存结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的OracleDataReader类型的内存结果集</returns>
public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch
{
cmd.Dispose();
conn.Close();
throw;
}
}
/// <summary>
/// 执行数据库查询操作,返回DataSet类型的结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的DataSet类型的结果集</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
DataSet ds = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = cmd;
ds = new DataSet();
adapter.Fill(ds);
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return ds;
}
/// <summary>
/// 执行数据库查询操作,返回DataTable类型的结果集
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的DataTable类型的结果集</returns>
public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
DataTable dt = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = cmd;
dt = new DataTable();
adapter.Fill(dt);
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return dt;
}
/// <summary>
/// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值
/// </summary>
/// <param name="connectionString">数据库连接字符串</param>
/// <param name="cmdType">命令的类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
object result = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 执行数据库事务查询操作,返回结果集中位于第一行第一列的Object类型的值
/// </summary>
/// <param name="trans">一个已存在的数据库事务对象</param>
/// <param name="commandType">命令类型</param>
/// <param name="commandText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前事务查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
public static object ExecuteScalar(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
if (trans == null)
throw new ArgumentNullException("当前数据库事务不存在");
OracleConnection conn = trans.Connection;
if (conn == null)
throw new ArgumentException("当前事务所在的数据库连接不存在");
OracleCommand cmd = new OracleCommand();
object result = null;
try
{
PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
trans.Dispose();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 执行数据库查询操作,返回结果集中位于第一行第一列的Object类型的值
/// </summary>
/// <param name="conn">数据库连接对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
/// <returns>当前查询操作返回的结果集中位于第一行第一列的Object类型的值</returns>
public static object ExecuteScalar(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter\[\] cmdParms)
{
if (conn == null) throw new ArgumentException("当前数据库连接不存在");
OracleCommand cmd = new OracleCommand();
object result = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 执行数据库命令前的准备工作
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">数据库连接对象</param>
/// <param name="trans">事务对象</param>
/// <param name="cmdType">Command类型</param>
/// <param name="cmdText">Oracle存储过程名称或PL/SQL命令</param>
/// <param name="cmdParms">命令参数集合</param>
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter\[\] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 将.NET日期时间类型转化为Oracle兼容的日期时间格式字符串
/// </summary>
/// <param name="date">.NET日期时间类型对象</param>
/// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO\_DATE('2007-12-1','YYYY-MM-DD'))</returns>
public static string GetOracleDateFormat(DateTime date)
{
return "TO\_DATE('" + date.ToString("yyyy-M-dd") + "','YYYY-MM-DD')";
}
/// <summary>
/// 将.NET日期时间类型转化为Oracle兼容的日期格式字符串
/// </summary>
/// <param name="date">.NET日期时间类型对象</param>
/// <param name="format">Oracle日期时间类型格式化限定符</param>
/// <returns>Oracle兼容的日期时间格式字符串(如该字符串:TO\_DATE('2007-12-1','YYYY-MM-DD'))</returns>
public static string GetOracleDateFormat(DateTime date, string format)
{
if (format == null || format.Trim() == "") format = "YYYY-MM-DD";
return "TO\_DATE('" + date.ToString("yyyy-M-dd") + "','" + format + "')";
}
/// <summary>
/// 将指定的关键字处理为模糊查询时的合法参数值
/// </summary>
/// <param name="source">待处理的查询关键字</param>
/// <returns>过滤后的查询关键字</returns>
public static string HandleLikeKey(string source)
{
if (source == null || source.Trim() == "") return null;
source = source.Replace("\[", "\[\]\]");
source = source.Replace("\_", "\[\_\]");
source = source.Replace("%", "\[%\]");
return ("%" + source + "%");
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="connection">SqlServer数据库连接对象</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlDataReader对象</returns>
public static OracleDataReader RunStoredProcedure(OracleConnection connection, string storedProcName, IDataParameter\[\] parameters)
{
OracleDataReader returnReader = null;
connection.Open();
OracleCommand command = BuildSqlCommand(connection, storedProcName, parameters);
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
/// <summary>
/// 构建SqlCommand对象
/// </summary>
/// <param name="connection">数据库连接</param>
/// <param name="storedProcName">存储过程名</param>
/// <param name="parameters">存储过程参数</param>
/// <returns>SqlCommand</returns>
private static OracleCommand BuildSqlCommand(OracleConnection connection, string storedProcName, IDataParameter\[\] parameters)
{
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
}
}
三、最小化到托盘
参考:https://www.cnblogs.com/monkey6/p/13188488.html
四、弹窗在所有窗体最前
技术要点:
使用窗体的TopMost属性
代码:
private void Message_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.TopMost = false;
}
五、弹窗带声音报警
技术要点:
使用SoundPlayer类
代码:
private void Message_Load(object sender, EventArgs e)
{
//弹窗后播放音乐
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"./alarm1.wav";
player.Load();
player.Play();
}
六、弹出的窗体不获得鼠标焦点,影响用户正常工作
技术要点:重写窗体方法
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test007
{
public partial class Message : Form
{
public Message()
{
InitializeComponent();
}
//弹窗后不获取鼠标焦点,避免打扰用户的工作
const int WS_EX_NOACTIVATE = 0x08000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_NOACTIVATE;
return cp;
}
}
}
}
七、开机自启动
WIN+R快捷键打开运行对话框,输入shell:startup进入开机自启动文件夹,将程式的快捷方式放入其中即可
整体控件,组件及代码
主窗体:
主窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test007
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3\_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否确认选择"+comboBox1.Text+"XX?", "XX选择", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
//隐藏窗体
this.Visible = false;
//图标显示在托盘区
notifyIcon1.Visible = true;
//托盘区气泡提醒
notifyIcon1.ShowBalloonTip(2000, "提示", "右键图标退出程式", ToolTipIcon.Info);
if (!timer1.Enabled)
{
timer1.Enabled = true;
timer1.Start();
}
}
}
private void 退出ToolStripMenuItem\_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
// 关闭所有的线程
this.Dispose();
this.Close();
}
}
private void timer1\_Tick(object sender, EventArgs e)
{
string sql = "";
if (comboBox1.Text == "XX")
sql = "SELECT TITLE,MSG FROM MONITOR";
else if (comboBox1.Text == "XX")
sql = "SELECT TITLE,MSG FROM MONITOR";
else
sql = "SELECT TITLE,MSG FROM MONITOR";
DataTable dt = OracleHelper.ExecuteDataTable(OracleHelper.connstrSMTP, CommandType.Text,sql);
if(dt.Rows.Count > 0)
{
Message msg = new Message(dt);
msg.Show();
}
}
private void Form1\_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 0;
}
}
}
弹窗窗体(Message窗体)
弹窗窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test007
{
public partial class Message : Form
{
public Message(DataTable warningMsg)
{
InitializeComponent();
this.warningMsg = warningMsg;
}
private DataTable warningMsg;
#region
//弹窗后不获取鼠标焦点,避免打扰用户的工作
const int WS\_EX\_NOACTIVATE = 0x08000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS\_EX\_NOACTIVATE;
return cp;
}
}
#endregion
\[DllImport("user32")\]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
//下面是可用的常量,根据不同的动画效果声明自己需要的
private const int AW\_HOR\_POSITIVE = 0x0001;//自左向右显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志
private const int AW\_HOR\_NEGATIVE = 0x0002;//自右向左显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志
private const int AW\_VER\_POSITIVE = 0x0004;//自顶向下显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志
private const int AW\_VER\_NEGATIVE = 0x0008;//自下向上显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW\_CENTER标志时忽略该标志该标志
private const int AW\_CENTER = 0x0010;//若使用了AW\_HIDE标志,则使窗口向内重叠;否则向外扩展
private const int AW\_HIDE = 0x10000;//隐藏窗口
private const int AW\_ACTIVE = 0x20000;//激活窗口,在使用了AW\_HIDE标志后不要使用这个标志
private const int AW\_SLIDE = 0x40000;//使用滑动类型动画效果,默认为滚动动画类型,当使用AW\_CENTER标志时,这个标志就被忽略
private const int AW\_BLEND = 0x80000;//使用淡入淡出效果
private void Message\_Load(object sender, EventArgs e)
{
//弹窗时在所有窗体之前
this.TopMost = true;
//弹窗后播放音乐
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"./alarm1.wav";
player.Load();
player.Play();
this.dataGridView1.DataSource = warningMsg;
timer1.Enabled = true;
//弹窗
int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
this.Location = new Point(x, y);
//自下而上弹出窗体
AnimateWindow(this.Handle, 3000, AW\_SLIDE | AW\_ACTIVE | AW\_VER\_NEGATIVE);
//弹窗后允许其他窗体在前
this.TopMost = false;
}
private void timer1\_Tick(object sender, EventArgs e)
{
//关闭窗体,淡入淡出
AnimateWindow(this.Handle, 1000, AW\_BLEND | AW\_HIDE);
timer1.Enabled = false;
this.Close();
this.Dispose();
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章