Hide

SftButton/OCX 3.0 - Button Control

Display
Print

Popup Sample (C#)

This sample illustrates drop down buttons and popup menus.

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


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

private int m_WorldIndex = 0;

private void SetImage()
{
    // Update the button with the next image in the image list
    // to get the spinning globe effect
    axSftButton4.Image1.NETImageObject = imageList1.Images[m_WorldIndex];
    ++m_WorldIndex;
    if (m_WorldIndex >= imageList1.Images.Count) m_WorldIndex = 0;
}

private void timer1_Tick(object sender, System.EventArgs e)
{
    // Update the button bitmap every so often to get the
    // spinning globe effect
    SetImage();
}

// In order to support .NET 1.0 through the latest, we use TrackPopupMenu
// to nicely right-align the popup menu (Windows API), because the
// ContextMenu class doesn't support defining the alignment
[DllImport("user32.dll", EntryPoint="TrackPopupMenu")]
public static extern IntPtr TrackPopupMenu(IntPtr Handle, uint uFlags, int x, int y, int nReserved, IntPtr hwnd, IntPtr prcRect);
const int TPM_RIGHTALIGN = 0x0008;
const int TPM_TOPALIGN = 0x0000;
const int TPM_LEFTBUTTON = 0x0000;

private void menuItem1_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Default Action selected");
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Action 1 selected");
}

private void menuItem3_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Action 2 selected");
}

private void menuItem4_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Action 3 selected");
}

private void axSftButton1_ClickEvent(object sender, System.EventArgs e)
{
    // Invoke the default action, because the button was clicked
    menuItem1_Click(axSftButton1, new System.EventArgs());
}

private void axSftButton1_DropDownClick(object sender, System.EventArgs e)
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    Point pt = new Point(axSftButton1.Location.X + axSftButton1.Size.Width,
                            axSftButton1.Location.Y + axSftButton1.Size.Height);
    pt = PointToScreen(pt);
    axSftButton1.CancelMode();
    TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
}

private void axSftButton2_ClickEvent(object sender, System.EventArgs e)
{
    // Invoke the default action, because the button was clicked
    menuItem1_Click(axSftButton2, new System.EventArgs());
}

private void axSftButton2_DropDownClick(object sender, System.EventArgs e)
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    Point pt = new Point(axSftButton2.Location.X + axSftButton2.Size.Width,
        axSftButton2.Location.Y + axSftButton2.Size.Height);
    pt = PointToScreen(pt);
    axSftButton2.CancelMode();
    TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
}

private void axSftButton3_ClickEvent(object sender, System.EventArgs e)
{
    // Invoke the default action, because the button was clicked
    menuItem1_Click(axSftButton3, new System.EventArgs());
}

private void axSftButton3_DropDownClick(object sender, System.EventArgs e)
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    Point pt = new Point(axSftButton3.Location.X + axSftButton3.Size.Width,
        axSftButton3.Location.Y + axSftButton3.Size.Height);
    pt = PointToScreen(pt);
    axSftButton3.CancelMode();
    TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
}

private void axSftButton4_ClickEvent(object sender, System.EventArgs e)
{
    // Invoke the default action, because the button was clicked
    menuItem1_Click(axSftButton4, new System.EventArgs());
}

private void axSftButton4_DropDownClick(object sender, System.EventArgs e)
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    Point pt = new Point(axSftButton4.Location.X + axSftButton4.Size.Width,
        axSftButton4.Location.Y + axSftButton4.Size.Height);
    pt = PointToScreen(pt);
    axSftButton4.CancelMode();
    TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
}

private void axSftButton5_ClickEvent(object sender, System.EventArgs e)
{
    // Invoke the default action, because the button was clicked
    menuItem1_Click(axSftButton5, new System.EventArgs());
}

private void axSftButton5_DropDownClick(object sender, System.EventArgs e)
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    Point pt = new Point(axSftButton5.Location.X + axSftButton5.Size.Width,
        axSftButton5.Location.Y + axSftButton5.Size.Height);
    pt = PointToScreen(pt);
    axSftButton5.CancelMode();
    TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
}

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