如何使用Process类打开便利贴

本文关键字:便利贴 何使用 Process | 更新日期: 2023-09-27 18:06:05

我想用过程类打开窗口便签,我已经知道如何为计算器和油漆做这件事,但不是便签。我把便利贴地址给它,它却打不开。我用的是64位的windows。(windows)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace main
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("mspaint");
            System.Diagnostics.Process.Start("calc");
            //sticky notes
            System.Diagnostics.Process.Start("C:''Windows''System32''StikyNot.exe");
        }
    }
}

它给出一个异常文件不能被找到,但是它存在于那个位置

如何使用Process类打开便利贴

你必须从C:'Windows'Sysnative'StikyNot.exe:

打开文件
System.Diagnostics.Process.Start(@"C:'Windows'Sysnative'StikyNot.exe");

由于x64系统上的System32文件夹是合成的,所以直接调用它不起作用。您必须使用Sysnative名称将路径映射到'real' system32文件夹

此位置适用于win8.1 64位

System.Diagnostics.Process.Start(@"C:'Windows'WinSxS'amd64_microsoft-windows-stickynotes-app_31bf3856ad364e35_6.3.9600.16384_none_de0517088d429664'StikyNot.exe");

如果它不适合你,你可以在你的windows驱动器中搜索StikyNot的其他位置

如果您想进一步尝试,您可以使用下面的程序作为参考。

下面是一个示例调用。实现逻辑如下:

public void NewNote(string someText)
{
var note = new StickyNote();
note.Activate();
note.Activated += (s, e) => { note.Signal(someText); };
}

来源:Jesper Neidermann's Blog

using System;  
using System.Diagnostics;  
using System.IO;  
using System.Runtime.InteropServices;  
using System.Threading;  
using System.Windows.Forms;  
namespace DayView  
{  
    public class StickyNote  
    {  
        private const string m_ProcessName = "StikyNot";  
        private readonly string m_ProcessFileName = Path.Combine(Environment.SystemDirectory, "StikyNot.exe");  
        private event EventHandler m_Activated = delegate { };  
        [DllImport("user32.dll")]  
        [return: MarshalAs(UnmanagedType.Bool)]  
        static extern bool SetForegroundWindow(IntPtr hWnd);  
        public void Activate()  
        {  
            bool makeNewNote = true;  
            Process p = FindProcess();  
            if (p == null)  
            {  
                p = StartProcess();  
                if (!NoteContainsText(p.MainWindowHandle))  
                {  
                    makeNewNote = false;  
                }  
            }  
            var state = new StickyNoteState();  
            state.MakeNewNote = makeNewNote;  
            state.StickyNoteProcess = p;  
            ThreadPool.QueueUserWorkItem(Activate, state);  
        }  
        private void Activate(object state)  
        {  
            var stickyNoteState = state as StickyNoteState;  
            if (stickyNoteState.MakeNewNote)  
            {  
                NewNote(stickyNoteState.StickyNoteProcess);  
            }  
            OnActivated();  
        }  
        private Process StartProcess()  
        {  
            var startInfo = new ProcessStartInfo(m_ProcessFileName);  
            Process p = Process.Start(startInfo);  
            Thread.Sleep(200); //This is an annoying hack. I haven't been able to find another way to be sure the process is started.  
            return p;  
        }  
        private void NewNote(Process p)  
        {  
            SetForegroundWindow(p.MainWindowHandle);  
            Signal("^n");              
        }  
        /// <summary>  
        /// Weird hack to find out if note contains text.  
        /// </summary>  
        /// <returns></returns>  
        private bool NoteContainsText(IntPtr handle)  
        {  
            string textOfClipboard = Clipboard.GetText();  
            Signal("^a");  
            Signal("^c");  
            Signal("{RIGHT}");  
            string noteText = Clipboard.GetText().Trim();  
            if (textOfClipboard == null)  
            {  
                Clipboard.SetText(textOfClipboard);  
            }  
            return !string.IsNullOrEmpty(noteText);  
        }  
        private Process FindProcess()  
        {  
                Process[] processes = Process.GetProcessesByName(m_ProcessName);  
                if(processes != null && processes.Length > 0)  
                {  
                    return processes[0];  
                }  
            return null;  
        }  
        internal void OnActivated()  
        {  
            m_Activated(this, new EventArgs());  
        }  
        public event EventHandler Activated  
        {  
            add { m_Activated += value; }  
            remove { m_Activated -= value; }  
        }  
        public void Signal(string message)  
        {  
            SendKeys.SendWait(message);  
            SendKeys.Flush();  
        }  
    }  
    public class StickyNoteState  
    {  
        public bool MakeNewNote { get; set; }  
        public Process StickyNoteProcess { get; set; }  
    }  
}  

你还需要添加一个应用程序设置条目。

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <appSettings>  
    <add key="SendKeys" value="SendInput"/>  
  </appSettings>  
</configuration>