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 >
Wrap
C/C++ Source or Header
|
1992-03-07
|
11KB
|
367 lines
#include "windows.h"
#include "custcntl.h"
#include "diamond.h"
typedef struct {
int Width, Height;
int MouseX, MouseY;
int Button, ButtonDown;
int PrevButton;
} DIAMONDSTRUCT;
HANDLE hLibData;
HANDLE hInst;
HANDLE hData;
HANDLE hFont;
HANDLE hOldFont;
LPFNSTRTOID lpfnVerId;
LPFNIDTOSTR lpfnIdStr;
char *CLASSNAME = "Diamond";
#define ID GetWindowWord(hWnd,GWW_ID)
#define PARENT GetWindowWord(hWnd,GWW_HWNDPARENT)
#define INSTANCE GetWindowWord(hWnd,GWW_HINSTANCE)
#define WINEXTRA 2
#define SET_MEMORY(m) SetWindowWord(hWnd,0,m)
#define MEMORY GetWindowWord(hWnd,0)
#define DEREF(h) ((DIAMONDSTRUCT *)LocalLock(h))
int FAR PASCAL LibMain( HANDLE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR CmdLine )
{
HANDLE hMem;
WNDCLASS *wc;
hData = wDataSeg;
UnlockData( 0 );
if( ! hInst )
{
hMem = LocalAlloc( LHND, sizeof(WNDCLASS) );
if( hMem )
{
wc = (WNDCLASS *)LocalLock( hMem );
if( wc )
{
wc->lpszClassName = CLASSNAME;
wc->hCursor = LoadCursor( NULL, IDC_ARROW );
wc->lpszMenuName = NULL;
wc->style = CS_GLOBALCLASS;
wc->lpfnWndProc = DiamondWndProc;
wc->hInstance = hInstance;
wc->hIcon = NULL;
wc->cbWndExtra = WINEXTRA;
wc->cbClsExtra = 0;
wc->hbrBackground = GetStockObject( NULL_BRUSH );
hInst = ( RegisterClass( wc ) ) ? hInstance : NULL;
LocalUnlock( hMem );
}
LocalFree( hMem );
}
}
return( hInst );
}
VOID FAR PASCAL WEP( int nParam )
{
return;
}
LONG FAR PASCAL DiamondWndProc( HWND hWnd, WORD msg, WORD wParam, LONG lParam )
{
switch( msg )
{
case WM_ENABLE: /* Getting a WM_ENABLE message... */
{
DIAMONDSTRUCT *Ds;
Ds = DEREF( MEMORY ); /* Lock memory */
if( ! Ds )
return( FALSE );
if( wParam ) /* If window is begin enabled... */
{
Ds->Button = -1;
PaintControl( hWnd, ALLUP );
}
else
{ /* If window being disabled... */
Ds->Button = -2;
PaintControl( hWnd, ALLDISABLE );
}
LocalUnlock( MEMORY ); /* Unlock memory */
return( TRUE );
}
case WM_CREATE: /* Control is begin created... */
{
LPCREATESTRUCT Cs;
DIAMONDSTRUCT *Ds;
HANDLE hMem;
Cs = (LPCREATESTRUCT)lParam; /* Lock parameters passed from RC */
hMem = LocalAlloc( LHND, sizeof(DIAMONDSTRUCT) );
if( ! hMem )
{
SET_MEMORY( 0 ); /* Allocate memory for info structure */
DestroyWindow( hWnd );
break; /* If it can't, it quits */
}
Ds = DEREF( hMem ); /* Lock memory */
if( ! Ds )
{
SET_MEMORY( 0 );
LocalFree( hMem );
DestroyWindow( hWnd );
break;
}
Ds->Width = Cs->cx; /* Save initial size */
Ds->Height = Cs->cy;
Ds->MouseX = Ds->MouseY = Ds->ButtonDown = 0;
LocalUnlock( hMem ); /* Unlock memory */
SET_MEMORY( hMem ); /* Save local memory handle */
break;
}
case WM_GETDLGCODE:
lParam = DLGC_BUTTON; /* Tell parent we're a button... */
return( lParam );
case WM_SIZE: /* New size being set... */
{
DIAMONDSTRUCT *Ds;
Ds = (DIAMONDSTRUCT *)LocalLock( MEMORY );
if( ! Ds )
return( FALSE ); /* Lock memory */
Ds->Width = LOWORD( lParam );
Ds->Height = HIWORD( lParam ); /* Save new size */
LocalUnlock( MEMORY ); /* Unlock memory */
break;
}
case WM_PAINT: /* Just repaint the button as ALLUP */
{
PAINTSTRUCT Ps;
BeginPaint( hWnd, &Ps );
PaintControl( hWnd, ALLUP );
EndPaint( hWnd, &Ps );
break;
}
case WM_LBUTTONDOWN: /* If a mouse button is pressed... */
{
DIAMONDSTRUCT *Ds;
Ds = DEREF( MEMORY ); /* Lock memory */
if( ! Ds )
return( FALSE ); /* If button already pressed or */
/* disabled, exit now */
if( Ds->ButtonDown || Ds->Button == -2 )
{
LocalUnlock( MEMORY ); /* Unlock memory */
return( TRUE );
}
Ds->MouseX = LOWORD( lParam ); /* Save new mouse position */
Ds->MouseY = HIWORD( lParam );
/* Get button number */
Ds->Button = GetButtonFromXY( Ds->MouseX, Ds->MouseY );
PaintControl( hWnd, Ds->Button );
Ds->ButtonDown = 1; /* Button is now pressed */
Ds->PrevButton = Ds->Button;
SetCapture( hWnd ); /* Capture mouse, unlock memory */
LocalUnlock( MEMORY );
return( TRUE );
}
case WM_MOUSEMOVE: /* Should the mouse be moved over a control... */
{
DIAMONDSTRUCT *Ds;
int NewButton;
Ds = DEREF( MEMORY ); /* Lock memory or die */
if( ! Ds )
return( FALSE );
if( ! Ds->ButtonDown ) /* If button's not pressed, exit */
{
LocalUnlock( MEMORY );
return( TRUE );
} /* Get button currently over */
NewButton = GetButtonFromXY( LOWORD( lParam ), HIWORD( lParam ) );
if( NewButton != Ds->PrevButton ) /* If different than pressed */
{
if( NewButton == Ds->Button ) /* but back to button pressed */
PaintControl( hWnd, NewButton ); /* Paint original button */
else
{ /* If not original button */
if( Ds->PrevButton == Ds->Button )
PaintControl( hWnd, ALLUP ); /* Paint as ALLUP */
}
Ds->PrevButton = NewButton;
}
LocalUnlock( MEMORY ); /* Unlock memory */
return( TRUE );
}
case WM_LBUTTONUP: /* When button is released */
{
DIAMONDSTRUCT *Ds;
Ds = DEREF( MEMORY ); /* Lock memory */
if( ! Ds )
return( FALSE );
if( ! Ds->ButtonDown ) /* If button already up, exit */
{
LocalUnlock( MEMORY );
return( TRUE );
}
Ds->ButtonDown = 0; /* Button is now up, release mouse */
ReleaseCapture();
PaintControl( hWnd, ALLUP ); /* Paint as all up */
if( Ds->Button == Ds->PrevButton ) /* If valid button */
PostMessage( PARENT, WM_COMMAND, ID, /* Tell parent */
MAKELONG( Ds->Button, BN_CLICKED ) );
LocalUnlock( MEMORY ); /* Unlock and bye bye.... */
return( TRUE );
}
case WM_DESTROY:
if( MEMORY )
LocalFree( MEMORY ); /* Kill memory when window killed */
default:
return( DefWindowProc( hWnd, msg, wParam, lParam ) );
}
}
BOOL PaintControl( HWND hWnd, int Style )
{
HDC hDC;
HDC mDC;
HBITMAP hBmp;
HBRUSH hBrush;
DIAMONDSTRUCT *Ds;
if( ! MEMORY )
return( FALSE );
Ds = (DIAMONDSTRUCT far *)LocalLock( MEMORY ); /* Lock memory */
if( ! Ds )
return( FALSE );
/* Get bitmap from style */
hBmp = LoadBitmap( hInst, MAKEINTRESOURCE( 8002 + Style ) );
hDC = GetDC( hWnd ); /* Get display contexts */
mDC = CreateCompatibleDC( hDC );
SelectObject( mDC, hBmp ); /* Use selected bitmap */
SelectObject( mDC, GetStockObject( NULL_BRUSH ) );
FloodFill( mDC, 2, 2, RGB( 0, 0, 0 ) );
FloodFill( mDC, 2, 62, RGB( 0, 0, 0 ) );
FloodFill( mDC, 62, 2, RGB( 0, 0, 0 ) );
FloodFill( mDC, 62, 62, RGB( 0, 0, 0 ) );
/* Copy bitmap to window */
BitBlt( hDC, 0, 0, Ds->Width, Ds->Height, mDC, 0, 0, SRCCOPY );
DeleteDC( mDC );
ReleaseDC( hWnd, hDC ); /* Get rid of resources */
DeleteObject( hBmp );
LocalUnlock( MEMORY );
return( TRUE );
}
/*
Given X and Y mouse position, returns button number where:
0 - Top button
1 - Right button
2 - Bottom button
3 - Left button
-1 - Off pad.
*/
int GetButtonFromXY( int Xpos, int Ypos )
{
if( Ypos < 16 )
{
if( ( Xpos > 31 - Ypos ) && ( Xpos < 32 + Ypos ) )
return( 0 );
return( -1 );
}
if( Ypos > 47 )
{
if( ( Xpos > 15 + ( Ypos - 48 ) ) && ( Xpos < 32 + ( 64 - Ypos ) ) )
return( 2 );
return( -1 );
}
if( Xpos < 16 )
{
if( ( Ypos > 32 - Xpos ) && ( Ypos < 32 + Xpos ) )
return( 3 );
return( -1 );
}
if( Xpos > 47 )
{
if( Ypos > Xpos - 32 && Ypos < 32 + ( 64 - Xpos ) )
return( 1 );
return( -1 );
}
if( Ypos < 32 )
{
if( Xpos < Ypos )
return( 3 );
if( Xpos > 64 - Ypos )
return( 1 );
return( 0 );
}
if( Xpos < 64 - Ypos )
return( 3 );
if( Xpos > Ypos )
return( 1 );
return( 2 );
}