Thread: VSTA Anyone?
View Single Post
  #10  
Old 17-04-2011, 14:15
helpy
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by thayne View Post
Quote:
Originally Posted by thayne View Post
All working, but I dont know how to bind ToolBox.Show(); on custom pannel button.
That is the problem, there isn't a way to do that as far as I know and it relegates VSTA in Corel to useless status until it is corrected by Corel. It is unfortunate since it is so powerful compared to VBA.
I wrote a little addin in VSTA for CorelDRAW X5 (C#), which creates a custom CommandBar with 10 Buttons. There are some problems:
  • You can not run this in Debugger! It only works if you compile it and copy the dll to the AddIn directory.
  • Cleaning up (RemoveUI) does not work in ShutDown Event, so I did it in the QueryQuit Event.

Here is my example:
Code:
using System;
using Corel.Interop.CorelDRAW;
using Corel.Interop.VGCore;

namespace [YourNameSpace]
{
    [System.AddIn.AddIn("TESTC AddIn", Version = "0.1", Publisher = "", Description = "")]
    public partial class Main
    {
        CommandBar cbAddIn;
        string[] cmdID = new string[10];
        CommandBarControl[] ctlBtn = new CommandBarControl[10];

        private void Main_Startup(object sender, EventArgs e)
        {
            app.Start += new DIDrawApplicationEvents_StartEventHandler(app_Start);
            app.QueryQuit += new DIDrawApplicationEvents_QueryQuitEventHandler(app_QueryQuit);
            app.OnUpdatePluginCommand += new DIDrawApplicationEvents_OnUpdatePluginCommandEventHandler(app_OnUpdatePluginCommand);
            app.OnPluginCommand += new DIDrawApplicationEvents_OnPluginCommandEventHandler(app_OnPluginCommand);
            UI_Setup();
        }

        private void app_Start()
        {
        }

        private void app_QueryQuit(ref bool Cancel)
        {
            UI_Remove();
        }

        private void Main_Shutdown(object sender, EventArgs e)
        {
        }

        public void UI_Setup()
        {
            string toolTip;

            cbAddIn = app.FrameWork.CommandBars.Add("TEST AddIn", cuiBarPosition.cuiBarFloating, true);
            if (cbAddIn == null)
            {
                return;
            }

            for (int i = 0; i <= 9; i++)
            {
                cmdID[i] = System.Guid.NewGuid().ToString();
                if (
                    app.AddPluginCommand(
                        cmdID[i], "Caption of command " + i.ToString(), "Tooltip of command " + i.ToString()
                    ) == true
                )
                {
                    toolTip = "Tooltip of TestButton " + i.ToString();

                    ctlBtn[i] = cbAddIn.Controls.AddCustomButton(
                        Corel.Interop.VGCore.UICategories.cdrCmdCategoryPlugins,
                        cmdID[i],
                        0,
                        true
                    );
                    if (ctlBtn[i] != null)
                    {
                        ctlBtn[i].Visible = true;
                        ctlBtn[i].ToolTipText = toolTip;
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("Could not add CommandBarControl: CommandID = " + cmdID[i]);
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("Could not add PluginCommand: CommandID = " + cmdID[i]);
                }
            }
            cbAddIn.Visible = true;
        }

        public void UI_Remove()
        {
            cbAddIn.Delete();
            for (int i = 1; i < 10; i++)
                app.RemovePluginCommand(cmdID[i]);
        }

        public void TestButtonX(int nr, string CommandID)
        {
            System.Windows.Forms.MessageBox.Show("You pressed button nr. " + nr.ToString() + " with CommandID " + CommandID);
        }

        private void app_OnPluginCommand(string CommandID)
        {
            for( int i = 0; i<10; i++)
            {
                if (cmdID[i] == CommandID)
                {
                    TestButtonX(i, CommandID);
                    break;
                }
            }
        }

        private void app_OnUpdatePluginCommand(string CommandID, ref bool Enabled, ref Corel.Interop.CorelDRAW.cdrCommandCheckState CheckState)
        {
            for (int i = 0; i < 10; i++)
            {
                if (cmdID[i] == CommandID)
                {
                    Enabled = true;
                    break;
                }
            }
        }

        #region VSTA generated code
        private Application app = null;
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Main_Startup);
            this.Shutdown += new System.EventHandler(Main_Shutdown);
            app = (Application)this.Host;
        }
        #endregion

    }
}
For now it only uses the same default icon for each button. I will try to change icons later.

There are some more application events (DocumentClose, DocumentNew, DocumentOpen, WindowActivate, ...) which you also could use!

Hope, that this will help some of you!

greetings from Germany,
guido

Last edited by helpy; 17-04-2011 at 14:17.
Reply With Quote