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

  1. // Maths1Ctl.cpp : Implementation of the CMaths1Ctrl OLE control class.
  2.  
  3. #include "stdafx.h"
  4. #include "Maths1.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(CMaths1Ctrl, COleControl)
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Message map
  21.  
  22. BEGIN_MESSAGE_MAP(CMaths1Ctrl, COleControl)
  23.     //{{AFX_MSG_MAP(CMaths1Ctrl)
  24.     ON_WM_LBUTTONDOWN()
  25.     //}}AFX_MSG_MAP
  26. END_MESSAGE_MAP()
  27.  
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // Dispatch map
  31.  
  32. BEGIN_DISPATCH_MAP(CMaths1Ctrl, COleControl)
  33.     //{{AFX_DISPATCH_MAP(CMaths1Ctrl)
  34.     DISP_PROPERTY_NOTIFY(CMaths1Ctrl, "Opposite", m_bOpposite, OnOppositeChanged, VT_BOOL)
  35.     DISP_STOCKPROP_BACKCOLOR()
  36.     DISP_STOCKPROP_FORECOLOR()
  37.     //}}AFX_DISPATCH_MAP
  38.     DISP_FUNCTION_ID(CMaths1Ctrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
  39. END_DISPATCH_MAP()
  40.  
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // Event map
  44.  
  45. BEGIN_EVENT_MAP(CMaths1Ctrl, COleControl)
  46.     //{{AFX_EVENT_MAP(CMaths1Ctrl)
  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(CMaths1Ctrl, 1)
  57.     PROPPAGEID(CMaths1PropPage::guid)
  58. END_PROPPAGEIDS(CMaths1Ctrl)
  59.  
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // Initialize class factory and guid
  63.  
  64. IMPLEMENT_OLECREATE_EX(CMaths1Ctrl, "MATHS.Maths1Ctrl.1",
  65.     0x183CB003, 0xbeed, 0x11ce, 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf)
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Type library ID and version
  70.  
  71. IMPLEMENT_OLETYPELIB(CMaths1Ctrl, _tlid, _wVerMajor, _wVerMinor)
  72.  
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // Interface IDs
  76.  
  77. const IID BASED_CODE IID_DMaths1 =
  78.         { 0x183cb001, 0xbeed, 0x11ce, { 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf } };
  79. const IID BASED_CODE IID_DMaths1Events =
  80.         { 0x183cb002, 0xbeed, 0x11ce, { 0x96, 0x11, 0, 0xaa, 0, 0x4a, 0x75, 0xcf } };
  81.  
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Control type information
  85.  
  86. static const DWORD BASED_CODE _dwMaths1OleMisc =
  87.     OLEMISC_ACTIVATEWHENVISIBLE |
  88.     OLEMISC_SETCLIENTSITEFIRST |
  89.     OLEMISC_INSIDEOUT |
  90.     OLEMISC_CANTLINKINSIDE |
  91.     OLEMISC_RECOMPOSEONRESIZE;
  92.  
  93. IMPLEMENT_OLECTLTYPE(CMaths1Ctrl, IDS_Maths1, _dwMaths1OleMisc)
  94.  
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CMaths1Ctrl::CMaths1CtrlFactory::UpdateRegistry -
  98. // Adds or removes system registry entries for CMaths1Ctrl
  99.  
  100. BOOL CMaths1Ctrl::CMaths1CtrlFactory::UpdateRegistry(BOOL bRegister)
  101. {
  102.     if (bRegister)
  103.         return AfxOleRegisterControlClass(
  104.             AfxGetInstanceHandle(),
  105.             m_clsid,
  106.             m_lpszProgID,
  107.             IDS_Maths1,
  108.             IDB_Maths1,
  109.             FALSE,                      //  Not insertable
  110.             _dwMaths1OleMisc,
  111.             _tlid,
  112.             _wVerMajor,
  113.             _wVerMinor);
  114.     else
  115.         return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
  116. }
  117.  
  118.  
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CMaths1Ctrl::CMaths1Ctrl - Constructor
  121.  
  122. CMaths1Ctrl::CMaths1Ctrl()
  123. {
  124.     InitializeIIDs(&IID_DMaths1, &IID_DMaths1Events);
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // CMaths1Ctrl::~CMaths1Ctrl - Destructor
  130.  
  131. CMaths1Ctrl::~CMaths1Ctrl()
  132. {
  133. }
  134.  
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CMaths1Ctrl::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 CMaths1Ctrl::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.  
  162.     pdc->MoveTo(xLeft + CX(5), yTop + (yScale / 2));
  163.     pdc->LineTo(xLeft + xScale - CX(5), yTop + (yScale / 2));        // Draw a horizontol line
  164.  
  165.     if (!m_bOpposite)
  166.     {
  167.         pdc->MoveTo(xLeft + (xScale / 2), yTop + CY(5));
  168.         pdc->LineTo(xLeft + (xScale / 2), yTop + yScale - CY(5));        // Draw a vertical line
  169.     }
  170.  
  171.     pdc->SelectObject(pPenSave);
  172. }
  173.  
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. // CMaths1Ctrl::DoPropExchange - Persistence support
  177.  
  178. void CMaths1Ctrl::DoPropExchange(CPropExchange* pPX)
  179. {
  180.     ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
  181.     COleControl::DoPropExchange(pPX);
  182.  
  183.     PX_Bool(pPX, CString((LPCTSTR)IDS_OPPOSITE), m_bOpposite, FALSE);
  184.  
  185. }
  186.  
  187.  
  188. /////////////////////////////////////////////////////////////////////////////
  189. // CMaths1Ctrl::OnResetState - Reset control to default state
  190.  
  191. void CMaths1Ctrl::OnResetState()
  192. {
  193.     COleControl::OnResetState();  // Resets defaults found in DoPropExchange
  194. }
  195.  
  196. /////////////////////////////////////////////////////////////////////////////
  197. // CMaths1Ctrl message handlers
  198.  
  199. void CMaths1Ctrl::OnLButtonDown(UINT nFlags, CPoint point) 
  200. {
  201.     CRect rcBounds;
  202.     GetClientRect(&rcBounds);
  203.  
  204.     long xLeft = rcBounds.left;            // Use with scaling macros
  205.     long yTop = rcBounds.top;    
  206.     long xScale = rcBounds.Width();
  207.     long yScale = rcBounds.Height();    
  208.  
  209.     // Is point on horizontal line
  210.     if ((point.y <= (yTop + (yScale / 2) + CY(5))) && (point.y >= (yTop + (yScale / 2) - CY(5))))
  211.         FireInside((short)point.x, (short)point.y);
  212.     else
  213.     {
  214.         if (!m_bOpposite)
  215.         {
  216.             // Is point on vertical line
  217.             if ((point.x <= (xLeft + (xScale / 2) + CX(5))) && (point.x >= (xLeft + (xScale / 2) - CX(5))))
  218.                 FireInside((short)point.x, (short)point.y);
  219.             else
  220.                 FireOutside();
  221.         }
  222.         else
  223.             FireOutside();
  224.     }
  225.     
  226.     COleControl::OnLButtonDown(nFlags, point);
  227. }
  228.  
  229. //Simple Ellipse Hit-Testing
  230. BOOL CMaths1Ctrl::InEllipse(const CPoint& pt,
  231.     int x1, int y1, int x2, int y2)
  232. {
  233.     // Determine radii                       
  234.     double a = (x2 - x1) / 2;
  235.     double b = (y2 - y1) / 2;
  236.     
  237.     // Determine x, y
  238.     double x = pt.x - (x1 + x2) / 2;
  239.     double y = pt.y - (y1 + y2) / 2;
  240.     
  241.     // Apply ellipse formula
  242.     return ((x * x) / (a * a) + (y * y) / (b * b) <= 1);
  243. }
  244.  
  245. void CMaths1Ctrl::OnOppositeChanged() 
  246. {
  247.     InvalidateControl();
  248. }
  249. /////////////////////////////////////////////////////////////////////////////
  250. // CAboutDlg dialog
  251.  
  252. class CAboutDlg : public CDialog
  253. {
  254. // Construction
  255. public:
  256.     CAboutDlg(CWnd* pParent = NULL);   // standard constructor
  257.  
  258. // Dialog Data
  259.     //{{AFX_DATA(CAboutDlg)
  260.     enum { IDD = IDD_ABOUTBOX_Maths1 };
  261.         // NOTE: the ClassWizard will add data members here
  262.     //}}AFX_DATA
  263.  
  264.  
  265. // Overrides
  266.     // ClassWizard generated virtual function overrides
  267.     //{{AFX_VIRTUAL(CAboutDlg)
  268.     protected:
  269.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  270.     //}}AFX_VIRTUAL
  271.  
  272. // Implementation
  273. protected:
  274.  
  275.     // Generated message map functions
  276.     //{{AFX_MSG(CAboutDlg)
  277.     virtual BOOL OnInitDialog();
  278.     //}}AFX_MSG
  279.     DECLARE_MESSAGE_MAP()
  280. };
  281. /////////////////////////////////////////////////////////////////////////////
  282. // CAboutDlg dialog
  283.  
  284.  
  285. CAboutDlg::CAboutDlg(CWnd* pParent /*=NULL*/)
  286.     : CDialog(CAboutDlg::IDD, pParent)
  287. {
  288.     //{{AFX_DATA_INIT(CAboutDlg)
  289.         // NOTE: the ClassWizard will add member initialization here
  290.     //}}AFX_DATA_INIT
  291. }
  292.  
  293.  
  294. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  295. {
  296.     CDialog::DoDataExchange(pDX);
  297.     //{{AFX_DATA_MAP(CAboutDlg)
  298.         // NOTE: the ClassWizard will add DDX and DDV calls here
  299.     //}}AFX_DATA_MAP
  300. }
  301.  
  302.  
  303. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  304.     //{{AFX_MSG_MAP(CAboutDlg)
  305.     //}}AFX_MSG_MAP
  306. END_MESSAGE_MAP()
  307.  
  308. /////////////////////////////////////////////////////////////////////////////
  309. // CAboutDlg message handlers
  310.  
  311. BOOL CAboutDlg::OnInitDialog() 
  312. {
  313.     CDialog::OnInitDialog();
  314.     CenterWindow();    
  315.     return TRUE;  // return TRUE unless you set the focus to a control
  316.                   // EXCEPTION: OCX Property Pages should return FALSE
  317. }
  318.  
  319. /////////////////////////////////////////////////////////////////////////////
  320. // CMaths1Ctrl::AboutBox - Display an "About" box to the user
  321.  
  322. void CMaths1Ctrl::AboutBox()
  323. {
  324.     CAboutDlg about;
  325.     about.DoModal();
  326. }
  327.