I'm a total beginner with MFC based programs, and am having trouble with identifier definitions in the program I'm currently working on. I've posted the problematic snippet below. The rest of the program follows it.
DDX_Text(pDX, IDC_BUTTON_CLICKS, m_EchoText);
DDX_Text(pDX, IDC_V_SLIDER_ECHO, m_VSliderEcho);
DDX_Control(pDX, IDC_V_SLIDER_BAR, m_VSliderBar);
//The above lines work. Below here doesn't work, I'm getting a message saying IDC_H_SLIDER_BAR and IDC_H_SLIDER_ECHO aren't defined.
DDX_Control(pDX, IDC_H_SLIDER_BAR, m_HSliderBar);
DDX_Text(pDX, IDC_H_SLIDER_ECHO, m_HSliderEcho);
Here is the full implementation file:
// MFCApplication1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "MFCApplication1.h"
#include "MFCApplication1Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMFCApplication1Dlg dialog
CMFCApplication1Dlg::CMFCApplication1Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CMFCApplication1Dlg::IDD, pParent)
, m_EchoText(_T(""))
, m_VSliderEcho(_T(""))
, m_HSliderEcho(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_BUTTON_CLICKS, m_EchoText);
DDX_Text(pDX, IDC_V_SLIDER_ECHO, m_VSliderEcho);
DDX_Control(pDX, IDC_V_SLIDER_BAR, m_VSliderBar);
DDX_Control(pDX, IDC_H_SLIDER_BAR, m_HSliderBar);
DDX_Text(pDX, IDC_H_SLIDER_ECHO, m_HSliderEcho);
}
BEGIN_MESSAGE_MAP(CMFCApplication1Dlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_ADD, &CMFCApplication1Dlg::OnBnClickedBtnAdd)
ON_WM_VSCROLL()
ON_WM_HSCROLL()
END_MESSAGE_MAP()
// CMFCApplication1Dlg message handlers
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
m_OkCount = 0;
m_VSliderBar.SetRange(0, 100, TRUE);
m_VSliderBar.SetPos(0);
m_VSliderEcho.Format(_T("%d"), 0);
m_HSliderBar.SetRange(0, 100, TRUE);
m_HSliderBar.SetPos(0);
m_HSliderEcho.Format(_T("%d"), 0);
// 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
// TODO: Add extra initialization here
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 CMFCApplication1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMFCApplication1Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMFCApplication1Dlg::OnBnClickedBtnAdd()
{
// TODO: Add your control notification handler code here
m_OkCount++;
m_EchoText.Format(_T("%d"), m_OkCount);
// without UpdateData() status area will _NOT_ be updated.
UpdateData(FALSE);
}
void CMFCApplication1Dlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
if (pScrollBar == (CScrollBar *)&m_VSliderBar)
{
int value = m_VSliderBar.GetPos();
m_VSliderEcho.Format(_T("%d"), value);
UpdateData(FALSE);
}
else{
CDialogEx::OnVScroll(nSBCode, nPos, pScrollBar);
}
}
void CMFCApplication1Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if (pScrollBar == (CScrollBar *)&m_HSliderBar)
{
int value = m_HSliderBar.GetPos();
m_HSliderEcho.Format(_T("%d"), value);
UpdateData(FALSE);
}
else{
CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
}
And here is the .h file:
// MFCApplication1Dlg.h : header file
//
#pragma once
#include "afxcmn.h"
// CMFCApplication1Dlg dialog
class CMFCApplication1Dlg : public CDialogEx
{
// Construction
public:
CMFCApplication1Dlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
enum { IDD = IDD_MFCAPPLICATION1_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedBtnAdd();
CSliderCtrl m_VSliderBar;
CSliderCtrl m_HSliderBar;
private:
int m_OkCount;
CString m_EchoText;
CString m_VSliderEcho;
CString m_HSliderEcho;
public:
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
};
Probably IDC_H_SLIDER_BAR
and IDC_H_SLIDER_ECHO
are not defined in Resource.h
(case sensitive!)