Hi,
I have two CorelDRAW 2017 files in separate windows. I have following VBA in GMS format:
#If VBA7 ThenPrivate Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As LongPtr) As LongPtr
#ElsePrivate Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
#End IfSub corel_interact() Pth = "C:\test\"fleEXE= "corelInteract.exe" ShellExecute 0, "Open", fleEXE, "", Pth, 1End Sub
This code runs an executable file created by VisualStudio. It is a windows form application with a button on it that creates a rectangle around the selected shape.
namespace corelInteract{ public partial class Form1 : Form { private corel.Application corelApp; public Form1() { Type cdrType = Type.GetTypeFromProgID("CorelDRAW.Application.19"); corelApp = (corel.Application)Activator.CreateInstance(cdrType); InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { if (corelApp.ActiveDocument.SelectionRange.Count > 0) { double xx, yy, width, height; corelApp.ActiveDocument.ActiveShape.GetBoundingBox(out xx, out yy, out width, out height); corelApp.ActiveDocument.ActiveLayer.CreateRectangle2(xx,yy,width,height); corelApp.ActiveDocument.ClearSelection(); } } }}if (corelApp.ActiveDocument.SelectionRange.Count > 0)
{ double xx, yy, width, height; corelApp.ActiveDocument.ActiveShape.GetBoundingBox(out xx, out yy, out width, out height); corelApp.ActiveDocument.ActiveLayer.CreateRectangle2(xx,yy,width,height); corelApp.ActiveDocument.ClearSelection(); } } }}
There are two issues:
1. The rectangle is created in the first window even if the macro is run in the second window.
2. The form goes below CorelDraw if I click the CorelDraw window.
Thanks and Kind Regards.
Sorry for messed up c# code. Here it is:
private void button1_Click(object sender, EventArgs e) { if (corelApp.ActiveDocument.SelectionRange.Count > 0) { double xx, yy, width, height; corelApp.ActiveDocument.ActiveShape.GetBoundingBox(out xx, out yy, out width, out height); corelApp.ActiveDocument.ActiveLayer.CreateRectangle2(xx,yy,width,height); corelApp.ActiveDocument.ClearSelection(); } } }}
I'm afraid I did not understand what you exactly need against what you intend to do...
1. Please define 'Window' (first and second window...). The rectangle is created in Active document, according to your code. Do you mean you have the second document activated when you run the macro and the rectangle is always created in the first one?
I would suggest to firstly send a message retrieving the active document name.
2. The form created by C# must be transformed in a Corel child window in order to behave like a VBA window. It is necessary to create a Wrapper class:
public class WindowWrapper : System.Windows.Forms.IWin32Window { public WindowWrapper(IntPtr handle) { _hwnd = handle; } public IntPtr Handle { get { return _hwnd; } } private IntPtr _hwnd; }
And I used it in one of my applications in the next way:
IntPtr hwnd = IntPtr.Zero; private void btShowForm_Click(object sender, RoutedEventArgs e) { frmCorel frmC = new frmCorel(corelApp); frmC.Show(new WindowWrapper(WinGetHandle("CorelDRAW", hwnd))); } public static IntPtr WinGetHandle(string wName, IntPtr hWnd) { foreach (Process pList in Process.GetProcesses()) { if (pList.MainWindowTitle.Contains(wName)) { hWnd = pList.MainWindowHandle; break; } } return hWnd; }