Click here to Skip to main content
15,892,809 members
Articles / Multimedia / DirectX

A DirectX Wrapper

Rate me:
Please Sign up or sign in to vote.
4.69/5 (20 votes)
18 Jul 200333 min read 181.2K   4.3K   66  
A DirectX Wrapper
// ButtonPropDialog.cpp : implementation file
//

#include "stdafx.h"
#include "GuiEdit.h"
#include "ButtonPropDialog.h"
#include "Colorpickerdlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CButtonPropDialog dialog


CButtonPropDialog::CButtonPropDialog(CWnd* pParent /*=NULL*/, int buttonType)
	: CDialog(CButtonPropDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CButtonPropDialog)
	m_isProportional = FALSE;
	m_isSerif = FALSE;
	m_fileName = _T("");
	m_fontWeight = 0;
	m_fontHeight = 0;
	m_height = 0;
	m_hotKey = _T("");
	m_text = _T("");
	m_width = 0;
	m_X = 0;
	m_xOff = 0;
	m_Y = 0;
	m_yOff = 0;
	m_ID = 0;
	m_isNoFrame = FALSE;
	m_isInvisible = FALSE;
	//}}AFX_DATA_INIT

    m_buttonType=buttonType;
}


void CButtonPropDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CButtonPropDialog)
	DDX_Check(pDX, IDC_CHECK_PROPORTIONAL, m_isProportional);
	DDX_Check(pDX, IDC_CHECK_SERIF, m_isSerif);
	DDX_Text(pDX, IDC_EDIT_FILENAME, m_fileName);
	DDX_Text(pDX, IDC_EDIT_FONTWEIGHT, m_fontWeight);
	DDV_MinMaxInt(pDX, m_fontWeight, 0, 1000);
	DDX_Text(pDX, IDC_EDIT_FONTHEIGHT, m_fontHeight);
	DDV_MinMaxInt(pDX, m_fontHeight, 6, 72);
	DDX_Text(pDX, IDC_EDIT_HEIGHT, m_height);
	DDV_MinMaxInt(pDX, m_height, 8, 32000);
	DDX_Text(pDX, IDC_EDIT_HOTKEY, m_hotKey);
	DDV_MaxChars(pDX, m_hotKey, 255);
	DDX_Text(pDX, IDC_EDIT_TEXT, m_text);
	DDV_MaxChars(pDX, m_text, 255);
	DDX_Text(pDX, IDC_EDIT_WIDTH, m_width);
	DDV_MinMaxInt(pDX, m_width, 8, 32000);
	DDX_Text(pDX, IDC_EDIT_X, m_X);
	DDX_Text(pDX, IDC_EDIT_XOFF, m_xOff);
	DDX_Text(pDX, IDC_EDIT_Y, m_Y);
	DDX_Text(pDX, IDC_EDIT_YOFF, m_yOff);
	DDX_Text(pDX, IDC_EDIT_ID, m_ID);
	DDX_Check(pDX, IDC_CHECK_NOFRAME, m_isNoFrame);
	DDX_Check(pDX, IDC_CHECK_INVISIBLE, m_isInvisible);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CButtonPropDialog, CDialog)
	//{{AFX_MSG_MAP(CButtonPropDialog)
	ON_WM_PAINT()
	ON_EN_KILLFOCUS(IDC_EDIT_FONTHEIGHT, OnKillfocusEditFontheight)
	ON_EN_KILLFOCUS(IDC_EDIT_FONTWEIGHT, OnKillfocusEditFontweight)
	ON_BN_CLICKED(IDC_CHECK_PROPORTIONAL, OnCheckProportional)
	ON_BN_CLICKED(IDC_CHECK_SERIF, OnCheckSerif)
	ON_BN_CLICKED(IDC_BUTTON_DARK, OnButtonDark)
	ON_BN_CLICKED(IDC_BUTTON_FIT, OnButtonFit)
	ON_BN_CLICKED(IDC_BUTTON_HOT, OnButtonHot)
	ON_BN_CLICKED(IDC_BUTTON_LIGHT, OnButtonLight)
	ON_BN_CLICKED(IDC_BUTTON_CENTRE_BITMAP, OnButtonCentreBitmap)
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_MEDIUM, OnButtonMedium)
	ON_BN_CLICKED(IDC_BUTTON_TEXT, OnButtonText)
	ON_WM_SETCURSOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CButtonPropDialog message handlers

void CButtonPropDialog::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
    RECT rect;
    HBRUSH brush;
    HFONT font;

	// Do not call CDialog::OnPaint() for painting messages
    TRACE("[defdialog on paint]\n");

    rect.left=25;
    rect.top=65;
    rect.right=95;
    rect.bottom=80;
    brush=CreateSolidBrush(m_darkColour);
    dc.SelectObject(brush);
    dc.Rectangle(&rect);
    DeleteObject(brush);

    rect.left+=83;
    rect.right+=83;
    brush=CreateSolidBrush(m_mediumColour);
    dc.SelectObject(brush);
    dc.Rectangle(&rect);
    DeleteObject(brush);

    rect.left+=83;
    rect.right+=83;
    brush=CreateSolidBrush(m_lightColour);
    dc.SelectObject(brush);
    dc.Rectangle(&rect);
    DeleteObject(brush);

    rect.left+=97;
    rect.right+=97;
    brush=CreateSolidBrush(m_textColour);
    dc.SelectObject(brush);
    dc.Rectangle(&rect);
    DeleteObject(brush);

    rect.left+=83;
    rect.right+=83;
    brush=CreateSolidBrush(m_hilightColour);
    dc.SelectObject(brush);
    dc.Rectangle(&rect);
    DeleteObject(brush);

    font=gb_DX.CreateFont(m_fontHeight, m_fontWeight, m_isProportional, m_isSerif);
    if ( font )
        {
        CRgn rgn;

        rect.left=25;
        rect.top=270;
        rect.right=450;
        rect.bottom=500;
        rgn.CreateRectRgnIndirect(&rect);
        dc.SelectClipRgn(&rgn);
        dc.SelectObject(font);
        dc.SetTextColor(0);
        //dc.SetBkColor(RGB(100,100,100));
        dc.SetBkMode(TRANSPARENT);
        dc.TextOut(rect.left+2, rect.top+2, "ABCD abcd GJY gjy /?#$%");

        DeleteObject(rgn);
        //DeleteObject(font);
        }
    else
        {
        TRACE("[def values dialog onpaint] failed to create font\n");
        }

}

void CButtonPropDialog::OnKillfocusEditFontheight() 
{
    UpdateData(TRUE);
    InvalidateRgn(NULL);
}

void CButtonPropDialog::OnKillfocusEditFontweight() 
{
    UpdateData(TRUE);
    InvalidateRgn(NULL);
}

void CButtonPropDialog::OnCheckProportional() 
{
    UpdateData(TRUE);
    InvalidateRgn(NULL);
}

void CButtonPropDialog::OnCheckSerif() 
{
    UpdateData(TRUE);
    InvalidateRgn(NULL);
}

void CButtonPropDialog::OnButtonDark() 
{
    COLORREF c=0;
    CColorPickerDlg colorPick(m_darkColour, NULL);	

    if ( colorPick.DoModal() == IDOK )
        {
        m_darkColour=colorPick.GetColor();
        InvalidateRgn(NULL);
        }
}

void CButtonPropDialog::OnButtonFit() 
{

    if ( m_buttonType == GUI_HAS_BITMAP )
        {
        gsdxBitmap_t *bitmap;
        RECT r;
        int w, h;

        ZeroMemory(&r, sizeof(r));
        bitmap=gb_DX.LoadBitmap(m_fileName, &r);
        if ( bitmap )
            {
            UpdateData();
            w=r.right - r.left;
            h=r.bottom - r.top;

            m_width=w + 6;
            m_height=h + 8;

            m_xOff=4;
            m_yOff=4;

            delete bitmap;
            UpdateData(FALSE);
            }
        else
            {
            CString str;
        
            str.Format("Error loading bitmap: %s", m_fileName);
            MessageBox(str, "Error", MB_ICONEXCLAMATION);
            }
        }

}

void CButtonPropDialog::OnButtonHot() 
{
    COLORREF c=0;
    CColorPickerDlg colorPick(m_hilightColour, NULL);	

    if ( colorPick.DoModal() == IDOK )
        {
        m_hilightColour=colorPick.GetColor();
        InvalidateRgn(NULL);
        }
}

void CButtonPropDialog::OnButtonLight() 
{
    COLORREF c=0;
    CColorPickerDlg colorPick(m_lightColour, NULL);	

    if ( colorPick.DoModal() == IDOK )
        {
        m_lightColour=colorPick.GetColor();
        InvalidateRgn(NULL);
        }
}

// actually, centre bitmap or text
void CButtonPropDialog::OnButtonCentreBitmap() 
{
    if ( m_buttonType == GUI_HAS_TEXT )
        {
        HFONT font;
        SIZE size;
        int i, len;
        char temp[GUI_MAX_TEXT+1], *t2;

        UpdateData();
        font=gb_DX.CreateFont(m_fontHeight, m_fontWeight, m_isProportional, m_isSerif);
        i=0;
        t2=temp;
        len=m_text.GetLength();
        while ( i < len )
            {
            if ( m_text[i] == '^' )
                i++;
            else
                {
                *t2++=m_text[i];
                i++;
                }
            }
        *t2=0;
        gb_DX.GetTextSize(font, temp, &size);

        if ( size.cx > m_width )
            m_width=size.cx + 8;
        if ( size.cy > m_height )
            m_height=size.cy + 8;

        m_xOff=(m_width - size.cx)/2;
        m_yOff=(m_height - size.cy)/2;
        UpdateData(FALSE);
        }
    else
        {
        if ( m_buttonType == GUI_HAS_BITMAP )
            {
            gsdxBitmap_t *bitmap;
            RECT r;
            int w, h;

            ZeroMemory(&r, sizeof(r));
            bitmap=gb_DX.LoadBitmap(m_fileName, &r);
            if ( bitmap )
                {
                UpdateData();
                w=r.right - r.left;
                h=r.bottom - r.top;

                if ( w > m_width )
                    m_width=w + 8;
                if ( h > m_height )
                    m_height=h + 8;

                m_xOff=(m_width - w)/2;
                m_yOff=(m_height - h)/2;

                delete bitmap;
                UpdateData(FALSE);
                }
            else
                {
                CString str;
                
                str.Format("Error loading bitmap: %s", m_fileName);
                MessageBox(str, "Error", MB_ICONEXCLAMATION);
                }
            }

        }
	
}

void CButtonPropDialog::OnButtonBrowse() 
{
    // open, def extension, filename, flags, filter
    CFileDialog fdiag(TRUE, NULL, m_fileName, OFN_FILEMUSTEXIST|OFN_NOCHANGEDIR|OFN_PATHMUSTEXIST, NULL);

    if ( fdiag.DoModal() == IDOK )
        {
        m_fileName=fdiag.GetFileName();
        UpdateData(FALSE);
        }
}

void CButtonPropDialog::OnButtonMedium() 
{
    COLORREF c=0;
    CColorPickerDlg colorPick(m_mediumColour, NULL);	

    if ( colorPick.DoModal() == IDOK )
        {
        m_mediumColour=colorPick.GetColor();
        InvalidateRgn(NULL);
        }
}

void CButtonPropDialog::OnButtonText() 
{
    COLORREF c=0;
    CColorPickerDlg colorPick(m_textColour, NULL);	

    if ( colorPick.DoModal() == IDOK )
        {
        m_textColour=colorPick.GetColor();
        InvalidateRgn(NULL);
        }
}

BOOL CButtonPropDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
    CButton *dlg;

    if ( m_guiFlag == GUI_NORMAL )
        {
        dlg=(CButton *)GetDescendantWindow(IDC_RADIO_NORMAL);
        dlg->SetCheck(1);
        }
    else
        {
        if ( m_guiFlag == GUI_TOUCH )
            {
            dlg=(CButton *)GetDescendantWindow(IDC_RADIO_TOUCH);
            dlg->SetCheck(1);
            }
        else
            {
            if ( m_guiFlag == GUI_TOGGLE )
                {
                dlg=(CButton *)GetDescendantWindow(IDC_RADIO_TOGGLE);
                dlg->SetCheck(1);
                }
            }
        }

    if ( m_buttonType == GUI_HAS_TEXT )
        {
        dlg=(CButton *)GetDescendantWindow(IDC_BUTTON_BROWSE);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_BUTTON_FIT);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_EDIT_FILENAME);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_EDIT_TYPE);
        dlg->SetWindowText("Text");
        }
    else
        {
        dlg=(CButton *)GetDescendantWindow(IDC_EDIT_FONTHEIGHT);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_EDIT_FONTWEIGHT);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_EDIT_TEXT);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_CHECK_PROPORTIONAL);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_CHECK_SERIF);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_BUTTON_TEXT);
        dlg->EnableWindow(FALSE);
        dlg=(CButton *)GetDescendantWindow(IDC_BUTTON_HOT);
        dlg->EnableWindow(FALSE);
        if ( m_buttonType == GUI_HAS_BITMAP )
            {
            dlg=(CButton *)GetDescendantWindow(IDC_EDIT_TYPE);
            dlg->SetWindowText("Bitmap");
            }
        else
            {
            if ( m_buttonType == GUI_HAS_SPRITE )
                {
                dlg=(CButton *)GetDescendantWindow(IDC_EDIT_TYPE);
                dlg->SetWindowText("Sprite");
                }
            }
        }

    UpdateData(FALSE);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CButtonPropDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Canada Canada
Professional Programmer living in Beautiful Vancouver, BC, Canada.

Comments and Discussions