工作中由于长时间离开电脑,导致企业微信会显示离开图标。
用程序来干预一下它,应该可以解决问题。
废话不多说了,上图,贴代码。
程序简单操作界面如下:
程序中时间间隔为10分钟,点击开始按钮后执行,点击停止后定时结束。
打开第三方程序的代码如下:
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_LEFTDOWN = 0x02;
const int MOUSEEVENTF_LEFTUP = 0x04;
private void RunWxWorkApp()
{
try
{
// 指定想要打开的程序路径
string programPath = @"C:\Program Files (x86)\WXWork\WXWork.exe";
System.Diagnostics.Process.Start(programPath);
// 假设需要点击的聊天窗口位置为(200, 200)
SetCursorPos(200, 200);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
catch (Exception ex)
{
MessageBox.Show($"Error opening application: {ex.Message}");
}
}
定时任务使用timer组件
后台代码如下:
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 AutoClickWxWorkApp
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_LEFTDOWN = 0x02;
const int MOUSEEVENTF_LEFTUP = 0x04;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer.Interval = int.Parse(textBox1.Text);
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
RunWxWorkApp();
}
private void button2_Click(object sender, EventArgs e)
{
timer.Stop();
}
private void RunWxWorkApp()
{
try
{
// 指定想要打开的程序路径
string programPath = @"C:\Program Files (x86)\WXWork\WXWork.exe";
System.Diagnostics.Process.Start(programPath);
// 假设需要点击的聊天窗口位置为(200, 200)
SetCursorPos(200, 200);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
}
catch (Exception ex)
{
MessageBox.Show($"Error opening application: {ex.Message}");
}
}
}
}