Hide

SftBox/OCX 5.0 - Combo Box Control

Display
Print

Pictures Sample (C#)

This sample illustrates images, checkboxes, radiobuttons, more.

The source code is located at C:\Program Files (x86)\Softelvdm\SftBox OCX 5.0\Samples\Visual Studio - CSharp\Pictures\Form1.cs or C:\Program Files\Softelvdm\SftBox OCX 5.0\Samples\Visual Studio - CSharp\Pictures\Form1.cs (on 32-bit Windows versions).

private void Button1_Click(object sender, System.EventArgs e)
{
    Application.Exit();
}

private void CopyImageFromCurrentItem()
{
    int ItemIndex;
    SftPictureObject Img;

    ItemIndex = axSftBox1.Items.Selection;
    if (ItemIndex < 0)
        return;

    // don't use images that are too large in the other areas
    Img = axSftBox1.get_Cell(ItemIndex, 0).Image;
    if (Img.ActualHeight <= 20 && Img.ActualWidth <= 20) {
        axSftBox1.RowColumnHeader.Image = Img;
        axSftBox1.get_Header(1).Image = Img;
    } else {
        axSftBox1.RowColumnHeader.Image.Clear();
        axSftBox1.get_Header(1).Image.Clear();
    }
}

private void ToggleImage(SftPictureObject Img)
{
    switch (Img.Appearance) {
    case SftPictureImageConstants.sftImageCheckboxNo:
        Img.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
        break;
    case SftPictureImageConstants.sftImageCheckboxYes:
        Img.Appearance = SftPictureImageConstants.sftImageCheckboxNo;
        break;
    case SftPictureImageConstants.sftImageCheckboxUnknown:
        Img.Appearance = SftPictureImageConstants.sftImageCheckboxNo;
        break;
    case SftPictureImageConstants.sftImageCheckboxNoDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageCheckboxYesDisabled;
        break;
    case SftPictureImageConstants.sftImageCheckboxYesDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageCheckboxNoDisabled;
        break;
    case SftPictureImageConstants.sftImageCheckboxUnknownDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageCheckboxNoDisabled;
        break;
    case SftPictureImageConstants.sftImageRadioButtonNo:
        Img.Appearance = SftPictureImageConstants.sftImageRadioButtonYes;
        break;
    case SftPictureImageConstants.sftImageRadioButtonYes:
        Img.Appearance = SftPictureImageConstants.sftImageRadioButtonNo;
        break;
    case SftPictureImageConstants.sftImageRadioButtonNoDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageRadioButtonYesDisabled;
        break;
    case SftPictureImageConstants.sftImageRadioButtonYesDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageRadioButtonNoDisabled;
        break;
    case SftPictureImageConstants.sftImageUp:
        Img.Appearance = SftPictureImageConstants.sftImageDown;
        break;
    case SftPictureImageConstants.sftImageUpDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageDownDisabled;
        break;
    case SftPictureImageConstants.sftImageDown:
        Img.Appearance = SftPictureImageConstants.sftImageUp;
        break;
    case SftPictureImageConstants.sftImageDownDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageUpDisabled;
        break;
    case SftPictureImageConstants.sftImageSortAsc:
        Img.Appearance = SftPictureImageConstants.sftImageSortDesc;
        break;
    case SftPictureImageConstants.sftImageSortAscDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageSortDescDisabled;
        break;
    case SftPictureImageConstants.sftImageSortDesc:
        Img.Appearance = SftPictureImageConstants.sftImageSortAsc;
        break;
    case SftPictureImageConstants.sftImageSortDescDisabled:
        Img.Appearance = SftPictureImageConstants.sftImageSortAscDisabled;
        break;
    }
}

private void PropagateImage(int ItemIndex, SftPictureObject Img)
{
    // don't use images that are too large in the other areas
    if (Img.ActualHeight <= 20 && Img.ActualWidth < 20) {
        axSftBox1.get_Item(ItemIndex).Image = Img;
        axSftBox1.get_Item(ItemIndex).LabelImage = Img;
        axSftBox1.get_Item(ItemIndex).RowHeader.Image = Img;
    }
}

private void UpdateImages(int ItemIndex, System.Drawing.Image img, SftBoxHAlignConstants Align)
{
    SftBoxCell C = axSftBox1.get_Cell(ItemIndex, 0);
    C.Image.NETImageObject = img;
    C.ImageHAlign = Align;
    PropagateImage(ItemIndex, C.Image);
}

private void UpdateImageList(int ItemIndex, ImageList ImageListControl, int i, SftBoxHAlignConstants Align)
{
    SftBoxCell C = axSftBox1.get_Cell(ItemIndex, 0);
    C.Image.SetImageListH((int) ImageListControl.Handle, (short) i);
    C.ImageHAlign = Align;
    PropagateImage(ItemIndex, C.Image);
}

private void UpdateColor(int ItemIndex, System.Drawing.Color Clr, SftBoxHAlignConstants Align)
{
    SftBoxCell C = axSftBox1.get_Cell(ItemIndex, 0);
    C.Image.SetColorSample(OLECvt.ToOleColor(Clr), OLECvt.ToOleColor(Color.Black));
    C.Image.Width = 12;
    C.Image.Height = 12;
    C.ImageHAlign = Align;
    PropagateImage(ItemIndex, C.Image);
}

private void AddColor(string Text, Color Clr)
{
    int i;
    i = axSftBox1.Items.Add(Text);
    axSftBox1.get_Item(i).Level = 2;
    UpdateColor(i, Clr, SftBoxHAlignConstants.halignSftBoxRight);
}

private void UpdateBuiltinImage(int ItemIndex, SftPictureImageConstants Style, int wPix, int hPix, SftBoxHAlignConstants Align)
{
    SftBoxCell C = axSftBox1.get_Cell(ItemIndex, 0);
    C.Image.Appearance = Style;
    C.Image.Width = wPix;
    C.Image.Height = hPix;
    C.ImageHAlign = Align;
    PropagateImage(ItemIndex, C.Image);
}

private void AddBuiltinImage(string Text, SftPictureImageConstants Style, int wPix, int hPix)
{
    int i = axSftBox1.Items.Add(Text);
    axSftBox1.get_Item(i).Level = 2;
    UpdateBuiltinImage(i, Style, wPix, hPix, SftBoxHAlignConstants.halignSftBoxRight);
}

private void ShowSortDirection(bool Ascending)
{
    if (Ascending)
        axSftBox1.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortAsc;
    else
        axSftBox1.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortDesc;
    m_SortDirection = Ascending;
}

private void SetSortDirection(bool NewDirection)
{
    ShowSortDirection(NewDirection);
    if (NewDirection)
        axSftBox1.Items.Sort(-1, 0, SftBoxSortConstants.sortSftBoxAscending);
    else
        axSftBox1.Items.Sort(-1, 0, SftBoxSortConstants.sortSftBoxDescending);
}

private void Form1_Load(object sender, System.EventArgs e) {

    int ItemIndex = axSftBox1.Items.Add("Supported Picture Types");
    axSftBox1.get_Cell(ItemIndex, 1).Text = "SftBox/OCX supports numerous image types, such as GDI+ images, bitmaps, icons, ImageLists and also offers numerous built-in images.";

    // add GDI+ samples

    ItemIndex = axSftBox1.Items.Add("GDI+ Images");
    axSftBox1.get_Item(ItemIndex).Level = 1;
    axSftBox1.get_Cell(ItemIndex, 1).Text = "All GDI+ images are supported, like GIF, JPEG, Exif, PNG, TIFF and device-independent bitmaps (up to 64bpp) with semi-transparent and translucent areas.";

    int I = axSftBox1.Items.Add("PNG Sample with alpha-channel for translucent edges");
    axSftBox1.get_Item(I).Level = 2;
    UpdateImages(I, picturePNG1.Image, SftBoxHAlignConstants.halignSftBoxRight);

    I = axSftBox1.Items.Add("Another PNG Sample with alpha-channel for translucent edges");
    axSftBox1.get_Item(I).Level = 2;
    UpdateImages(I, picturePNG2.Image, SftBoxHAlignConstants.halignSftBoxRight);

    // add bitmap samples

    ItemIndex = axSftBox1.Items.Add("Bitmaps");
    axSftBox1.get_Item(ItemIndex).Level = 1;
    axSftBox1.get_Cell(ItemIndex, 1).Text = "Bitmaps can be of varying sizes and also support Bitmap Transparency, which allows the background to show through the image (in areas selected by the bitmap designer).";

    I = axSftBox1.Items.Add("Large Bitmap");
    axSftBox1.get_Item(I).Level = 2;
    UpdateImages(I, PictureLogo.Image, SftBoxHAlignConstants.halignSftBoxRight);

    I = axSftBox1.Items.Add("Various bitmap sizes");
    axSftBox1.get_Item(I).Level = 2;
    UpdateImages(I, PictureSmallBitmap.Image, SftBoxHAlignConstants.halignSftBoxRight);

    // add icon samples

    ItemIndex = axSftBox1.Items.Add("Icons");
    axSftBox1.get_Item(ItemIndex).Level = 1;
    axSftBox1.get_Cell(ItemIndex, 1).Text = "The supported icons can be stardard size icons (32x32) or any other size supported by the operating system.";

    I = axSftBox1.Items.Add("Standard Icon (32x32)");
    axSftBox1.get_Item(I).Level = 2;
    UpdateImages(I, PictureIcon.Image, SftBoxHAlignConstants.halignSftBoxRight);

    I = axSftBox1.Items.Add("Any other size");
    axSftBox1.get_Item(I).Level = 2;
    UpdateImages(I, PictureIconSmall.Image, SftBoxHAlignConstants.halignSftBoxRight);

    // add imagelist samples

    ItemIndex = axSftBox1.Items.Add("ImageLists");
    axSftBox1.get_Item(ItemIndex).Level = 1;
    axSftBox1.get_Cell(ItemIndex, 1).Text = "Complete ImageList support simplifies bitmap handling and can avoid the limited resource availability on earlier Windows versions.";

    for (int C = 0 ; C < imageList1.Images.Count ; ++C) {
        I = axSftBox1.Items.Add("Image " + C.ToString());
        axSftBox1.get_Item(I).Level = 2;
        UpdateImageList(I, imageList1, C, SftBoxHAlignConstants.halignSftBoxRight);
    }

    // add color samples

    ItemIndex = axSftBox1.Items.Add("Color Samples");
    axSftBox1.get_Item(ItemIndex).Level = 1;
    axSftBox1.get_Cell(ItemIndex, 1).Text = "Using the built-in color sample image, simple color selection can easily be implemented.  Color samples can display all colors available.";

    AddColor("Black", System.Drawing.Color.Black);
    AddColor("Blue", System.Drawing.Color.Blue);
    AddColor("Cyan", System.Drawing.Color.Cyan);
    AddColor("Green", System.Drawing.Color.Lime);
    AddColor("Magenta", System.Drawing.Color.Magenta);
    AddColor("Red", System.Drawing.Color.Red);
    AddColor("White", System.Drawing.Color.White);
    AddColor("Yellow", System.Drawing.Color.Yellow);

    AddColor("3DDKSHADOW - Dark shadow for 3D elements", System.Drawing.SystemColors.ControlDarkDark);
    AddColor("3DFACE - Face color for 3D elements", System.Drawing.SystemColors.Control);
    AddColor("3DHILIGHT - Edges facing the light source", System.Drawing.SystemColors.ControlLightLight);
    AddColor("3DLIGHT - Edges facing the light source", System.Drawing.SystemColors.ControlLight);
    AddColor("3DSHADOW - Edges facing away from the light source", System.Drawing.SystemColors.ControlDark);
    AddColor("INFOBK - Background color for tooltip controls", System.Drawing.SystemColors.Info);
    AddColor("INFOTEXT - Text color for tooltip controls", System.Drawing.SystemColors.InfoText);
    AddColor("MENUTEXT - Text in menus", System.Drawing.SystemColors.MenuText);
    AddColor("ACTIVEBORDER - Active window border", System.Drawing.SystemColors.ActiveBorder);
    AddColor("ACTIVECAPTION - Active window caption", System.Drawing.SystemColors.ActiveCaption);
    AddColor("APPWORKSPACE - Background color MDI applications", System.Drawing.SystemColors.AppWorkspace);
    AddColor("BACKGROUND - Desktop", System.Drawing.SystemColors.Desktop);
    AddColor("BTNFACE - Face shading on push buttons", System.Drawing.SystemColors.Control);
    AddColor("BTNHILIGHT - Highlight color for buttons", System.Drawing.SystemColors.ControlLightLight);
    AddColor("BTNSHADOW - Edge shading on push buttons", System.Drawing.SystemColors.ControlDark);
    AddColor("BTNTEXT - Text on push buttons", System.Drawing.SystemColors.ControlText);
    AddColor("CAPTIONTEXT - Text in caption", System.Drawing.SystemColors.ActiveCaptionText);
    AddColor("GRAYTEXT - Grayed (disabled); text", System.Drawing.SystemColors.GrayText);
    AddColor("HIGHLIGHT - Selected Item(s)", System.Drawing.SystemColors.Highlight);
    AddColor("HIGHLIGHTTEXT - Text of selected item(s)", System.Drawing.SystemColors.HighlightText);
    AddColor("INACTIVEBORDER - Inactive window border", System.Drawing.SystemColors.InactiveBorder);
    AddColor("INACTIVECAPTION - Inactive window caption", System.Drawing.SystemColors.InactiveCaption);
    AddColor("INACTIVECAPTIONTEXT - Inactive caption text color", System.Drawing.SystemColors.InactiveCaptionText);
    AddColor("MENU - Menu background", System.Drawing.SystemColors.Menu);
    AddColor("SCROLLBAR - Scroll bar gray area", System.Drawing.SystemColors.ScrollBar);
    AddColor("WINDOW - Window background", System.Drawing.SystemColors.Window);
    AddColor("WINDOWFRAME - Window frame", System.Drawing.SystemColors.WindowFrame);
    AddColor("WINDOWTEXT - Text in windows", System.Drawing.SystemColors.WindowText);

    // add predefined image samples

    ItemIndex = axSftBox1.Items.Add("Predefined (Built-in) Pictures");
    axSftBox1.get_Cell(ItemIndex, 1).Text = "Predefined images are available for commonly used items, such as check boxes, radio buttons, sort direction indicators and more...";

    ItemIndex = axSftBox1.Items.Add("Check Boxes - Honors Windows Themes");
    axSftBox1.get_Item(ItemIndex).Level = 1;

    AddBuiltinImage("Enabled, Selected Check Box", SftPictureImageConstants.sftImageCheckboxYes, 14, 14);
    AddBuiltinImage("Disabled, Selected Check Box", SftPictureImageConstants.sftImageCheckboxYesDisabled, 14, 14);
    AddBuiltinImage("Enabled Check Box", SftPictureImageConstants.sftImageCheckboxNo, 14, 14);
    AddBuiltinImage("Disabled Check Box", SftPictureImageConstants.sftImageCheckboxNoDisabled, 14, 14);
    AddBuiltinImage("Enabled, Unknown Check Box", SftPictureImageConstants.sftImageCheckboxUnknown, 14, 14);
    AddBuiltinImage("Disabled, Unknown Check Box", SftPictureImageConstants.sftImageCheckboxUnknownDisabled, 14, 14);

    ItemIndex = axSftBox1.Items.Add("Radio Buttons - Honors Windows Themes");
    axSftBox1.get_Item(ItemIndex).Level = 1;

    AddBuiltinImage("Enabled, Selected Radio Button", SftPictureImageConstants.sftImageRadioButtonYes, 14, 14);
    AddBuiltinImage("Disabled, Selected Radio Button", SftPictureImageConstants.sftImageRadioButtonYesDisabled, 14, 14);
    AddBuiltinImage("Enabled Radio Button", SftPictureImageConstants.sftImageRadioButtonNo, 14, 14);
    AddBuiltinImage("Disabled Radio Button", SftPictureImageConstants.sftImageRadioButtonNoDisabled, 14, 14);

    ItemIndex = axSftBox1.Items.Add("Up/Down Buttons - Honors Windows Themes");
    axSftBox1.get_Item(ItemIndex).Level = 1;

    AddBuiltinImage("Enabled Up Button", SftPictureImageConstants.sftImageUp, 14, 14);
    AddBuiltinImage("Disabled Up Button", SftPictureImageConstants.sftImageUpDisabled, 14, 14);
    AddBuiltinImage("Enabled Down Button", SftPictureImageConstants.sftImageDown, 14, 14);
    AddBuiltinImage("Disabled Down Button", SftPictureImageConstants.sftImageDownDisabled, 14, 14);

    ItemIndex = axSftBox1.Items.Add("Sort Direction Indicator");
    axSftBox1.get_Item(ItemIndex).Level = 1;

    AddBuiltinImage("Enabled Ascending Indicator", SftPictureImageConstants.sftImageSortAsc, 8, 8);
    AddBuiltinImage("Disabled Ascending Indicator", SftPictureImageConstants.sftImageSortAscDisabled, 8, 8);
    AddBuiltinImage("Enabled Descending Indicator", SftPictureImageConstants.sftImageSortDesc, 8, 8);
    AddBuiltinImage("Disabled Descending Indicator", SftPictureImageConstants.sftImageSortDescDisabled, 8, 8);


    axSftBox1.RowHeaders.MakeOptimal(0); // make the row headers optimal
    axSftBox1.get_Column(0).MakeOptimal(0); // make the first column optimal
    axSftBox1.Items.RecalcHorizontalExtent(0); // update horizontal scroll bar

    axSftBox1.Items.Selection = 0;

    CopyImageFromCurrentItem();
}

private void ExpandCollapseItem(int ItemIndex, short Shift)
{
    SftBoxItem Item = axSftBox1.get_Item(ItemIndex);
    if (Item.Expanded)
        Item.Collapse(true);
    else
        Item.Expand(true, ((Shift & (short)SftBoxKeyConstants.constSftBoxCtrlMask))!=0 ? true : false);
}

private void axSftBox1_ItemClick(object sender, AxSftBoxLib50._ISftBoxEvents_ItemClickEvent e)
{
    switch (e.areaType) {
    case SftBoxAreaConstants.areaSftBoxColumn:
        if (e.colNum == 0)
            SetSortDirection(!m_SortDirection);
        break;
    case SftBoxAreaConstants.areaSftBoxCellGraphic:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ToggleImage(axSftBox1.get_Cell(e.itemIndex, e.colNum).Image);
        break;
    case SftBoxAreaConstants.areaSftBoxItem:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ToggleImage(axSftBox1.get_Item(e.itemIndex).Image);
        break;
    case SftBoxAreaConstants.areaSftBoxLabel:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ToggleImage(axSftBox1.get_Item(e.itemIndex).LabelImage);
        break;
    case SftBoxAreaConstants.areaSftBoxButton:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ExpandCollapseItem(e.itemIndex, e.shift);
        break;
    }
}

private void axSftBox1_ItemDblClk(object sender, AxSftBoxLib50._ISftBoxEvents_ItemDblClkEvent e)
{
    switch (e.areaType) {
    case SftBoxAreaConstants.areaSftBoxColumnRes:
        axSftBox1.get_Column(e.colNum).MakeOptimal(0); // Make column width optimal
        axSftBox1.Items.RecalcHorizontalExtent(0); // Update horizontal scroll bar
        break;
    case SftBoxAreaConstants.areaSftBoxColumn:
        if (e.colNum == 0)
            SetSortDirection(!m_SortDirection);
        break;
    case SftBoxAreaConstants.areaSftBoxCellGraphic:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ToggleImage(axSftBox1.get_Cell(e.itemIndex, e.colNum).Image);
        break;
    case SftBoxAreaConstants.areaSftBoxLabel:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ToggleImage(axSftBox1.get_Item(e.itemIndex).LabelImage);
        break;
    case SftBoxAreaConstants.areaSftBoxItem:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        if (axSftBox1.get_Item(e.itemIndex).Expandable)
            ExpandCollapseItem(e.itemIndex, e.shift);
        else
            ToggleImage(axSftBox1.get_Item(e.itemIndex).Image);
        break;
    case SftBoxAreaConstants.areaSftBoxButton:
    case SftBoxAreaConstants.areaSftBoxCell:
        if (e.part == SftBoxPortionConstants.partSftBoxDropDown)
            axSftBox1.DropDown.Suppress(); // Make sure we don't close the drop down
        ExpandCollapseItem(e.itemIndex, e.shift);
        break;
    }
}

private void axSftBox1_SelectionChange(object sender, System.EventArgs e)
{
    CopyImageFromCurrentItem();
}

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.