Hide

SftButton/OCX 3.0 - Button Control

Display
Print

Toggle Sample (C++)

This sample illustrates toggle buttons.

The source code is located at C:\Program Files (x86)\Softelvdm\SftButton OCX 3.0\Samples\VC++\Toggle\ToggleDlg.cpp or C:\Program Files\Softelvdm\SftButton OCX 3.0\Samples\VC++\Toggle\ToggleDlg.cpp (on 32-bit Windows versions).

// ToggleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Toggle.h"
#include "ToggleDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CToggleDlg dialog

CToggleDlg::CToggleDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CToggleDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CToggleDlg)
        // 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);
}

void CToggleDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CToggleDlg)
    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);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CToggleDlg, CDialog)
    //{{AFX_MSG_MAP(CToggleDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CToggleDlg message handlers

BOOL CToggleDlg::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_vButton1 = m_Button1.GetControlUnknown();
    m_vButton2 = m_Button2.GetControlUnknown();
    m_vButton3 = m_Button3.GetControlUnknown();
    m_vButton4 = m_Button4.GetControlUnknown();

    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 CToggleDlg::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();
    }
}

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

void CToggleDlg::ChangeBothPictures()
{
    // Change the pictures based on the button's Pressed status
    CBitmap bmp;

    PICTDESC pictDesc;
    pictDesc.cbSizeofstruct = sizeof(pictDesc);
    pictDesc.picType = PICTYPE_BITMAP;
    if (m_vButton3->Pressed)
        bmp.LoadBitmap(IDB_ON);
    else
        bmp.LoadBitmap(IDB_OFF);
    pictDesc.bmp.hbitmap = (HBITMAP) bmp.m_hObject;

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

    IPictureDispPtr pPicDisp = pPic;

    ISftPictureObjectPtr pImg1;
    hr = m_vButton3->get_Image1(&pImg1);
    ASSERT(SUCCEEDED(hr));

    hr = pImg1->put_Picture(pPicDisp);
    ASSERT(SUCCEEDED(hr));

    bmp.DeleteObject();
    pPic = NULL;

    if (m_vButton3->Pressed)
        bmp.LoadBitmap(IDB_OFF);
    else
        bmp.LoadBitmap(IDB_ON);
    pictDesc.bmp.hbitmap = (HBITMAP) bmp.m_hObject;

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

    ISftPictureObjectPtr pImg2;
    hr = m_vButton3->get_Image2(&pImg2);
    ASSERT(SUCCEEDED(hr));

    pPicDisp = pPic;
    hr = pImg2->put_Picture(pPicDisp);
    ASSERT(SUCCEEDED(hr));

}

void CToggleDlg::OnClickSftbtn3()
{
    ChangeBothPictures();
}

void CToggleDlg::OnDblClickSftbtn3()
{
    ChangeBothPictures();
}

BEGIN_EVENTSINK_MAP(CToggleDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CToggleDlg)
    ON_EVENT(CToggleDlg, IDC_SFTBTN3, 2 /* Click */, OnClickSftbtn3, VTS_NONE)
    ON_EVENT(CToggleDlg, IDC_SFTBTN3, 3 /* DblClick */, OnDblClickSftbtn3, VTS_NONE)
    //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

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