gpt4 book ai didi

c# - 将参数传递给正在运行的应用程序

转载 作者:可可西里 更新时间:2023-11-01 07:53:54 25 4
gpt4 key购买 nike

我正在制作一个图像 uploader (将图像上传到图像托管网站),但我在传递参数(图像位置到已运行的应用程序)时遇到了一些问题

  • 首先假设 MyApp.exe 一直在运行
  • 每当我右键单击图像时,我都会在默认的 Windows 上下文菜单中添加一个项目,上面写着“上传图像”。
  • 当点击它时,它需要将位置传递给已经运行的应用程序。

我的程序.cs:

static class Program
{
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr
wParam, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);

[STAThread]
static void Main(params string[] Arguments)
{
if (Arguments.Length > 0)
{
//This means that the the upload item in the context menu is clicked
//Here the method "uploadImage(string location)"
//of the running application must be ran
}
else
{
//just start the application
Application.Run(new ControlPanel());
}
}
}

请注意,ControlPanel 类没有可见的表单,只有托盘图标存在,因为不需要表单。

我可以就如何执行此操作获得任何帮助吗?

最佳答案

我已经弄明白了,非常感谢发布 http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64 的人链接,这正是我要找的!

这是一个完整的解决方案:

static class Program
{
[STAThread]
static void Main(params string[] Arguments)
{
SingleInstanceApplication.Run(new ControlPanel(), NewInstanceHandler);
}

public static void NewInstanceHandler(object sender, StartupNextInstanceEventArgs e)
{
string imageLocation = e.CommandLine[1];
MessageBox.Show(imageLocation);
e.BringToForeground = false;
ControlPanel.uploadImage(imageLocation);
}

public class SingleInstanceApplication : WindowsFormsApplicationBase
{
private SingleInstanceApplication()
{
base.IsSingleInstance = true;
}

public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
{
SingleInstanceApplication app = new SingleInstanceApplication();
app.MainForm = f;
app.StartupNextInstance += startupHandler;
app.Run(Environment.GetCommandLineArgs());
}
}
}

非常感谢所有人,尤其是发布我上面提到的链接的人,但我猜他删除了他的回答?

问候,肯尼

关于c# - 将参数传递给正在运行的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3793997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com