home *** CD-ROM | disk | FTP | other *** search
/ On Hand / On_Hand_From_Softbank_1994_Release_2_Disc_2_1994.iso / 00202 / s / disk1 / circ2.c_ / circ2.bin
Text File  |  1993-04-28  |  10KB  |  347 lines

  1. //---------------------------------------------------------------------------
  2. //        Copyright (C) 1991-93, Microsoft Corporation
  3. //
  4. // You have a royalty-free right to use, modify, reproduce and distribute
  5. // the Sample Custom Control Files (and/or any modified version) in any way
  6. // you find useful, provided that you agree that Microsoft has no warranty,
  7. // obligation or liability for any Custom Control File.
  8. //---------------------------------------------------------------------------
  9. // Circ2.c
  10. //---------------------------------------------------------------------------
  11. // Contains control procedure for CIRC2 control
  12. //---------------------------------------------------------------------------
  13.  
  14. #include <windows.h>
  15. #include <vbapi.h>
  16. #include "circ2.h"
  17.  
  18. //---------------------------------------------------------------------------
  19. // Global Variables
  20. //---------------------------------------------------------------------------
  21. HANDLE hmodDLL;
  22.  
  23.  
  24. //---------------------------------------------------------------------------
  25. // Local Prototypes
  26. //---------------------------------------------------------------------------
  27. VOID NEAR PaintCircle(HCTL hctl, HWND hwnd, HDC hdc);
  28. VOID NEAR RecalcArea(HCTL hctl, HWND hwnd);
  29. VOID NEAR FlashCircle(HCTL hctl, HDC hdc);
  30. BOOL NEAR InCircle(HCTL hctl, SHORT xcoord, SHORT ycoord);
  31. VOID NEAR FireClickIn(HCTL hctl, SHORT x, SHORT y);
  32. VOID NEAR FireClickOut(HCTL hctl);
  33.  
  34.  
  35. //---------------------------------------------------------------------------
  36. // Circle Control Procedure
  37. //---------------------------------------------------------------------------
  38. LONG FAR PASCAL _export CircleCtlProc
  39. (
  40.     HCTL   hctl,
  41.     HWND   hwnd,
  42.     USHORT msg,
  43.     USHORT wp,
  44.     LONG   lp
  45. )
  46. {
  47.     switch (msg)
  48.     {
  49.     case WM_NCCREATE:
  50.         {
  51.         LPCIRC lpcirc = LpcircDEREF(hctl);
  52.  
  53.         lpcirc->CircleShape = TRUE;
  54.         lpcirc->FlashColor = 128L;
  55.         VBSetControlProperty(hctl, IPROP_CIRCLE_BACKCOLOR, 255L);
  56.         // *** lpcirc may now be invalid due to call to VB API ***
  57.         break;
  58.         }
  59.  
  60.         case WM_LBUTTONDOWN:
  61.         case WM_LBUTTONDBLCLK:
  62.         if (InCircle(hctl, (SHORT)lp, (SHORT)HIWORD(lp)))
  63.         {
  64.         HDC hdc = GetDC(hwnd);
  65.  
  66.         FlashCircle(hctl, hdc);
  67.         ReleaseDC(hwnd, hdc);
  68.         FireClickIn(hctl, (SHORT)lp, (SHORT)HIWORD(lp));
  69.         }
  70.         else
  71.         FireClickOut(hctl);
  72.             break;
  73.  
  74.         case WM_LBUTTONUP:
  75.         if (InCircle(hctl, (SHORT)lp, (SHORT)HIWORD(lp)))
  76.         {
  77.         HDC hdc = GetDC(hwnd);
  78.  
  79.         PaintCircle(hctl, hwnd, hdc);
  80.         ReleaseDC(hwnd, hdc);
  81.         }
  82.             break;
  83.  
  84.         case WM_PAINT:
  85.             if (wp)
  86.         PaintCircle(hctl, hwnd, (HDC)wp);
  87.         else
  88.         {
  89.                 PAINTSTRUCT ps;
  90.  
  91.         BeginPaint(hwnd, &ps);
  92.         PaintCircle(hctl, hwnd, ps.hdc);
  93.         EndPaint(hwnd, &ps);
  94.         }
  95.             break;
  96.  
  97.         case WM_SIZE:
  98.         RecalcArea(hctl, hwnd);
  99.             break;
  100.  
  101.         case VBM_SETPROPERTY:
  102.         switch (wp)
  103.         {
  104.         case IPROP_CIRCLE_SHAPE:
  105.             LpcircDEREF(hctl)->CircleShape = (SHORT)lp;
  106.             RecalcArea(hctl, hwnd);
  107.             InvalidateRect(hwnd, NULL, TRUE);
  108.                     return 0;
  109.         }
  110.             break;
  111.  
  112.     }
  113.  
  114.     return VBDefControlProc(hctl, hwnd, msg, wp, lp);
  115. }
  116.  
  117.  
  118. //---------------------------------------------------------------------------
  119. // Handle painting by drawing circle into the given hdc.
  120. //---------------------------------------------------------------------------
  121. VOID NEAR PaintCircle
  122. (
  123.     HCTL hctl,
  124.     HWND hwnd,
  125.     HDC  hdc
  126. )
  127. {
  128.     HBRUSH hbr;
  129.     HBRUSH hbrOld = NULL;
  130.     LPRECT lprect = &LpcircDEREF(hctl)->rectDrawInto;
  131.  
  132.     hbr = (HBRUSH)SendMessage(GetParent(hwnd), WM_CTLCOLOR,
  133.                   hdc, MAKELONG(hwnd, 0));
  134.     if (hbr)
  135.     hbrOld = SelectObject(hdc, hbr);
  136.     Ellipse(hdc, lprect->left, lprect->top, lprect->right, lprect->bottom);
  137.     if (hbrOld)
  138.     SelectObject(hdc, hbrOld);    // Restore old brush
  139. }
  140.  
  141.  
  142. //---------------------------------------------------------------------------
  143. // Paint the circle in the FlashColor.
  144. //---------------------------------------------------------------------------
  145. VOID NEAR FlashCircle
  146. (
  147.     HCTL hctl,
  148.     HDC  hdc
  149. )
  150. {
  151.     HBRUSH  hbr;
  152.     HBRUSH  hbrOld = NULL;
  153.     LPCIRC  lpcirc = LpcircDEREF(hctl);
  154.     LPRECT  lprect = &lpcirc->rectDrawInto;
  155.  
  156.     hbr = CreateSolidBrush(RGBCOLOR(lpcirc->FlashColor));
  157.     if (hbr)
  158.     hbrOld = SelectObject(hdc, hbr);
  159.     Ellipse(hdc, lprect->left, lprect->top, lprect->right, lprect->bottom);
  160.     if (hbr)
  161.     {
  162.     SelectObject(hdc, hbrOld);
  163.     DeleteObject(hbr);
  164.     }
  165. }
  166.  
  167.  
  168. //---------------------------------------------------------------------------
  169. // Use the hwnd's client size to determine the bounding rectangle for the
  170. // circle.  If CircleShape is TRUE, then we need to calculate a square
  171. // centered in lprect.
  172. //---------------------------------------------------------------------------
  173. VOID NEAR RecalcArea
  174. (
  175.     HCTL hctl,
  176.     HWND hwnd
  177. )
  178. {
  179.     LPCIRC lpcirc = LpcircDEREF(hctl);
  180.     LPRECT lprect = &lpcirc->rectDrawInto;
  181.  
  182.     GetClientRect(hwnd, lprect);
  183.     if (!lpcirc->CircleShape)
  184.         return;
  185.     if (lprect->right > lprect->bottom)
  186.     {
  187.     lprect->left  = (lprect->right - lprect->bottom) / 2;
  188.     lprect->right = lprect->left + lprect->bottom;
  189.     }
  190.     else if (lprect->bottom > lprect->right)
  191.     {
  192.     lprect->top    = (lprect->bottom - lprect->right) / 2;
  193.     lprect->bottom = lprect->top + lprect->right;
  194.     }
  195. }
  196.  
  197.  
  198. //---------------------------------------------------------------------------
  199. // Return TRUE if the given coordinates are inside of the circle.
  200. //---------------------------------------------------------------------------
  201. BOOL NEAR InCircle
  202. (
  203.     HCTL  hctl,
  204.     SHORT xcoord,
  205.     SHORT ycoord
  206. )
  207. {
  208.     double  a, b;
  209.     double  x, y;
  210.     LPRECT  lprect = &LpcircDEREF(hctl)->rectDrawInto;
  211.  
  212.     a = (lprect->right    - lprect->left) / 2;
  213.     b = (lprect->bottom - lprect->top)    / 2;
  214.     x = xcoord - (lprect->left + lprect->right)  / 2;
  215.     y = ycoord - (lprect->top  + lprect->bottom) / 2;
  216.     return ((x * x) / (a * a) + (y * y) / (b * b) <= 1);
  217. }
  218.  
  219.  
  220. //---------------------------------------------------------------------------
  221. // TYPEDEF for parameters to the ClickIn event.
  222. //---------------------------------------------------------------------------
  223. typedef struct tagCLICKINPARMS
  224.     {
  225.     float far *Y;
  226.     float far *X;
  227.     LPVOID     Index;
  228.     } CLICKINPARMS;
  229.  
  230.  
  231. //---------------------------------------------------------------------------
  232. // Fire the ClickIn event, passing the x,y coords of the click.
  233. //---------------------------------------------------------------------------
  234. VOID NEAR FireClickIn
  235. (
  236.     HCTL  hctl,
  237.     SHORT x,
  238.     SHORT y
  239. )
  240. {
  241.     CLICKINPARMS params;
  242.     float     xTwips, yTwips;
  243.  
  244.     xTwips = (float)VBXPixelsToTwips(x);
  245.     yTwips = (float)VBYPixelsToTwips(y);
  246.     params.X = &xTwips;
  247.     params.Y = &yTwips;
  248.     VBFireEvent(hctl, IEVENT_CIRCLE_CLICKIN, ¶ms);
  249. }
  250.  
  251.  
  252. //---------------------------------------------------------------------------
  253. // Fire the ClickOut event.
  254. //---------------------------------------------------------------------------
  255. VOID NEAR FireClickOut
  256. (
  257.     HCTL hctl
  258. )
  259. {
  260.     VBFireEvent(hctl, IEVENT_CIRCLE_CLICKOUT, NULL);
  261. }
  262.  
  263.  
  264. //---------------------------------------------------------------------------
  265. // Initialize library. This routine is called when the first client loads
  266. // the DLL.
  267. //---------------------------------------------------------------------------
  268. int FAR PASCAL LibMain
  269. (
  270.     HANDLE hModule,
  271.     WORD   wDataSeg,
  272.     WORD   cbHeapSize,
  273.     LPSTR  lpszCmdLine
  274. )
  275. {
  276.     // Avoid warnings on unused (but required) formal parameters
  277.     wDataSeg    = wDataSeg;
  278.     cbHeapSize    = cbHeapSize;
  279.     lpszCmdLine = lpszCmdLine;
  280.  
  281.     hmodDLL = hModule;
  282.  
  283.     return 1;
  284. }
  285.  
  286.  
  287. //---------------------------------------------------------------------------
  288. // Register custom control.  This routine is called by VB when the custom
  289. // control DLL is loaded for use.
  290. //---------------------------------------------------------------------------
  291. BOOL FAR PASCAL _export VBINITCC
  292. (
  293.     USHORT usVersion,
  294.     BOOL   fRuntime
  295. )
  296. {
  297.     // Avoid warnings on unused (but required) formal parameters
  298.     fRuntime  = fRuntime;
  299.     usVersion = usVersion;
  300.  
  301.     // Register control(s)
  302.     return VBRegisterModel(hmodDLL, &modelCircle);
  303. }
  304.  
  305.  
  306. //---------------------------------------------------------------------------
  307. // WEP
  308. //---------------------------------------------------------------------------
  309. // C7 and QCWIN provide default a WEP:
  310. //---------------------------------------------------------------------------
  311. #if (_MSC_VER < 610)
  312.  
  313. int FAR PASCAL WEP(int fSystemExit);
  314.  
  315. //---------------------------------------------------------------------------
  316. // For Windows 3.0 it is recommended that the WEP function reside in a
  317. // FIXED code segment and be exported as RESIDENTNAME.  This is
  318. // accomplished using the alloc_text pragma below and the related EXPORTS
  319. // and SEGMENTS directives in the .DEF file.
  320. //
  321. // Read the comments section documenting the WEP function in the Windows
  322. // 3.1 SDK "Programmers Reference, Volume 2: Functions" before placing
  323. // any additional code in the WEP routine for a Windows 3.0 DLL.
  324. //---------------------------------------------------------------------------
  325. #pragma alloc_text(WEP_TEXT,WEP)
  326.  
  327. //---------------------------------------------------------------------------
  328. // Performs cleanup tasks when the DLL is unloaded.  WEP() is
  329. // called automatically by Windows when the DLL is unloaded (no
  330. // remaining tasks still have the DLL loaded).    It is strongly
  331. // recommended that a DLL have a WEP() function, even if it does
  332. // nothing but returns success (1), as in this example.
  333. //---------------------------------------------------------------------------
  334. int FAR PASCAL WEP
  335. (
  336.     int fSystemExit
  337. )
  338. {
  339.     // Avoid warnings on unused (but required) formal parameters
  340.     fSystemExit = fSystemExit;
  341.  
  342.     return 1;
  343. }
  344. #endif // C6
  345.  
  346. //---------------------------------------------------------------------------
  347.