home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / QBAS / DIAMOND1.ZIP / DIAMOND.C < prev    next >
C/C++ Source or Header  |  1992-03-07  |  11KB  |  367 lines

  1.  
  2. #include "windows.h"
  3. #include "custcntl.h"
  4. #include "diamond.h"
  5.  
  6. typedef struct {
  7.    int Width, Height;
  8.    int MouseX, MouseY;
  9.    int Button, ButtonDown;
  10.    int PrevButton;
  11. } DIAMONDSTRUCT;
  12.  
  13. HANDLE       hLibData;
  14. HANDLE       hInst;
  15. HANDLE       hData;
  16. HANDLE       hFont;
  17. HANDLE       hOldFont;
  18.  
  19. LPFNSTRTOID  lpfnVerId;
  20. LPFNIDTOSTR  lpfnIdStr;
  21.  
  22. char *CLASSNAME = "Diamond";
  23.  
  24. #define ID            GetWindowWord(hWnd,GWW_ID)
  25. #define PARENT        GetWindowWord(hWnd,GWW_HWNDPARENT)
  26. #define INSTANCE      GetWindowWord(hWnd,GWW_HINSTANCE)
  27.  
  28. #define WINEXTRA      2
  29.  
  30. #define SET_MEMORY(m) SetWindowWord(hWnd,0,m)
  31. #define MEMORY        GetWindowWord(hWnd,0)
  32. #define DEREF(h)      ((DIAMONDSTRUCT *)LocalLock(h))
  33.  
  34.  
  35. int FAR PASCAL LibMain( HANDLE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR CmdLine )
  36. {
  37.    HANDLE hMem;
  38.    WNDCLASS *wc;
  39.  
  40.    hData = wDataSeg;
  41.    UnlockData( 0 );
  42.  
  43.    if( ! hInst )
  44.    {
  45.       hMem = LocalAlloc( LHND, sizeof(WNDCLASS) );
  46.       if( hMem )
  47.       {
  48.          wc = (WNDCLASS *)LocalLock( hMem );
  49.          if( wc )
  50.          {
  51.             wc->lpszClassName = CLASSNAME;
  52.             wc->hCursor = LoadCursor( NULL, IDC_ARROW );
  53.             wc->lpszMenuName = NULL;
  54.             wc->style = CS_GLOBALCLASS;
  55.             wc->lpfnWndProc = DiamondWndProc;
  56.             wc->hInstance = hInstance;
  57.             wc->hIcon = NULL;
  58.             wc->cbWndExtra = WINEXTRA;
  59.             wc->cbClsExtra = 0;
  60.             wc->hbrBackground = GetStockObject( NULL_BRUSH );
  61.  
  62.             hInst = ( RegisterClass( wc ) ) ? hInstance : NULL;
  63.             LocalUnlock( hMem );
  64.          }
  65.          LocalFree( hMem );
  66.       }
  67.    }
  68.    return( hInst );
  69. }
  70.  
  71.  
  72.  
  73. VOID FAR PASCAL WEP( int nParam )
  74. {
  75.    return;
  76. }
  77.  
  78.  
  79. LONG FAR PASCAL DiamondWndProc( HWND hWnd, WORD msg, WORD wParam, LONG lParam )
  80. {
  81.    switch( msg )
  82.    {
  83.       case WM_ENABLE:                    /*  Getting a WM_ENABLE message...  */
  84.       {
  85.          DIAMONDSTRUCT *Ds;
  86.  
  87.          Ds = DEREF( MEMORY );                              /*  Lock memory  */
  88.          if( ! Ds )
  89.             return( FALSE );
  90.  
  91.          if( wParam )                     /*  If window is begin enabled...  */
  92.          {
  93.             Ds->Button = -1;
  94.             PaintControl( hWnd, ALLUP );
  95.          }
  96.          else
  97.          {                                  /*  If window being disabled...  */
  98.             Ds->Button = -2;
  99.             PaintControl( hWnd, ALLDISABLE );
  100.          }
  101.  
  102.          LocalUnlock( MEMORY );                           /*  Unlock memory  */
  103.          return( TRUE );
  104.       }
  105.  
  106.       case WM_CREATE:                       /*  Control is begin created...  */
  107.       {
  108.          LPCREATESTRUCT Cs;
  109.          DIAMONDSTRUCT *Ds;
  110.          HANDLE hMem;
  111.  
  112.          Cs = (LPCREATESTRUCT)lParam;    /*  Lock parameters passed from RC  */
  113.  
  114.          hMem = LocalAlloc( LHND, sizeof(DIAMONDSTRUCT) );
  115.          if( ! hMem )
  116.          {
  117.             SET_MEMORY( 0 );         /*  Allocate memory for info structure  */
  118.             DestroyWindow( hWnd );
  119.             break;                                /*  If it can't, it quits  */
  120.          }
  121.  
  122.          Ds = DEREF( hMem );                                /*  Lock memory  */
  123.          if( ! Ds )
  124.          {
  125.             SET_MEMORY( 0 );
  126.             LocalFree( hMem );
  127.             DestroyWindow( hWnd );
  128.             break;
  129.          }
  130.  
  131.          Ds->Width = Cs->cx;                          /*  Save initial size  */
  132.          Ds->Height = Cs->cy;
  133.          Ds->MouseX = Ds->MouseY = Ds->ButtonDown = 0;
  134.  
  135.          LocalUnlock( hMem );                             /*  Unlock memory  */
  136.          SET_MEMORY( hMem );                   /*  Save local memory handle  */
  137.          break;
  138.       }
  139.  
  140.       case WM_GETDLGCODE:
  141.          lParam = DLGC_BUTTON;            /*  Tell parent we're a button...  */
  142.          return( lParam );
  143.  
  144.       case WM_SIZE:                               /*  New size being set...  */
  145.       {
  146.          DIAMONDSTRUCT *Ds;
  147.  
  148.          Ds = (DIAMONDSTRUCT *)LocalLock( MEMORY );
  149.          if( ! Ds )
  150.             return( FALSE );                                /*  Lock memory  */
  151.  
  152.          Ds->Width = LOWORD( lParam );
  153.          Ds->Height = HIWORD( lParam );                   /*  Save new size  */
  154.          LocalUnlock( MEMORY );                           /*  Unlock memory  */
  155.          break;
  156.       }
  157.  
  158.       case WM_PAINT:                   /*  Just repaint the button as ALLUP  */
  159.       {
  160.          PAINTSTRUCT Ps;
  161.  
  162.          BeginPaint( hWnd, &Ps );
  163.          PaintControl( hWnd, ALLUP );
  164.          EndPaint( hWnd, &Ps );
  165.          break;
  166.       }
  167.  
  168.       case WM_LBUTTONDOWN:              /*  If a mouse button is pressed...  */
  169.       {
  170.          DIAMONDSTRUCT *Ds;
  171.  
  172.          Ds = DEREF( MEMORY );                              /*  Lock memory  */
  173.          if( ! Ds )
  174.             return( FALSE );               /*  If button already pressed or  */
  175.                                                      /*  disabled, exit now  */
  176.          if( Ds->ButtonDown || Ds->Button == -2 )
  177.          {
  178.             LocalUnlock( MEMORY );                        /*  Unlock memory  */
  179.             return( TRUE );
  180.          }
  181.  
  182.          Ds->MouseX = LOWORD( lParam );         /*  Save new mouse position  */
  183.          Ds->MouseY = HIWORD( lParam );
  184.                                                       /*  Get button number  */
  185.          Ds->Button = GetButtonFromXY( Ds->MouseX, Ds->MouseY );
  186.          PaintControl( hWnd, Ds->Button );
  187.  
  188.          Ds->ButtonDown = 1;                      /*  Button is now pressed  */
  189.          Ds->PrevButton = Ds->Button;
  190.  
  191.          SetCapture( hWnd );               /*  Capture mouse, unlock memory  */
  192.          LocalUnlock( MEMORY );
  193.  
  194.          return( TRUE );
  195.       }
  196.  
  197.       case WM_MOUSEMOVE:    /*  Should the mouse be moved over a control...  */
  198.       {
  199.          DIAMONDSTRUCT *Ds;
  200.          int NewButton;
  201.  
  202.          Ds = DEREF( MEMORY );                       /*  Lock memory or die  */
  203.          if( ! Ds )
  204.             return( FALSE );
  205.  
  206.          if( ! Ds->ButtonDown )           /*  If button's not pressed, exit  */
  207.          {
  208.             LocalUnlock( MEMORY );
  209.             return( TRUE );
  210.          }                                    /*  Get button currently over  */
  211.  
  212.          NewButton = GetButtonFromXY( LOWORD( lParam ), HIWORD( lParam ) );
  213.  
  214.          if( NewButton != Ds->PrevButton )    /*  If different than pressed  */
  215.          {
  216.             if( NewButton == Ds->Button )    /*  but back to button pressed  */
  217.                PaintControl( hWnd, NewButton );   /*  Paint original button  */
  218.             else
  219.             {                                    /*  If not original button  */
  220.                if( Ds->PrevButton == Ds->Button )
  221.                   PaintControl( hWnd, ALLUP );           /*  Paint as ALLUP  */
  222.             }
  223.             Ds->PrevButton = NewButton;
  224.          }
  225.          LocalUnlock( MEMORY );                           /*  Unlock memory  */
  226.          return( TRUE );
  227.       }
  228.  
  229.       case WM_LBUTTONUP:                        /*  When button is released  */
  230.       {
  231.          DIAMONDSTRUCT *Ds;
  232.  
  233.          Ds = DEREF( MEMORY );                              /*  Lock memory  */
  234.          if( ! Ds )
  235.             return( FALSE );
  236.  
  237.          if( ! Ds->ButtonDown )              /*  If button already up, exit  */
  238.          {
  239.             LocalUnlock( MEMORY );
  240.             return( TRUE );
  241.          }
  242.          Ds->ButtonDown = 0;            /*  Button is now up, release mouse  */
  243.          ReleaseCapture();
  244.  
  245.          PaintControl( hWnd, ALLUP );                   /*  Paint as all up  */
  246.  
  247.          if( Ds->Button == Ds->PrevButton )             /*  If valid button  */
  248.             PostMessage( PARENT, WM_COMMAND, ID,            /*  Tell parent  */
  249.                          MAKELONG( Ds->Button, BN_CLICKED ) );
  250.  
  251.          LocalUnlock( MEMORY );                  /*  Unlock and bye bye....  */
  252.  
  253.          return( TRUE );
  254.       }
  255.  
  256.       case WM_DESTROY:
  257.          if( MEMORY )
  258.             LocalFree( MEMORY );         /*  Kill memory when window killed  */
  259.  
  260.       default:
  261.          return( DefWindowProc( hWnd, msg, wParam, lParam ) );
  262.    }
  263. }
  264.  
  265.  
  266.  
  267.  
  268. BOOL PaintControl( HWND hWnd, int Style )
  269. {
  270.    HDC hDC;
  271.    HDC mDC;
  272.    HBITMAP hBmp;
  273.    HBRUSH hBrush;
  274.    DIAMONDSTRUCT *Ds;
  275.  
  276.    if( ! MEMORY )
  277.       return( FALSE );
  278.  
  279.    Ds = (DIAMONDSTRUCT far *)LocalLock( MEMORY );           /*  Lock memory  */
  280.    if( ! Ds )
  281.       return( FALSE );
  282.                                                   /*  Get bitmap from style  */
  283.    hBmp = LoadBitmap( hInst, MAKEINTRESOURCE( 8002 + Style ) );
  284.  
  285.    hDC = GetDC( hWnd );                            /*  Get display contexts  */
  286.    mDC = CreateCompatibleDC( hDC );
  287.  
  288.    SelectObject( mDC, hBmp );                       /*  Use selected bitmap  */
  289.    SelectObject( mDC, GetStockObject( NULL_BRUSH ) );
  290.    FloodFill( mDC, 2, 2, RGB( 0, 0, 0 ) );
  291.    FloodFill( mDC, 2, 62, RGB( 0, 0, 0 ) );
  292.    FloodFill( mDC, 62, 2, RGB( 0, 0, 0 ) );
  293.    FloodFill( mDC, 62, 62, RGB( 0, 0, 0 ) );
  294.                                                   /*  Copy bitmap to window  */
  295.    BitBlt( hDC, 0, 0, Ds->Width, Ds->Height, mDC, 0, 0, SRCCOPY );
  296.  
  297.    DeleteDC( mDC );
  298.    ReleaseDC( hWnd, hDC );                         /*  Get rid of resources  */
  299.    DeleteObject( hBmp );
  300.  
  301.    LocalUnlock( MEMORY );
  302.    return( TRUE );
  303. }
  304.  
  305.  
  306.  
  307. /*
  308.  
  309. Given X and Y mouse position, returns button number where:
  310.     0  - Top button
  311.     1  - Right button
  312.     2  - Bottom button
  313.     3  - Left button
  314.     -1 - Off pad.
  315.  
  316. */
  317.  
  318. int GetButtonFromXY( int Xpos, int Ypos )
  319. {
  320.    if( Ypos < 16 )
  321.    {
  322.       if( ( Xpos > 31 - Ypos ) && ( Xpos < 32 + Ypos ) )
  323.          return( 0 );
  324.       return( -1 );
  325.    }
  326.  
  327.    if( Ypos > 47 )
  328.    {
  329.       if( ( Xpos > 15 + ( Ypos - 48 ) ) && ( Xpos < 32 + ( 64 - Ypos ) ) )
  330.          return( 2 );
  331.       return( -1 );
  332.    }
  333.  
  334.    if( Xpos < 16 )
  335.    {
  336.       if( ( Ypos > 32 - Xpos ) && ( Ypos < 32 + Xpos ) )
  337.          return( 3 );
  338.       return( -1 );
  339.    }
  340.  
  341.    if( Xpos > 47 )
  342.    {
  343.       if( Ypos > Xpos - 32 && Ypos < 32 + ( 64 - Xpos ) )
  344.          return( 1 );
  345.       return( -1 );
  346.    }
  347.  
  348.    if( Ypos < 32 )
  349.    {
  350.       if( Xpos < Ypos )
  351.          return( 3 );
  352.  
  353.       if( Xpos > 64 - Ypos )
  354.          return( 1 );
  355.  
  356.       return( 0 );
  357.    }
  358.  
  359.    if( Xpos < 64 - Ypos )
  360.       return( 3 );
  361.    if( Xpos > Ypos )
  362.       return( 1 );
  363.    return( 2 );
  364. }
  365.  
  366.  
  367.