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\VC++\Popup\PopupDlg.cpp or C:\Program Files\Softelvdm\SftButton OCX 3.0\Samples\VC++\Popup\PopupDlg.cpp (on 32-bit Windows versions).

// PopupDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Popup.h"
#include "PopupDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPopupDlg dialog

CPopupDlg::CPopupDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CPopupDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CPopupDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    m_Menu.LoadMenu(IDR_MENU1);
    for (int i = 0 ; i < WORLDBITMAPS ; ++i ) {
        m_World[i].LoadBitmap(IDB_WORLD1+i);
        ASSERT(m_World[i].m_hObject);
    }
    m_WorldIndex = 0;
}

void CPopupDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CPopupDlg)
    DDX_Control(pDX, IDC_TITLE, m_Title);
    DDX_Control(pDX, IDC_SFTBTN1, m_Button1);
    DDX_Control(pDX, IDC_SFTBTN2, m_Button2);
    DDX_Control(pDX, IDC_SFTBTN3, m_Button3);
    DDX_Control(pDX, IDC_SFTBTN4, m_Button4);
    DDX_Control(pDX, IDC_SFTBTN5, m_Button5);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPopupDlg, CDialog)
    //{{AFX_MSG_MAP(CPopupDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_COMMAND(IDM_DEFAULTACTION, OnDefaultAction)
    ON_WM_TIMER()
    ON_COMMAND(IDM_ACTION1, OnAction1)
    ON_COMMAND(IDM_ACTION2, OnAction2)
    ON_COMMAND(IDM_ACTION3, OnAction3)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPopupDlg message handlers

BOOL CPopupDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon

    // Make a bold font for the dialog title
    CFont* pFont = GetFont(); // get dialog font
    LOGFONT lf;
    pFont->GetLogFont(&lf);
    lf.lfWeight += (FW_BOLD-FW_NORMAL);
    m_BoldFont.CreateFontIndirect(&lf);

    m_Title.SetFont(&m_BoldFont);

    // Get all ISftButton interface pointers for access to all
    // properties and methods (see Using SftButton/OCX with Visual C++
    // in the online help for more information)
    m_vButton2 = m_Button2.GetControlUnknown();
    m_vButton4 = m_Button4.GetControlUnknown();

    SetImage();
    SetTimer(TIMERID, 200, NULL);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPopupDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

void CPopupDlg::SetImage()
{
    // Update the button with the next bitmap in the m_World bitmap
    // array to get the spinning globe effect

    PICTDESC pictDesc;
    pictDesc.cbSizeofstruct = sizeof(pictDesc);
    pictDesc.picType = PICTYPE_BITMAP;
    pictDesc.bmp.hbitmap = (HBITMAP) m_World[m_WorldIndex].m_hObject;

    IPicture* pPic;
    HRESULT hr = ::OleCreatePictureIndirect(&pictDesc, IID_IPicture, FALSE, (void**)&pPic);
    ASSERT(SUCCEEDED(hr));
    if (FAILED(hr)) return;

    IPictureDispPtr pPicDisp = pPic;

    ISftPictureObjectPtr pImg;
    hr = m_vButton4->get_Image1(&pImg);
    ASSERT(SUCCEEDED(hr));

    hr = pImg->putref_Picture(pPicDisp);
    ASSERT(SUCCEEDED(hr));

    ++m_WorldIndex;
    if (m_WorldIndex >= WORLDBITMAPS) m_WorldIndex = 0;
}

void CPopupDlg::OnTimer(UINT_PTR nIDEvent)
{
    // Update the button bitmap every so often to get the
    // spinning globe effect
    SetImage();
    CDialog::OnTimer(nIDEvent);
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPopupDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void CPopupDlg::OnClickSftbtn1()
{
    // Invoke the default action, because the button was clicked
    SendMessage(WM_COMMAND, IDM_DEFAULTACTION);
}

void CPopupDlg::OnDropDownClickSftbtn1()
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    RECT rect;
    m_Button1.GetWindowRect(&rect);     // get button position
    m_Button1.SendMessage(WM_CANCELMODE);// required before invoking popup menu
    ShowMenu(rect.right, rect.bottom);  // display the menu
}

void CPopupDlg::OnClickSftbtn2()
{
    // Invoke the default action, because the button was clicked
    SendMessage(WM_COMMAND, IDM_DEFAULTACTION);
}

void CPopupDlg::OnDropDownClickSftbtn2()
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    RECT rect;
    m_Button2.GetWindowRect(&rect);     // get button position

    // we want the button border while the menu is displayed
    // so we need to override the border style temporarily
    VARIANT_BOOL vb;
    m_vButton2->get_BorderAlways(&vb);
    m_vButton2->put_BorderAlways(VARIANT_TRUE);   // restore original border style

    m_Button2.SendMessage(WM_CANCELMODE);// required before invoking popup menu
    ShowMenu(rect.right, rect.bottom);  // display the menu

    m_vButton2->put_BorderAlways(vb);   // restore original border style

}

void CPopupDlg::OnClickSftbtn3()
{
    // Invoke the default action, because the button was clicked
    SendMessage(WM_COMMAND, IDM_DEFAULTACTION);
}

void CPopupDlg::OnDropDownClickSftbtn3()
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    RECT rect;
    m_Button3.GetWindowRect(&rect);     // get button position
    m_Button3.SendMessage(WM_CANCELMODE);// required before invoking popup menu
    ShowMenu(rect.right, rect.bottom);  // display the menu
}

void CPopupDlg::OnClickSftbtn4()
{
    // Invoke the default action, because the button was clicked
    SendMessage(WM_COMMAND, IDM_DEFAULTACTION);
}

void CPopupDlg::OnDropDownClickSftbtn4()
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    RECT rect;
    m_Button4.GetWindowRect(&rect);     // get button position
    m_Button4.SendMessage(WM_CANCELMODE);// required before invoking popup menu
    ShowMenu(rect.right, rect.bottom);  // display the menu
}


void CPopupDlg::OnClickSftbtn5()
{
    // Invoke the default action, because the button was clicked
    SendMessage(WM_COMMAND, IDM_DEFAULTACTION);
}

void CPopupDlg::OnDropDownClickSftbtn5()
{
    // The drop down button was clicked. Display a popup menu
    // right-aligned with the right edge of the button
    RECT rect;
    m_Button5.GetWindowRect(&rect);     // get button position
    m_Button5.SendMessage(WM_CANCELMODE);// required before invoking popup menu
    ShowMenu(rect.right, rect.bottom);  // display the menu
}

void CPopupDlg::OnDefaultAction()
{
    ::MessageBox(NULL, _T("Default Action selected"), _T("SftButton/OCX Sample"), MB_OK|MB_TASKMODAL);
}

void CPopupDlg::OnAction1()
{
    ::MessageBox(NULL, _T("Action 1 selected"), _T("SftButton/OCX Sample"), MB_OK|MB_TASKMODAL);
}

void CPopupDlg::OnAction2()
{
    ::MessageBox(NULL, _T("Action 2 selected"), _T("SftButton/OCX Sample"), MB_OK|MB_TASKMODAL);
}

void CPopupDlg::OnAction3()
{
    ::MessageBox(NULL, _T("Action 3 selected"), _T("SftButton/OCX Sample"), MB_OK|MB_TASKMODAL);
}

void CPopupDlg::ShowMenu(int x, int y)
{
    // Display the popup menu
    CMenu* pPopup = m_Menu.GetSubMenu(0); // get the popup menu
    pPopup->TrackPopupMenu(TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, x, y, this);
}

BEGIN_EVENTSINK_MAP(CPopupDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CPopupDlg)
    ON_EVENT(CPopupDlg, IDC_SFTBTN1, 2 /* Click */, OnClickSftbtn1, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN1, 4 /* DropDownClick */, OnDropDownClickSftbtn1, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN2, 2 /* Click */, OnClickSftbtn2, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN3, 2 /* Click */, OnClickSftbtn3, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN3, 4 /* DropDownClick */, OnDropDownClickSftbtn3, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN4, 2 /* Click */, OnClickSftbtn4, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN4, 4 /* DropDownClick */, OnDropDownClickSftbtn4, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN2, 4 /* DropDownClick */, OnDropDownClickSftbtn2, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN5, 2 /* Click */, OnClickSftbtn5, VTS_NONE)
    ON_EVENT(CPopupDlg, IDC_SFTBTN5, 4 /* DropDownClick */, OnDropDownClickSftbtn5, VTS_NONE)
    //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()


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