Wednesday, January 22, 2014

Setup a manged control for drag and drop

In an earlier post I have made an example of to make a form with a drag 'n' drop enabled managed control.

Now, here is the initialization code to put a nice image in the control (which is a PictureBox control now), and set the transparency color of the control to the background color of, in this case, the tab page it is on:

private void initDropControl()
    Image                           resImg;
    System.Drawing.Image            img;
    System.Drawing.Color            backGroundColor;
    int                             colorR;
    int                             colorG;
    int                             colorB;
    System.Windows.Forms.PictureBox dropControl;

    // Get handle to the control
    dropControl = ManagedHostDropFile.control();

    // Setup event handlers for the control
    dropControl.add_DragDrop(new ManagedEventHandler(this, 'dropFile_DragDrop'));
    dropControl.add_DragEnter(new ManagedEventHandler(this, 'dropFile_DragEnter'));

    // Load the AX resource image
    resImg = new Image();

    // Get the tab page background color
    backGroundColor = System.Drawing.ColorTranslator::FromOle(TabPageLoadFile.backgroundColor());
    colorR = backGroundColor.get_R();
    colorG = backGroundColor.get_G();
    colorB = backGroundColor.get_B();

    // Set the transparancy color of the AX resource image to the tab page background color
    resImg.transparent(true, colorR, colorG, colorB);

    // Create an image object that the Windows Form control is able to load
    img = System.Drawing.Image::FromHbitmap(new System.IntPtr(resImg.exportBitmap()));

    // Add the image to the control