Runtime Object Moving and Resizing

Wise SCADA allows you to design dynamic interfaces that can change through user interaction during Runtime, moving beyond static screen designs. In certain projects—such as custom dashboard layouts or panel groups that operators need to reposition—the ability to drag objects or change their dimensions via mouse interaction is essential.

The following ready-to-use script, when assigned to the Panel_0 object, provides these capabilities:

  • Drag & Drop: Move the object to any desired position on the screen.
  • Dynamic Resizing: Modify the width and height by grabbing the edges or corners of the object.

How to Apply

  1. Create a new Screen Script for the relevant screen.
  2. Copy and paste the code below into the script editor.
  3. Bind the respective methods to the MouseDown, MouseMove, and MouseUp events of the object you wish to interact with (e.g., Panel_0).
/*  SCADA tags can be accessed in two ways:
1. Define tags as public fields in MainClass. Values are read before the script runs and written back after execution.
2. Use TagRead and TagSet methods to read/write values dynamically during script execution.
Read Example: INT Value = (INT)TagRead("Tag_0"); If the tag doesn't exist, TagRead returns null. If null or incorrect type, the script will not run.
Write Example: BOOL Result = TagSet("Tag_0", 123); If successful, Result will be true; otherwise, it will be false.
Note: You can access the last read and last write times of tags within scripts, as well as SCADA functions. For more information, refer to the help pages.
*/
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.CSharp;
using System.Drawing;

public class MainClass
{
// Define SCADA tags here, for example: public BOOL Tag_1; You can use 'CTRL + Add' to add a Tag.
public void Main()
{

}

bool Dragging = false;
bool Resizing = false;
Point DragCursorPoint;
Point DragPanelPoint;
Size OriginalSize;
string ResizeDirection = "";
const int ResizeBorder = 6;


public void Panel_0_MouseDown(Object sender, MouseEventArgs e)
{
Control Panel = (Control)sender;
DragCursorPoint = e.Location;
DragPanelPoint = Panel.Location;
OriginalSize = Panel.Size;
ResizeDirection = GetResizeDirection(Panel, e.Location);
if (ResizeDirection != "")
{
Resizing = true;
Cursor.Current = GetResizeCursor(ResizeDirection);
}
else
{
Dragging = true;
Cursor.Current = Cursors.SizeAll;
}
}

public void Panel_0_MouseMove(Object sender, MouseEventArgs e)
{
Control Panel = (Control)sender;
if (!Dragging && !Resizing)
{
string Direction = GetResizeDirection(Panel, e.Location);
Cursor.Current = Direction == "" ? Cursors.Default : GetResizeCursor(Direction);
return;
}
if (Dragging)
{
Point ScreenPos = Panel.PointToScreen(e.Location);
Point FormPos = Panel.FindForm().PointToClient(ScreenPos);
Panel.Location = new Point(FormPos.X - DragCursorPoint.X, FormPos.Y - DragCursorPoint.Y);
}
else if (Resizing)
{
Rectangle Bounds = Panel.Bounds;
int DiffX = e.X - DragCursorPoint.X;
int DiffY = e.Y - DragCursorPoint.Y;
switch (ResizeDirection)
{
case "N":
Bounds.Y += DiffY;
Bounds.Height -= DiffY;
break;
case "S":
Bounds.Height = OriginalSize.Height + DiffY;
break;
case "W":
Bounds.X += DiffX;
Bounds.Width -= DiffX;
break;
case "E":
Bounds.Width = OriginalSize.Width + DiffX;
break;
case "NW":
Bounds.Y += DiffY;
Bounds.Height -= DiffY;
Bounds.X += DiffX;
Bounds.Width -= DiffX;
break;
case "NE":
Bounds.Y += DiffY;
Bounds.Height -= DiffY;
Bounds.Width = OriginalSize.Width + DiffX;
break;
case "SW":
Bounds.Height = OriginalSize.Height + DiffY;
Bounds.X += DiffX;
Bounds.Width -= DiffX;
break;
case "SE":
Bounds.Width = OriginalSize.Width + DiffX;
Bounds.Height = OriginalSize.Height + DiffY;
break;
}
if (Bounds.Width < 20) Bounds.Width = 20;
if (Bounds.Height < 20) Bounds.Height = 20;
Panel.Bounds = Bounds;
}
}

public void Panel_0_MouseUp(Object sender, MouseEventArgs e)
{
Dragging = false;
Resizing = false;
ResizeDirection = "";
Cursor.Current = Cursors.Default;
}

private string GetResizeDirection(Control Panel, Point MousePos)
{
bool Left = MousePos.X <= ResizeBorder;
bool Right = MousePos.X >= Panel.Width - ResizeBorder;
bool Top = MousePos.Y <= ResizeBorder;
bool Bottom = MousePos.Y >= Panel.Height - ResizeBorder;

if (Top && Left) return "NW";
if (Top && Right) return "NE";
if (Bottom && Left) return "SW";
if (Bottom && Right) return "SE";
if (Top) return "N";
if (Bottom) return "S";
if (Left) return "W";

Similar Posts

Leave a Reply