博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#源码刷新网页 最小化托盘http get和post请求配置保存版权时间限制定时调用 单实例运行,如果已经运行则激活窗口到最前显示
阅读量:2389 次
发布时间:2019-05-10

本文共 11747 字,大约阅读时间需要 39 分钟。

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    {        ///         /// 应用程序的主入口点。        ///         [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            SoftHelper.SoftSingle
(); //单实例运行,已运行则激活窗口 // 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    {        ///        /// 该函数设置由不同线程产生的窗口的显示状态        ///         /// 窗口句柄        /// 指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分        /// 
如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零
[DllImport("User32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。 /// 系统给创建前台窗口的线程分配的权限稍高于其他线程。 /// /// 将被激活并被调入前台的窗口句柄 ///
如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零
[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; } /// /// 程序以单例运行 /// public static void SoftSingle
() 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;            }        }        ///         /// 设置应用程序开机自动运行        ///         /// 应用程序的文件名          /// 是否自动运行,为false时,取消自动运行        /// 
设置不成功时抛出异常
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(); } }}
你可能感兴趣的文章
推荐 OWASP - Transport Layer Protection Cheat Sheet
查看>>
AutoNessus v1.3.2 released
查看>>
hack tools
查看>>
rhel5中管理swap空间
查看>>
/proc filesystem allows bypassing directory permissions on Linux
查看>>
nginx dos
查看>>
RASP解决方案包括开源方案
查看>>
Linux下共享文件系统文件传输的简单设计(转载)
查看>>
点评Ubuntu下的文件安全删除工具
查看>>
数据可视化
查看>>
Security Ressources Sites
查看>>
mysql的比较运算
查看>>
Data Breach Report
查看>>
再探偏移注射
查看>>
DNS Security Tips
查看>>
符号执行
查看>>
Remote Installation Service (RIS) in Windows Server 2003
查看>>
Layer Four Traceroute
查看>>
Hardening guide for Apache 2.2.15 on RedHat 5.4 (64bit edition)
查看>>
Microsoft Outlook Web Access (OWA) version 8.2.254.0 information disclosure vulnerability
查看>>