I added an "Edit with Photoshop" button and succeeded opening Photoshop with a file using this code:
Type PhotoshopType = Type.GetTypeFromProgID("Photoshop.Application");
object PhotoshopInst = Activator.CreateInstance(PhotoshopType);
PhotoshopType.InvokeMember("Open", BindingFlags.InvokeMethod, null, PhotoshopInst, new object[1] { "c:\files\image.jpg" });
But I am failing to activate (give focus to) Photoshop so the user does not have to switch manually. Any idea on how to accomplish it?
CodePudding user response:
This is typically a use-case for the
Where the "localized control type" is "window" and the ClassName" is "Photoshop".
CodePudding user response:
This type of work requires using the Win32 API that has lots of low level APIs to work with Windows.
http://www.pinvoke.net/ is the best place to look up examples of these API's.
Also the Help Manual with Visual Studio 6 has a Win32.chm.
Here is a link to the VS2008 documentation (2.2GB) and it has 99% of the win32 API info: https://www.microsoft.com/en-us/download/details.aspx?id=20955&751be11f-ede8-5a0c-058c-2ee190a24fa6=True
Set Active Window
https://www.pinvoke.net/default.aspx/user32.setactivewindow
C# Signature:
[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);
Method:
public void MakeFormActive()
{
SetActiveWindow(wndHandle);
}
Alternatively use this method that I found on MSDN https://social.msdn.microsoft.com/Forums/vstudio/en-US/c53bcda9-2a77-4af3-b0e2-3ea77deadcee/get-hwnd-from-activator-dreated-com-object?forum=clr
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
[SuppressUnmanagedCodeSecurity, SecurityCritical, DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern bool ShowWindowAsync(HandleRef hWnd, int nCmdShow);
private const int SW_SHOWNORMAL = 0x1;
private const int SW_SHOWMINIMIZED = 0x2;
private const int SW_SHOWMAXIMIZED = 0x3;
static void Main(string[] args) {
string appName = "Photoshop.Application";
Type PhotoshopType = Type.GetTypeFromProgID(appName);
object PhotoshopInst = Activator.CreateInstance(applicationType);
applicationType.InvokeMember("Visible", BindingFlags.IgnoreCase | BindingFlags.SetProperty | BindingFlags.Public, null, excelApplication, new object[]{true});
try
{
var process = (from p in Process.GetProcessesByName(appName) // Double check appName is correct
where p.MainWindowHandle != null
orderby p.Id
select p).LastOrDefault();
if(process != null)
ShowWindowAsync(new HandleRef(null, process.MainWindowHandle), SW_SHOWMAXIMIZED);
} finally {
Console.WriteLine("Press any key to exit.");
Console.ReadKey(true);
applicationType.InvokeMember("Quit", BindingFlags.IgnoreCase | BindingFlags.OptionalParamBinding | BindingFlags.InvokeMethod | BindingFlags.Public, null, excelApplication, new object[0]);
}
}