With GuiXT Controls, you can display a Windows Form as a popup. In this tutorial, we will display a barcode (optically readable text) as an example.

Step 1: Choose an appropriate barcode generator

For this example, we are using the ZXing.Net Library by Michael Jahn:
https://github.com/micjahn/ZXing.Net

We add a reference to the zxing.dll file to our project so that we can use it.

A list of common barcodes can be found here, for example:
http://en.wikipedia.org/wiki/Barcode

Step 2: Create a Windows Forms application with the following elements:

PictureBox: The barcode image
RichTextBox: Formatted text such as the ASCII input for the barcode

Important: To set the position of the form at runtime, please change the following property in the settings:

Step 3: Implement a function to generate and display the barcode:

Important: The Windows Forms project must be compiled as a DLL, and both DLLs (barcodeviewer.dll and zxing.dll) must be placed in the .NET script directory.

using System;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;

namespace BarcodeScanner
{
    public partial class Barcodeviewer : Form
    {
        public Barcodeviewer()
        {
            InitializeComponent();
        }

        public string create_barcode(string text, string posX, string posY, 
                        string p_type)
        {
            try
            {
                // New instance of our form
                var b = new Barcodeviewer();
                b.Text = text;
                b.richTextBox1.Text = text;

                BarcodeFormat format = BarcodeFormat.CODE_128;

                // Choose barcode type
                switch (p_type.ToLower())
                {
                    case "upca":
                        format = BarcodeFormat.UPC_A;
                        break;

                    case "code128":
                        format = BarcodeFormat.CODE_128;
                        break;

                    case "ean13":
                        format = BarcodeFormat.EAN_13;
                        break;

                    case "qr":
                        format = BarcodeFormat.QR_CODE;
                        break;
                }

                // Create barcode writer
                var writer = new BarcodeWriter
                {
                    Format = format,
                    Options = new EncodingOptions
                    {
                        Height = 100,
                        Width = 300,
                        Margin = 10,
                        PureBarcode = false
                    }
                };

                // Generate image (System.Drawing.Bitmap)
                Bitmap bitmap = writer.Write(text);

                // Display barcode
                b.pictureBox1.Image = bitmap;
                b.pictureBox1.Refresh();

                // Position form
                b.StartPosition = FormStartPosition.Manual;
                b.Location = new Point(Convert.ToInt32(posX), 
                        Convert.ToInt32(posY));

                b.Show();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "0";
        }
    }
}

Step 4: Create a GuiXT Script and InputScript:

For this example, we use a custom input field where you can enter the string that should be displayed as a barcode. In a real application, for example, you could extract the item number from an SAP field.

GuiXT

// Display barcode
inputfield (15,1) "Display barcode:" (15,15) _
    size="25" name="barcodetext" default="123456789012" -required
Set text[barcode_types] _
    "=--- Select Barcode Type ---;upca=UPC-A;code128=Code 128;ean13=EAN-13;"
dropdownlist (15,43) _
    "barcode_types" refer="V[barcode_type]" width="25"
pushbutton (15,70) "ok" _
    process="display_barcode.txt"
inputfield (16,1) "(Coordinates:)" (16,18) _
    size="5" name="barcode_position_x" default="0" -numerical
inputfield (16,25) size="5" -
    name="barcode_position_y" -nolabel default="0" -numerical



display_barcode.txt:

GuiXT
callvb returncode = tutorials.barcodeviewer.create_barcode _
    "&V[barcodetext]" "&V[barcode_position_x]" _ 
    "&V[barcode_position_y]" "&V[barcode_type]"

if not V[returncode=0]
  message "&V[returncode]" title="Return"
endif

return

Result:

Step 5: Embed the Windows Form into the SAP GUI

Moving, closing, and interacting with the barcode dialog is not intended. Therefore, it is recommended to embed the form or dialog into the SAP GUI window. Please first modify the properties of the form again. This is also the default setting for newly created dialogs.

Create a container for the barcode dialog in the GuiXT script and call the initialization routine:

GuiXT

Control (3,45) (5,85) progID="Shell.Explorer" name="r1" initFlag="r1init" _
    -closeonhide

Then you call the .NET (VB/C#) method that shows the dialog within the container:

GuiXT
if V[r1init]
  CallVB tutorials.barcodeviewer.embed_barcode _
    "&F[MARA-EAN11]" "ean13" "&V[r1]"
endif

In your .NET (VB/C#) project, you can essentially take over the create_barcode method, with the following changes (marked in red):

Important: The guinet.dll library must also be included for embedding into the SAP GUI

public string embed_barcode(string text,
                     string p_type,
                     object w)
{
    try
    {
        // New instance of our form
        var b = new Barcodeviewer();
        b.Text = text;
        b.richTextBox1.Text = text;

        BarcodeFormat format = BarcodeFormat.CODE_128;

        // Choose barcode type
        switch (p_type.ToLower())
        {
            case "upca":
                format = BarcodeFormat.UPC_A;
                break;

            case "code128":
                format = BarcodeFormat.CODE_128;
                break;

            case "ean13":
                format = BarcodeFormat.EAN_13;
                break;

            case "qr":
                format = BarcodeFormat.QR_CODE;
                break;
        }

        // Create barcode writer
        var writer = new BarcodeWriter
        {
            Format = format,
            Options = new EncodingOptions
            {
                Height = 100,
                Width = 300,
                Margin = 10,
                PureBarcode = false
            }
        };

        // Generate image (System.Drawing.Bitmap)
        Bitmap bitmap = writer.Write(text);

        // Display barcode
        b.pictureBox1.Image = bitmap;
        b.pictureBox1.Refresh();

        // Position form
        //b.StartPosition = FormStartPosition.Manual;
        //b.Location = new Point(Convert.ToInt32(posX), Convert.ToInt32(posY));

        //b.Show();

        // embed into SAP GUI window
        guixt.EmbedForm(b, w);
    }
    catch (Exception ex)
    {
        return ex.Message;
    }

    return "0";
}

Result of Step 5 (in transaction MM03):

Download barcode viewer scripts (ZIP)