home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / maths3 / mathsctl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  8.9 KB  |  333 lines

  1. // MathsCtl.cpp : Implementation of the CMaths3Ctrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "Maths3.h"
  5. #include "MathsCtl.h"
  6. #include "MathsPpg.h"
  7.  
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15.  
  16. IMPLEMENT_DYNCREATE(CMaths3Ctrl, COleControl)
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Message map
  21.  
  22. BEGIN_MESSAGE_MAP(CMaths3Ctrl, COleControl)
  23.     //{{AFX_MSG_MAP(CMaths3Ctrl)
  24.     ON_WM_LBUTTONDOWN()
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Dispatch map
  31.  
  32. BEGIN_DISPATCH_MAP(CMaths3Ctrl, COleControl)
  33.     //{{AFX_DISPATCH_MAP(CMaths3Ctrl)
  34.     DISP_PROPERTY_NOTIFY(CMaths3Ctrl, "Opposite", m_bOpposite, OnOppositeChanged, VT_BOOL)
  35.     DISP_STOCKPROP_BACKCOLOR()
  36.     DISP_STOCKPROP_FORECOLOR()
  37.     //}}AFX_DISPATCH_MAP
  38.     DISP_FUNCTION_ID(CMaths3Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  39. END_DISPATCH_MAP()
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Event map
  44.  
  45. BEGIN_EVENT_MAP(CMaths3Ctrl, COleControl)
  46.     //{{AFX_EVENT_MAP(CMaths3Ctrl)
  47.     EVENT_CUSTOM("Outside", FireOutside, VTS_NONE)
  48.     EVENT_CUSTOM("Inside", FireInside, VTS_XPOS_PIXELS  VTS_YPOS_PIXELS)
  49.     //}}AFX_EVENT_MAP
  50. END_EVENT_MAP()
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // Property pages
  55.  
  56. BEGIN_PROPPAGEIDS(CMaths3Ctrl, 1)
  57.     PROPPAGEID(CMaths3PropPage::guid)
  58. END_PROPPAGEIDS(CMaths3Ctrl)
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Initialize class factory and guid
  63.  
  64. IMPLEMENT_OLECREATE_EX(CMaths3Ctrl, "MATHS.Maths3Ctrl.1",
  65.     0x182CB003, 0xbeed, 0x11ce, 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf)
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Type library ID and version
  70.  
  71. IMPLEMENT_OLETYPELIB(CMaths3Ctrl, _tlid, _wVerMajor, _wVerMinor)
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Interface IDs
  76.  
  77. const IID BASED_CODE IID_DMaths3 =
  78.         { 0x182cb001, 0xbeed, 0x11ce, { 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf } };
  79. const IID BASED_CODE IID_DMaths3Events =
  80.         { 0x182cb002, 0xbeed, 0x11ce, { 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf } };
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Control type information
  85.  
  86. static const DWORD BASED_CODE _dwMaths3OleMisc =
  87.     OLEMISC_ACTIVATEWHENVISIBLE |
  88.     OLEMISC_SETCLIENTSITEFIRST |
  89.     OLEMISC_INSIDEOUT |
  90.     OLEMISC_CANTLINKINSIDE |
  91.     OLEMISC_RECOMPOSEONRESIZE;
  92.  
  93. IMPLEMENT_OLECTLTYPE(CMaths3Ctrl, IDS_Maths3, _dwMaths3OleMisc)
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CMaths3Ctrl::CMaths3CtrlFactory::UpdateRegistry -
  98. // Adds or removes system registry entries for CMaths3Ctrl
  99.  
  100. BOOL CMaths3Ctrl::CMaths3CtrlFactory::UpdateRegistry(BOOL bRegister)
  101. {
  102.     if (bRegister)
  103.         return AfxOleRegisterControlClass(
  104.             AfxGetInstanceHandle(),
  105.             m_clsid,
  106.             m_lpszProgID,
  107.             IDS_Maths3,
  108.             IDB_Maths3,
  109.             FALSE,                      //  Not insertable
  110.             _dwMaths3OleMisc,
  111.             _tlid,
  112.             _wVerMajor,
  113.             _wVerMinor);
  114.     else
  115.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  116. }
  117.  
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CMaths3Ctrl::CMaths3Ctrl - Constructor
  121.  
  122. CMaths3Ctrl::CMaths3Ctrl()
  123. {
  124.     InitializeIIDs(&IID_DMaths3, &IID_DMaths3Events);
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CMaths3Ctrl::~CMaths3Ctrl - Destructor
  130.  
  131. CMaths3Ctrl::~CMaths3Ctrl()
  132. {
  133. }
  134.  
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CMaths3Ctrl::OnDraw - Drawing function
  138.  
  139. #define X(x) (int)(xLeft + (x)*xScale/100)    // Scaling macros
  140. #define Y(y) (int)(yTop + (y)*yScale/100)    // so scale is 0 - 100
  141. #define CX(x) (int)((x)*xScale/100)
  142. #define CY(y) (int)((y)*yScale/100)
  143.  
  144. void CMaths3Ctrl::OnDraw(
  145.             CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
  146. {
  147.     long xLeft = rcBounds.left;            // Use with scaling macros
  148.     long yTop = rcBounds.top;    
  149.     long xScale = rcBounds.Width();
  150.     long yScale = rcBounds.Height();    
  151.  
  152.     int iPenWidth = max(CX(5), CY(5)); // Pen width based on control size
  153.     CBrush brushBack;                   // Background brush
  154.     CPen penFore(PS_SOLID, iPenWidth, TranslateColor(GetForeColor()));
  155.     COLORREF crBack = TranslateColor(GetBackColor());    //Use BackColor
  156.     brushBack.CreateSolidBrush(crBack);                 //Stock Property
  157.  
  158.     pdc->FillRect(rcBounds, &brushBack);                // Clear background        
  159.  
  160.     CPen* pPenSave = pdc->SelectObject(&penFore);
  161.     CBrush* pBrushSave = pdc->SelectObject(&brushBack);
  162.  
  163.     if (m_bOpposite)
  164.     {
  165.         // Draw a circle - 0
  166.         pdc->Ellipse(xLeft + CX(10), yTop + CY(10), xLeft + xScale - CX(10), yTop + yScale - CY(10));
  167.     }
  168.     else
  169.     {
  170.         // Draw a vertical line
  171.         pdc->MoveTo(xLeft + (xScale / 2), yTop + CY(10));
  172.         pdc->LineTo(xLeft + (xScale / 2), yTop + yScale - CY(10));
  173.     }
  174.  
  175.     pdc->SelectObject(pPenSave);
  176.     pdc->SelectObject(pBrushSave);
  177. }
  178.  
  179.  
  180. /////////////////////////////////////////////////////////////////////////////
  181. // CMaths3Ctrl::DoPropExchange - Persistence support
  182.  
  183. void CMaths3Ctrl::DoPropExchange(CPropExchange* pPX)
  184. {
  185.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  186.     COleControl::DoPropExchange(pPX);
  187.  
  188.     PX_Bool(pPX, CString((LPCTSTR)IDS_OPPOSITE), m_bOpposite, FALSE);
  189.  
  190. }
  191.  
  192.  
  193. /////////////////////////////////////////////////////////////////////////////
  194. // CMaths3Ctrl::OnResetState - Reset control to default state
  195.  
  196. void CMaths3Ctrl::OnResetState()
  197. {
  198.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  199. }
  200.  
  201.  
  202. /////////////////////////////////////////////////////////////////////////////
  203. // CMaths3Ctrl message handlers
  204.  
  205. void CMaths3Ctrl::OnLButtonDown(UINT nFlags, CPoint point) 
  206. {
  207.     CRect rcBounds;
  208.     GetClientRect(&rcBounds);
  209.  
  210.     long xLeft = rcBounds.left;            // Use with scaling macros
  211.     long yTop = rcBounds.top;    
  212.     long xScale = rcBounds.Width();
  213.     long yScale = rcBounds.Height();    
  214.  
  215.     if (m_bOpposite)
  216.     {
  217.         // Is point on circle
  218.         if (InEllipse(point, xLeft + CX(5), yTop + CY(5), xLeft + xScale - CX(5), yTop + yScale - CY(5))
  219.             && !InEllipse(point, xLeft + CX(15), yTop + CY(15), xLeft + xScale - CX(15), yTop + yScale - CY(15)))
  220.             FireInside((short)point.x, (short)point.y);
  221.         else
  222.             FireOutside();
  223.     }
  224.     else
  225.     {
  226.         // Is point on vertical line
  227.         if ((point.x >= xLeft + (xScale / 2) - CX(5)) && (point.x <= xLeft + (xScale / 2) + CX(5)))
  228.             FireInside((short)point.x, (short)point.y);
  229.         else
  230.             FireOutside();
  231.     }
  232.     
  233.     COleControl::OnLButtonDown(nFlags, point);
  234. }
  235.  
  236. //Simple Ellipse Hit-Testing
  237. BOOL CMaths3Ctrl::InEllipse(const CPoint& pt,
  238.     int x1, int y1, int x2, int y2)
  239. {
  240.     // Determine radii                       
  241.     double a = (x2 - x1) / 2;
  242.     double b = (y2 - y1) / 2;
  243.     
  244.     // Determine x, y
  245.     double x = pt.x - (x1 + x2) / 2;
  246.     double y = pt.y - (y1 + y2) / 2;
  247.     
  248.     // Apply ellipse formula
  249.     return ((x * x) / (a * a) + (y * y) / (b * b) <= 1);
  250. }
  251.  
  252. void CMaths3Ctrl::OnOppositeChanged() 
  253. {
  254.     InvalidateControl();
  255. }/////////////////////////////////////////////////////////////////////////////
  256. // CAboutDlg dialog
  257.  
  258. class CAboutDlg : public CDialog
  259. {
  260. // Construction
  261. public:
  262.     CAboutDlg(CWnd* pParent = NULL);   // standard constructor
  263.  
  264. // Dialog Data
  265.     //{{AFX_DATA(CAboutDlg)
  266.     enum { IDD = IDD_ABOUTBOX_Maths3 };
  267.         // NOTE: the ClassWizard will add data members here
  268.     //}}AFX_DATA
  269.  
  270.  
  271. // Overrides
  272.     // ClassWizard generated virtual function overrides
  273.     //{{AFX_VIRTUAL(CAboutDlg)
  274.     protected:
  275.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  276.     //}}AFX_VIRTUAL
  277.  
  278. // Implementation
  279. protected:
  280.  
  281.     // Generated message map functions
  282.     //{{AFX_MSG(CAboutDlg)
  283.     virtual BOOL OnInitDialog();
  284.     //}}AFX_MSG
  285.     DECLARE_MESSAGE_MAP()
  286. };
  287. /////////////////////////////////////////////////////////////////////////////
  288. // CAboutDlg dialog
  289.  
  290.  
  291. CAboutDlg::CAboutDlg(CWnd* pParent /*=NULL*/)
  292.     : CDialog(CAboutDlg::IDD, pParent)
  293. {
  294.     //{{AFX_DATA_INIT(CAboutDlg)
  295.         // NOTE: the ClassWizard will add member initialization here
  296.     //}}AFX_DATA_INIT
  297. }
  298.  
  299.  
  300. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  301. {
  302.     CDialog::DoDataExchange(pDX);
  303.     //{{AFX_DATA_MAP(CAboutDlg)
  304.         // NOTE: the ClassWizard will add DDX and DDV calls here
  305.     //}}AFX_DATA_MAP
  306. }
  307.  
  308.  
  309. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  310.     //{{AFX_MSG_MAP(CAboutDlg)
  311.     //}}AFX_MSG_MAP
  312. END_MESSAGE_MAP()
  313.  
  314. /////////////////////////////////////////////////////////////////////////////
  315. // CAboutDlg message handlers
  316.  
  317. BOOL CAboutDlg::OnInitDialog() 
  318. {
  319.     CDialog::OnInitDialog();
  320.     CenterWindow();    
  321.     return TRUE;  // return TRUE unless you set the focus to a control
  322.                   // EXCEPTION: OCX Property Pages should return FALSE
  323. }
  324.  
  325. /////////////////////////////////////////////////////////////////////////////
  326. // CMaths3Ctrl::AboutBox - Display an "About" box to the user
  327.  
  328. void CMaths3Ctrl::AboutBox()
  329. {
  330.     CAboutDlg about;
  331.     about.DoModal();
  332. }
  333.