Author Topic: ARX and MFC  (Read 6791 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6964
  • AKA Daniel
Re: ARX and MFC
« Reply #15 on: January 09, 2010, 08:14:08 AM »
Ok back to the code,
I added a method to handle displaying and validating the text in the edit control
I added a getxxx method to the pick button event (note the comments)
I added an OnOK method as it's a good place to validate the data
I also added a CString m_strWidth to hold the width as a string

here is the class

.h
Code: [Select]
#pragma once

//-----------------------------------------------------------------------------
#include "acui.h"
#include "afxwin.h"

//-----------------------------------------------------------------------------
class MyDialog : public CAcUiDialog {
DECLARE_DYNAMIC (MyDialog)

public:
MyDialog (CWnd *pParent =NULL, HINSTANCE hInstance =NULL) ;

enum { IDD = IDD_MYDIALOG} ;

protected:
  virtual void DoDataExchange (CDataExchange *pDX) ;
  virtual BOOL OnInitDialog();
  virtual void DisplayWidth(double width);
  virtual bool ValidateWidth();
  virtual void OnOK();
  afx_msg LRESULT OnAcadKeepFocus (WPARAM, LPARAM) ;
  afx_msg void OnBnClickedButtonpickitem();
  afx_msg void OnEnKillfocusEdit1();

DECLARE_MESSAGE_MAP()
public:
  CAcUiPickButton m_buttonPick;
  CAcUiNumericEdit m_TextBoxWidth;
  CString m_strWidth;
} ;

.cpp
Code: [Select]
#include "StdAfx.h"
#include "resource.h"
#include "MyDialog.h"

//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC (MyDialog, CAcUiDialog)

BEGIN_MESSAGE_MAP(MyDialog, CAcUiDialog)
  ON_MESSAGE(WM_ACAD_KEEPFOCUS, OnAcadKeepFocus)
  ON_BN_CLICKED(IDC_BUTTONPICKITEM, &MyDialog::OnBnClickedButtonpickitem)
  ON_EN_KILLFOCUS(IDC_EDIT1, &MyDialog::OnEnKillfocusEdit1)
END_MESSAGE_MAP()

//-----------------------------------------------------------------------------
MyDialog::MyDialog (CWnd *pParent /*=NULL*/, HINSTANCE hInstance /*=NULL*/) :
                    CAcUiDialog (MyDialog::IDD, pParent, hInstance) {
}

//-----------------------------------------------------------------------------
void MyDialog::DoDataExchange (CDataExchange *pDX) {
  CAcUiDialog::DoDataExchange (pDX) ;
  DDX_Control(pDX, IDC_BUTTONPICKITEM, m_buttonPick);
  DDX_Control(pDX, IDC_EDIT1, m_TextBoxWidth);
}

//-----------------------------------------------------------------------------
//----- Needed for modeless dialogs to keep focus.
//----- Return FALSE to not keep the focus, return TRUE to keep the focus
LRESULT MyDialog::OnAcadKeepFocus (WPARAM, LPARAM) {
  return (TRUE) ;
}

void MyDialog::OnBnClickedButtonpickitem()
{
  ads_point pt;
  ads_real width;

  BeginEditorCommand(); // Hide the dialog and give control to the editor

  // standard getxxx function
  if (acedGetPoint(NULL, _T("\nPick first point: "), pt) == RTNORM)
  {
    if(acedGetDist(pt,_T("\nPick second Point: "),&width))
    {
      CompleteEditorCommand(); // show our dialog on success
      DisplayWidth(width);
    }
  }
  else // otherwise cancel the command (including the dialog)
  {
    CancelEditorCommand();
  }
}

BOOL MyDialog::OnInitDialog()
{
  SetDialogName(_T("ArxMFC:MyDialog")); // for persisting out dialog posistion
  CAcUiDialog::OnInitDialog();
  m_buttonPick.AutoLoad();
  DisplayWidth(0.0); // a little something for default
  return TRUE;
}

void MyDialog::OnEnKillfocusEdit1()
{
  m_TextBoxWidth.Convert();
  m_TextBoxWidth.GetWindowText(m_strWidth);
}

void MyDialog::DisplayWidth( double width )
{
  m_strWidth.Format(_T("%g"),width);
  m_TextBoxWidth.SetWindowText(m_strWidth);
  m_TextBoxWidth.Convert();
}

bool MyDialog::ValidateWidth()
{
  if (!m_TextBoxWidth.Validate())
  {
    return false;
  }
  return true;
}

void MyDialog::OnOK()
{
  if (!ValidateWidth())
  {
    AfxMessageBox(_T("Sorry, Value out of desired range. Try again or Cancel"));
    m_TextBoxWidth.SetFocus();
    return;
  }
}
« Last Edit: January 09, 2010, 10:49:20 AM by Daniel »

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6964
  • AKA Daniel
Re: ARX and MFC
« Reply #16 on: January 09, 2010, 08:16:58 AM »
Remember the command we made? well lets go back and add a little code to lshow our dialog
 

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6964
  • AKA Daniel
Re: ARX and MFC
« Reply #17 on: January 09, 2010, 08:20:09 AM »
Compile and run, test the validate code  :laugh:

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6964
  • AKA Daniel
Re: ARX and MFC
« Reply #18 on: January 09, 2010, 08:24:25 AM »
Ya see, doing basic stuff in MFC is a walk in the park once you get the hang of it!  :laugh:

gskelly

  • Newt
  • Posts: 185
Re: ARX and MFC
« Reply #19 on: January 09, 2010, 09:22:12 AM »
If threads can be made to stay at the top this would be a nice one to have there!
Bricscad v12

frtfff

  • Bull Frog
  • Posts: 216
Re: ARX and MFC
« Reply #20 on: January 09, 2010, 10:01:39 AM »
 :kewl:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: ARX and MFC
« Reply #21 on: January 09, 2010, 07:13:44 PM »

Bookmarked !

Thanks Dan.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--