问题描述
我正在制作图像上传器(将图像上传到图像托管网站),但在传递参数(图像位置到已运行的应用程序)时遇到了一些问题
I am making an image uploader (upload image to image hosting website) and I'm having some issues passing an argument (image location to an already running application)
- 首先假设 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 类没有可见的表单,由于不需要表单,因此只存在托盘图标.
Note that the ControlPanel class doesn't have a visible form, only a tray icon is present since a form is not needed.
我能得到有关如何执行此操作的任何帮助吗?
Could I get any help on how to do this?
推荐答案
我已经想通了,非常感谢发布 http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64 链接,这正是我要找的!
I have figured it out, so awesome thanks for the person who posted the http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a5bcfc8a-bf69-4bbc-923d-f30f9ecf5f64 link, this is exactly what I was looking for!
这是一个完整的解决方案:
Here's a the full solution:
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());
}
}
}
非常感谢所有人,尤其是我上面提到的发布该链接的人,但我猜他删除了他的答案?
Thanks alot all, and especially the person who posted that link I mentioned above but I guess he deleted his answer?
问候,肯尼
这篇关于将参数传递给正在运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!