简体   繁体   中英

Resize Popup with .NET System.Windows.Forms commands in Wonderware Application Server

I'm trying to resize an open window and as there seems to be no way to do this in Wonderware Application Server I thought maybe a.Net function may work. I've used the System.Windows.Forms.MessageBox.Show script below in the Wonderware Application Server with a button activation script. Is there a similar function to simple change the Hight and Width of the currently active window?

The message box is just an example that the Wonderware Application Server application can access some System.Windows.Forms functions in it's QuickScript.NET scripting. The Windows Forms library (system.windows.forms.dll) was imported into the Wonderware Application Server application.The script would be running on an open window and I'd like to resize it but I can't get the.Net size function to work in QuickScript.NET.


Found this System Platform DLL example http://www.plctalk.net/qanda/showthread.php?t=114301 but Visual Studio has like 20 different Class Library templates. If I try the Class Library (.Net Framework) - C# template I get a dll and can import it into System Platform, I can then find the function in the Function Browser but nothing happens in runtime when the script is run and I get this error in the SMC log: Script execution exception.Message: Non-static method requires a target..

Demo - Visual Studio 2019 and the Class Library (.Net Framework) - C# template code:

namespace ClassLibraryDemo
{
    public class DemoClass
    {
        public int GetAdd(int a, int b)
        {
            return a + b;
        }
    }
}

Demo - System Platform Button Script - For this demo code it now works with the cls = new line added.

dim cls as ClassLibraryDemo.DemoClass;
cls = new ClassLibraryDemo.DemoClass();
Me.°Test = cls.GetAdd(Me.°Test,3);

Unfortunately, the resize code that I need still has the Non-static error and it already has the object equals new line.

ResizableForm - Visual Studio 2019 and the Class Library (.Net Framework) - C# template code:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ClassLibraryROB4
{
    public class ResizableForm
    {
        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        public Form GetCurrentWindow()
        {
            IntPtr activeWindowHandle = GetForegroundWindow();
            Form f = Control.FromHandle(activeWindowHandle) as Form;
            return f;
        }
    }
}

ResizableForm - System Platform Button Script. Now with Try-Catch

Try

Dim myLib As ClassLibraryROB4.ResizableForm;
Dim myGfc As System.Windows.Forms.Form;

myLib = new ClassLibraryROB4.ResizableForm();
myGfc = myLib.GetCurrentWindow();

myGfc.Width = 10;
myGfc.Height = 10;

catch LogError(error); endtry;

SMC Error - Try-Catch


A900.Faceplate1_Control.BUTTON2: System.Reflection.TargetException: Non-static method requires a target.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at ArchestrA.QuickScript.EvaluateFunction.Evaluate(IReferenceManagerRuntime rmr)
   at ArchestrA.QuickScript.RunExpressionStatement.Run(RuntimeContext context)
   at ArchestrA.QuickScript.RunStatements.Run(RuntimeContext context)
   at ArchestrA.QuickScript.RunTryCatch.Run(RuntimeContext context)

create custom form and set properties as you want:

static CustomMsgBox MsgBox; static DialogResult result = DialogResult.No;
////////////////////////////////////////////////////////////////////////////////
public static DialogResult Show(string Text, string Caption, string btnOK, string btnCancel)
{
     MsgBox = new CustomMsgBox();
     MsgBox.label1.Text = Text;
     MsgBox.button1.Text = btnOK;
     MsgBox.button2.Text = btnCancel;
     MsgBox.Text = Caption;
     result = DialogResult.No;
     MsgBox.ShowDialog();
     return result;
}
////////////////////////////////////////////////////////////////////////////////
result = DialogResult.Yes; MsgBox.Close();

This tutorial may helpful for you

And this link for resizing and positioning for windows form

If you're willing to dabble in using Script Function libraries you can write a utility to make graphics expandable.

I got the original tutorial from this site if you search for "Expandable Popups in Wonderware ArchestrA" but it looks like they're moving their tutorials behind a signup page.

#1 - Create your Script Function library.
I wrote very basic Class library in Visual Studio using C# which looks like this:

public class ResizableForm
{
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    public Form GetCurrentWindow()
    {
        IntPtr activeWindowHandle = GetForegroundWindow();
        Form f = Control.FromHandle(activeWindowHandle) as Form;
        return f;
    }
}

#2 - Import to ArchestrA
From the IDE main window: Galaxy -> Import -> Script Function Library...
Then select the.dll that you built from #1

#3 - ArchestrA Graphic Script
When calling the graphic you want to resize, make sure you're setting an explicit Width/Height on it, then you can dynamically resize it by putting something to trigger a script like this:

Dim myLib As ResizableForm;
Dim myGfc As System.Windows.Forms.Form;

myLib = new ResizableForm();
myGfc = myLib.GetCurrentWindow();

myGfc.Width = #;
myGfc.Height = #;

...and I think that's it. Hopefully that gets you far enough along that you can toy with the rest. I do recommend going through the EverDyn site's tutorial for better examples/details.

EDIT: I guess I should just mention that the GetCurrentWindow() call is super powerful to have in ArchestrA graphics. I use another Script to get a handle of the current graphic and call some .NET functions to print the current window to a dynamically chosen printer so my clients can create labels to stick to things. Basically if you can get the Form handle from this function you can use most .NET libraries to manipulate it how you're wanting. Good luck!


CODE EDIT AS PER COMMENT:

try
  dim formID as System.IntPtr;
  dim currentForm as System.Windows.Forms.Form;

  formID = ClassLibraryROB4.ResizableForm.GetForegroundWindow();
  currentForm = System.Windows.Forms.Control.FromHandle(formID);

  currentForm.Width = #;
  currentForm.Height = #;
catch
  LogError(error);
endtry;

  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM