Using directives and shared code

Table of contents


Description

Starting since ZennoPoster 7.3.2.0, it is possible to connect Visual Studio. You can find detailed instructions in the article Visual Studio Project

Shared code is ZennoPoster functionality that extends the capabilities of standard C# cubes. Shared code is used to insert additional classes and functions that are used by other C# actions.

Using directives are used to access functions and classes and create namespaces ( namespases ).

Working with shared code assumes that the user already has basic knowledge of C #

How to add an action to a project?

Via context menu Add Action -> Custom Code -> Using Directives and Shared Code

Or use smart search .

When you add an action, a new block "Using directives and shared code " will appear in the static blocks panel.

Where can it be applied?

  • For more efficient and convenient work with C# code

  • To create new namespaces

  • To organize work with large amounts of code so that there are no conflicts in namespaces

How to work with an action?

The Using Directives and Shared Code window consists of two tabs: Shared Code and Using Directives .

Shared code

It is a code editor with syntax highlighting, much like the code editor https://zennolab.atlassian.net/wiki/spaces/EN/pages/924582221 ... In it, you can also access the basic functionality for editing code in the context menu: copying, pasting, commenting, searching, replacing, etc.

In addition, it is possible to load the shared code from your file in the .txt or .cs format (C # source file) - this is the checkbox and field at the bottom of the window.

At the top of the editor all the uses used in the project are listed, and below is an example of declaring namespace ZennoLab.OwnCode . By analogy, users can create their own namespaces and then access them from C # actions.

To access functions and methods of shared code, they must be declared with the public access modifier . Classes can be declared public static if you do not need to work with objects of this class. If inheritance is not required, then immediately it is better to declare the class as public sealed .

In shared code, it is not possible to directly access instance or project entities as well as from C# cubes. Therefore, to work, for example, with an instance, these objects must be initiated via new ( Instance instance = new Instance ("127.0.0.1", 40500, "server"); ) or passed them via function arguments. Similarly with project variables - their values must be passed through function arguments.

Using directives

This tab has two areas. At the top, the user can add namespaces that are required to execute code in C# actions. For example, if the user needs to parse XML, then he must add using System.Xml; ...

The bottom part lists all the using that the project uses by default. They are not editable.

Fine-tuning the appearance of the code editor

You have the ability to independently customize the colors of the code editor. The SyntaxEditorColors.json configuration file is located in the directory: AppData%\ZennoLab\ZennoPoster\7\ProjectMaker. Colors for light and dark theme can be adjusted separately, in RGB format or by name.


Examples of using

In the example below, the shared code uses the passing of an instance object to the HtmlClick function and then searching for an HTML element by attributes and clicking on it. On success or failure, a string is returned to monitor the results.

namespace ZennoLab.OwnCode { public class CommonCode { public static string HtmlClick(Instance instance) { HtmlElement he = instance.ActiveTab.FindElementByAttribute("div", "class", "html", "regexp", 0); if (he.IsVoid) { return "fail"; } else { he.Click(); return "success"; } } } }

 To call this function, a C# cube is used with the following command:
return ZennoLab.OwnCode.CommonCode.HtmlClick (instance);
If this HTML element is found on the page, it will be clicked on, if not, the message “fail“ will come. This use is justified if there are many places in the project where you want to click on the same element.


In the following example, the SetImageOpacity method is fed an image and a transparency factor to be applied to the image. As a result, a picture is returned with a translucent effect.

public static Image SetImageOpacity(Image image, float opacity) { try { Bitmap bmp = new Bitmap(image.Width, image.Height); //creating graphics from a picture using (Graphics gfx = Graphics.FromImage(bmp)) { //creating a color matrix object ColorMatrix matrix = new ColorMatrix(); //setting transparency matrix.Matrix33 = opacity; //creating new attributes ImageAttributes attributes = new ImageAttributes(); //setting the transparency color of the picture attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); //draw the picture gfx.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes); } return bmp; } catch (Exception ex) { return null; } }

And this is how this function is called from a C# action:
Image img = OwnCode.CommonCode.SetImageOpacity (Image.FromFile (project.Directory + "//image.jpg"), .5f);

The resulting image from the file is processed using a translucency overlay effect and can then be saved to disk.