C#源码功能示例:刷新网页、最小化托盘、http get和post请求、配置保存、版权时间限制、定时调用、单实例运行,如果已经运行则激活窗口到最前显示等。

单实例运行激活窗口 调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;namespace HTMLWindowsFormsApplication3
{static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);SoftHelper.SoftSingle<Form1>(); //单实例运行,已运行则激活窗口// Application.Run(new Form1());//bool createdNew; //Mutex instance= new Mutex(true, "互斥名(保证在本机中唯一)", out createdNew);//if(createdNew)//{//    Application.EnableVisualStyles();//    Application.SetCompatibleTextRenderingDefault(false);//    Application.Run(new Form1());//    instance.ReleaseMutex();//}//else//{//    MessageBox.Show("已经启动了一个程序,请先退出!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);//    Application.Exit();//}}}
}

单实例运行,如果已经运行则激活窗口到最前显示 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace HTMLWindowsFormsApplication3
{public class SoftHelper{///<summary>/// 该函数设置由不同线程产生的窗口的显示状态/// </summary>/// <param name="hWnd">窗口句柄</param>/// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分</param>/// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零</returns>[DllImport("User32.dll")]private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);/// <summary>///  该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。///  系统给创建前台窗口的线程分配的权限稍高于其他线程。 /// </summary>/// <param name="hWnd">将被激活并被调入前台的窗口句柄</param>/// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零</returns>[DllImport("User32.dll")]private static extern bool SetForegroundWindow(IntPtr hWnd);private const int SW_SHOWNOMAL = 1;private static void HandleRunningInstance(Process instance){ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOMAL);//显示SetForegroundWindow(instance.MainWindowHandle);//当到最前端}private static Process RuningInstance(){Process currentProcess = Process.GetCurrentProcess();Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessName);foreach (Process process in Processes){if (process.Id != currentProcess.Id){return process;}}return null;}/// <summary>/// 程序以单例运行 /// </summary>public static void SoftSingle<T>() where T : Form, new(){Process process = RuningInstance();if (process == null){var mainForm = new T();Application.Run(mainForm);}else{HandleRunningInstance(process);}}}
}

C#源码刷新网页 最小化托盘http get和post请求配置保存 版权时间限制定时调用

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 Microsoft.Win32;
using System.Net;
using System.IO;namespace HTMLWindowsFormsApplication3
{public partial class Form1 : Form{System.Timers.Timer t;long num=0;Boolean textBox1HasText = false;//判断输入框是否有文本Boolean textBox2HasText = false;//判断输入框是否有文本public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){textBox1.Text= Settings1.Default.url;textBox2.Text = Settings1.Default.num;label1.Text = "自动同步" + num + "次";// button1.Focus();textBox1_Leave(null, null);textBox2_Leave(null, null);// textBox1.Text = "www.baidu.com";// webBrowser1.Navigate(textBox1.Text.ToString());SetAutoRun(Application.ExecutablePath, true);StartRefresh();// this.Hide();this.WindowState = FormWindowState.Minimized;//   notifyIcon1.Visible = false;//  this.ShowInTaskbar = false;// limited();}public void StartRefresh(){int time = 1;try{time = int.Parse(textBox2.Text.ToString());}catch { }t = new System.Timers.Timer(1000 * time);//实例化Timer类,设置间隔时间为10000毫秒;t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;            }private void limited(){DateTime dt1 = DateTime.Parse("2017-09-20");if (DateTime.Compare(dt1, DateTime.Now) < 0){MessageBox.Show("试用版已经到期,请联系小黄人软件QQ345139427");Application.Exit(); return;}}/// <summary>/// 设置应用程序开机自动运行/// </summary>/// <param name="fileName">应用程序的文件名</param>  /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>/// <exception cref="System.Exception">设置不成功时抛出异常</exception>public static void SetAutoRun(string fileName, bool isAutoRun)  //SetAutoRun(Application.ExecutablePath,true);{RegistryKey reg = null;try{if (!System.IO.File.Exists(fileName))throw new Exception("该文件不存在!");String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);if (reg == null)reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");if (isAutoRun)reg.SetValue(name, fileName);elsereg.SetValue(name, false);}catch (Exception ex){throw new Exception(ex.ToString());}finally{if (reg != null)reg.Close();}}//textBox2获得焦点private void textBox2_Enter(object sender, EventArgs e){if (textBox2HasText == false)textBox2.Text = "";textBox2.ForeColor = Color.Black;}//textBox2失去焦点private void textBox2_Leave(object sender, EventArgs e){if (textBox2.Text == ""){textBox2.Text = "多少秒刷新1次?";textBox2.ForeColor = Color.LightGray;textBox2HasText = false;}elsetextBox2HasText = true;}//textBox1获得焦点private void textBox1_Enter(object sender, EventArgs e){if (textBox1HasText == false)textBox1.Text = "";textBox1.ForeColor = Color.Black;}//textBox1失去焦点private void textBox1_Leave(object sender, EventArgs e){if (textBox1.Text == ""){textBox1.Text = "需要刷新的网址";textBox1.ForeColor = Color.LightGray;textBox1HasText = false;}elsetextBox1HasText = true;}private void Form1_SizeChanged(object sender, EventArgs e){if (this.WindowState == FormWindowState.Minimized) //判断是否最小化{this.ShowInTaskbar = false; //不显示在系统任务栏notifyIcon1.Visible = true; //托盘图标可见}}private void notifyIcon1_DoubleClick(object sender, EventArgs e){if (this.WindowState == FormWindowState.Minimized){this.Show();this.WindowState = FormWindowState.Normal;//   notifyIcon1.Visible = false;this.ShowInTaskbar = true;}}private void checkBox1_CheckedChanged(object sender, EventArgs e){if (checkBox1.Checked) //设置{SetAutoRun(Application.ExecutablePath, true);}else{SetAutoRun(Application.ExecutablePath, false);}}private void button1_Click(object sender, EventArgs e){Settings1.Default.url = textBox1.Text.ToString();Settings1.Default.num=textBox2.Text.ToString();Settings1.Default.Save();int time = 1;try{time = int.Parse(textBox2.Text.ToString());}catch { }t.Interval=time*1000;}public void theout(object source, System.Timers.ElapsedEventArgs e){//MessageBox.Show("OK!");// string str=HttpGet(textBox1.Text.ToString(),"");webBrowser1.Navigate(textBox1.Text.ToString());num++;this.Invoke((EventHandler)(delegate{label1.Text = "自动同步" + num + "次";}));}CookieContainer cookie = new CookieContainer();private string HttpPost(string Url, string postDataStr){HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);request.Method = "POST";request.ContentType = "application/x-www-form-urlencoded";request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);request.CookieContainer = cookie;Stream myRequestStream = request.GetRequestStream();StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));myStreamWriter.Write(postDataStr);myStreamWriter.Close();HttpWebResponse response = (HttpWebResponse)request.GetResponse();response.Cookies = cookie.GetCookies(response.ResponseUri);Stream myResponseStream = response.GetResponseStream();StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));string retString = myStreamReader.ReadToEnd();myStreamReader.Close();myResponseStream.Close();return retString;}public string HttpGet(string Url, string postDataStr){HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);request.Method = "GET";request.ContentType = "text/html;charset=UTF-8";HttpWebResponse response = (HttpWebResponse)request.GetResponse();Stream myResponseStream = response.GetResponseStream();StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));string retString = myStreamReader.ReadToEnd();myStreamReader.Close();myResponseStream.Close();return retString;}private void Form1_FormClosing(object sender, FormClosingEventArgs e){this.WindowState = FormWindowState.Minimized;e.Cancel = true;     }private void 退出ToolStripMenuItem_Click(object sender, EventArgs e){Settings1.Default.url = textBox1.Text.ToString();Settings1.Default.num = textBox2.Text.ToString();Settings1.Default.Save();// 关闭所有的线程this.Dispose();this.Close();}}
}

C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示相关推荐

  1. win7系统计算机无最小化,win7纯净版系统任务栏无法显示网页最小化窗口怎么办...

    最近有位用户说win7纯净版任务栏无法显示网页最小化窗口,一般情况下,我们在使用浏览器完都会直接点击窗口最小化,网页窗口最小化到任务栏上.要使用的时候只要单击就可以将任务栏浏览器恢复到最大化,以方便网 ...

  2. win7计算机窗口无法最小化,win7系统任务栏无法显示网页最小化窗口的解决方法...

    很多小伙伴都遇到过win7系统任务栏无法显示网页最小化窗口的困惑吧,一些朋友看过网上零散的win7系统任务栏无法显示网页最小化窗口的处理方法,并没有完完全全明白win7系统任务栏无法显示网页最小化窗口 ...

  3. PHP在线ps照片图片处理网站源码 photoshop网页版

    介绍: PHP在线ps照片图片处理网站源码 photoshop网页版,一个专业的在线ps照片处理软件功能与photoshop一样,比较精简些,绿色免安装直接在您的浏览器上用它修正,调整和美化您的图像. ...

  4. 抖音程序员HTML相册,快手抖音程序员表白女朋友3D立体相册源码html网页相册代码...

    前几天分享了一套源码,今天又为大家带来一套类似的源码,希望大家喜欢! 快手抖音很火的程序员女朋友3D立体相册源码html网页相册代码,经测试在IE8浏览器下无法预览,建议使用支持HTML5与css3效 ...

  5. PHP在线照片图片处理PS网站程序源码photoshop网页版

    源码介绍: PHP在线ps照片图片处理网站源码 photoshop网页版,一个专业的在线ps照片处理软件功能与photoshop一样,比较精简些,绿色免安装直接在您的浏览器上用它修正,调整和美化您的图 ...

  6. RocketMQ源码(十七)—Broker处理DefaultMQPushConsumer发起的拉取消息请求源码

    转载来源: RocketMQ源码(19)-Broker处理DefaultMQPushConsumer发起的拉取消息请求源码[一万字]_刘Java的博客-CSDN博客 此前我们学习了RocketMQ源码 ...

  7. mysql data文件夹恢复_【专注】Zabbix源码安装教程—步骤详解(2)安装并配置mysql...

    四.安装并配置mysql(1) 解压mysql-5.7.26.tar.gz与boost_1_59_0.tar.gz #tar -xvf mysql-5.7.26.tar.gz #tar -xvf bo ...

  8. Soul网关源码阅读番外篇(一) HTTP参数请求错误

    Soul网关源码阅读番外篇(一) HTTP参数请求错误 共同作者:石立 萧 * 简介     在Soul网关2.2.1版本源码阅读中,遇到了HTTP请求加上参数返回404的错误,此篇文章基于此进行探索 ...

  9. 网页设计个人主页源码_WebSSH - 网页上的SSH终端

    不少的云服务器的网页后台就能登录服务器,并可以在浏览器上进入命令行交互.能不能在自己也部署一个呢?能不能配置一个更加符合自身需求的网页 SSH 终端呢?或许可以以此做一个更好的运维管理平台?来看看 P ...

  10. webstack响应式网站导航html源码kyuan 本地静态化版

    介绍: webstack响应式网站导航html源码 安装方法:直接上传 一言.和风天气的api建议大家自己注册换成自己的,每个注册的人有每日免费使用次数, 自带的一起用可能最后都显示不出来了. 以上提 ...

最新文章

  1. Windows Azure Storage (25) Azure Append Blob
  2. 拿到腾讯字节快手 offer 后,他的 LeetCode 刷题经验在 GitHub 火了!
  3. java读avro的流_0016-Avro序列化反序列化和Spark读取Avro数据
  4. 7、计算机图形学——图形管线渲染与纹理映射
  5. “产教融合新范式,校企聚力新实践”——2018杭州云栖大会大学合作专场论坛成功举办...
  6. Ubuntu 9.10下Nvidia官方最新190.42显卡驱动安装
  7. 基于UDP客户端服务器的编程模型-linux网络编程
  8. Eclipse中的codetemplates.xml
  9. Python——数组(列表)的基本操作
  10. QT自定义控件(电池)
  11. SpaceClaim功能解析与应用介绍
  12. 小米8 青春版root时无法检测到手机
  13. 全国哀悼日,英来网停站一天。
  14. 16个值得个人站长做的广告联盟[转自cnzz]
  15. 【深度好文】Python图像处理之物体标识与面积测量
  16. 4K电视与4K显示器区别?对比测试!
  17. Apollo + Springboot 整合(多环境版)
  18. JavaFX鼠标移入后改变样式
  19. BZOJ刷题记录---提高组难度
  20. Arduino循迹小车教程四----代码篇

热门文章

  1. C#中的控件Binding
  2. 昆明计算机设计学院官网,文山高中考不上有什么出路
  3. 数介牵手亿阳,ALEIYE深入运营商大数据
  4. 谷歌gmail注册入口_如何阻止Gmail将事件添加到Google日历
  5. javascript 域名合法性检测
  6. c语言课程设计作业心得体会,【c语言课程设计心得体会】 c语言课程设计报告总结...
  7. MCAL配置-Cdd_Ipc
  8. 网络空间安全--密码学重点(适合提前自学的宝宝)
  9. 读书笔记:《遇见未知的自己》
  10. 统计文件中元音字母的数量