C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示
来源:互联网 发布:免费网络推广渠道 编辑:程序博客网 时间:2024/05/22 04:40
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); } } }}
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); else reg.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; } else textBox2HasText = 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; } else textBox1HasText = 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(); } }}
阅读全文
0 0
- C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示
- c# HttpWebRequest post和get请求http
- c#中点击最小化时最小化到托盘后台运行
- c#中点击最小化时 最小化到托盘 后台运行
- c# wpf单实例运行并激活前实例
- C# 托盘程序 实例 双击显示窗体,最小化到托盘
- C# 托盘程序 实例 双击显示窗体,最小化到托盘
- C# 限制MDI窗口只能调用单实例子窗口
- C#请求网页(Get、Post)
- C#请求网页(Get、Post)
- 限制应用程序运行一次并激活已经运行的程序
- C# 单实例运行
- c# 单实例运行
- C#简单代码实现窗体最小化到托盘运行
- C# WinForm 程序单实例运行,再次运行时激活前一个实例
- 【转】C# WinForm 程序单实例运行,再次运行时激活前一个实例
- C# 保存好运行窗口
- HTTP get和post请求
- 机器学习第1章 : 目录
- jQuery-基本过滤选择器
- 2017-09-19 LeetCode_030 Substring with Concatenation of All Words
- 位运算学习笔记
- Unity Android平台下插件/SDK开发通用流程
- C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示
- UDP使用connect系统调用
- SDK之动态链接库开发—基本概念
- 关于Tomcat启动项目时,控制台抛出各种异常,误以为项目错误
- Tomcat优化参数
- swift4.0中UIView的frame的扩展封装
- HTTP协商缓存实践
- mysql使用in函数的一个小问题
- postman使用注意事项