📄 spincube.c
字号:
{
MessageBox (NULL,
GetStringRes (IDS_ALLOCFAIL),
(LPCTSTR) "SPINCUBE.DLL",
MB_OK | MB_ICONEXCLAMATION);
return -1;
}
//
// Alloc the compatible DC for this control.
//
hdc = GetDC (hwnd);
if ((pSCI->hdcCompat = CreateCompatibleDC (hdc)) == NULL)
{
MessageBox (NULL,
GetStringRes (IDS_CREATEDCFAIL),
(LPCTSTR) "SPINCUBE.DLL",
MB_OK | MB_ICONEXCLAMATION);
return -1;
}
ReleaseDC (hwnd, hdc);
//
// Initialize this instance structure
//
pSCI->fCurrentXRotation =
pSCI->fCurrentYRotation =
pSCI->fCurrentZRotation = (float) 0.0;
pSCI->fCurrentXRotationInc =
pSCI->fCurrentYRotationInc =
pSCI->fCurrentZRotationInc = (float) 0.2617; // a random # (15 degrees)
pSCI->iCurrentXTranslation =
pSCI->iCurrentYTranslation =
pSCI->iCurrentZTranslation = 0;
//
// All these calculations so the cubes start out with random movements.
//
if ((pSCI->iCurrentXTranslationInc = (rand() % 10) + 2) > 7)
pSCI->iCurrentXTranslationInc = -pSCI->iCurrentXTranslationInc;
if ((pSCI->iCurrentYTranslationInc = (rand() % 10) + 2) <= 7)
pSCI->iCurrentYTranslationInc = -pSCI->iCurrentYTranslationInc;
if ((pSCI->iCurrentZTranslationInc = (rand() % 10) + 2) > 7)
pSCI->iCurrentZTranslationInc = -pSCI->iCurrentZTranslationInc;
pSCI->rcCubeBoundary.left =
pSCI->rcCubeBoundary.top = 0;
pSCI->rcCubeBoundary.right = lpcs->cx;
pSCI->rcCubeBoundary.bottom = lpcs->cy;
pSCI->iOptions = SPINCUBE_REPAINT_BKGND;
pSCI->hbmCompat = NULL;
SetWindowLong (hwnd, GWL_SPINCUBEDATA, (LONG) pSCI);
SetTimer (hwnd, SPIN_EVENT, SPIN_INTERVAL, NULL);
//
// Increment the count vars
//
giNumSpincubesThisProcess++;
giNumSpincubesAllProcesses++;
break;
}
case WM_PAINT:
Paint (hwnd);
break;
case WM_TIMER:
switch (wParam)
{
case SPIN_EVENT:
{
PSPINCUBEINFO pSCI = (PSPINCUBEINFO) GetWindowLong (hwnd,
GWL_SPINCUBEDATA);
InvalidateRect (hwnd, &pSCI->rcCubeBoundary, FALSE);
break;
}
}
break;
case WM_LBUTTONDBLCLK:
{
//
// Toggle the erase state of the control
//
if (DO_ERASE(hwnd))
SetWindowLong (hwnd, GWL_STYLE,
GetWindowLong (hwnd, GWL_STYLE) & ~SS_ERASE);
else
{
//
// Repaint the entire control to get rid of the (cube trails) mess
//
PSPINCUBEINFO pSCI = (PSPINCUBEINFO) GetWindowLong (hwnd,
GWL_SPINCUBEDATA);
SetWindowLong (hwnd, GWL_STYLE,
GetWindowLong (hwnd, GWL_STYLE) | SS_ERASE);
pSCI->iOptions |= SPINCUBE_REPAINT_BKGND;
InvalidateRect (hwnd, NULL, FALSE);
SendMessage (hwnd, WM_PAINT, 0, 0);
}
break;
}
case WM_RBUTTONDBLCLK:
{
//
// Toggle the motion state of the control
//
if (IN_MOTION(hwnd))
{
KillTimer (hwnd, SPIN_EVENT);
SetWindowLong (hwnd, GWL_STYLE,
GetWindowLong (hwnd, GWL_STYLE) & ~SS_INMOTION);
}
else
{
SetTimer (hwnd, SPIN_EVENT, SPIN_INTERVAL, NULL);
SetWindowLong (hwnd, GWL_STYLE,
GetWindowLong (hwnd, GWL_STYLE) | SS_INMOTION);
}
break;
}
case WM_SIZE:
if (wParam == SIZE_MAXIMIZED || wParam == SIZE_RESTORED)
{
PSPINCUBEINFO pSCI = (PSPINCUBEINFO) GetWindowLong (hwnd,
GWL_SPINCUBEDATA);
//
// Get a new bitmap which is the new size of our window
//
HDC hdc = GetDC (hwnd);
HBITMAP hbmTemp = CreateCompatibleBitmap (hdc,
(int) LOWORD (lParam),
(int) HIWORD (lParam));
if (!hbmTemp)
{
//
// Scream, yell, & committ an untimely demise...
//
MessageBox (NULL,
GetStringRes (IDS_CREATEBITMAPFAIL),
(LPCTSTR) "SPINCUBE.DLL",
MB_OK | MB_ICONEXCLAMATION);
DestroyWindow (hwnd);
}
pSCI->hbmSave = SelectObject (pSCI->hdcCompat, hbmTemp);
if (pSCI->hbmCompat)
DeleteObject (pSCI->hbmCompat);
ReleaseDC (hwnd, hdc);
pSCI->hbmCompat = hbmTemp;
//
// Reset the translation so the cube doesn't go spinning off into
// space somewhere- we'd never see it again!
//
pSCI->iCurrentXTranslation =
pSCI->iCurrentYTranslation =
pSCI->iCurrentZTranslation = 0;
//
// All these calculations so the cube starts out with random movements,
//
if ((pSCI->iCurrentXTranslationInc = (rand() % 10) + 2) > 7)
pSCI->iCurrentXTranslationInc = -pSCI->iCurrentXTranslationInc;
if ((pSCI->iCurrentYTranslationInc = (rand() % 10) + 2) <= 7)
pSCI->iCurrentYTranslationInc = -pSCI->iCurrentYTranslationInc;
if ((pSCI->iCurrentZTranslationInc = (rand() % 10) + 2) > 7)
pSCI->iCurrentZTranslationInc = -pSCI->iCurrentZTranslationInc;
pSCI->rcCubeBoundary.left =
pSCI->rcCubeBoundary.top = 0;
pSCI->rcCubeBoundary.right = (int) LOWORD (lParam);
pSCI->rcCubeBoundary.bottom = (int) HIWORD (lParam);
pSCI->iOptions |= SPINCUBE_REPAINT_BKGND;
InvalidateRect (hwnd, NULL, FALSE);
}
break;
case WM_DESTROY:
{
PSPINCUBEINFO pSCI = (PSPINCUBEINFO) GetWindowLong (hwnd,
GWL_SPINCUBEDATA);
//
// Clean up all the resources used for this control
//
if (IN_MOTION(hwnd))
KillTimer (hwnd, SPIN_EVENT);
SelectObject (pSCI->hdcCompat, pSCI->hbmSave);
DeleteObject (pSCI->hbmCompat);
DeleteDC (pSCI->hdcCompat);
LocalFree (LocalHandle ((LPVOID) pSCI));
//
// Decrement the global count vars
//
giNumSpincubesThisProcess--;
giNumSpincubesAllProcesses--;
break;
}
default:
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
return ((LONG) TRUE);
}
/******************************************************************************\
*
* FUNCTION: SpincubeDlgProc (standard dialog procedure INPUTS/RETURNS)
*
* COMMENTS: This dialog comes up in response to a user requesting to
* modify the control style. This sample allows for changing
* the control's text, and this is done by modifying the
* CCSTYLE structure pointed at by "gpccs" (a pointer
* that was passed to us by dlgedit).
*
\******************************************************************************/
LRESULT CALLBACK SpincubeDlgProc (HWND hDlg, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG :
{
if (gpccs->flStyle & SS_ERASE)
CheckDlgButton (hDlg, DID_ERASE, 1);
if (gpccs->flStyle & SS_INMOTION)
CheckDlgButton (hDlg, DID_INMOTION, 1);
break;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case DID_ERASE:
if (IsDlgButtonChecked (hDlg, DID_ERASE))
gpccs->flStyle |= SS_ERASE;
else
gpccs->flStyle &= ~SS_ERASE;
break;
case DID_INMOTION:
if (IsDlgButtonChecked (hDlg, DID_INMOTION))
gpccs->flStyle |= SS_INMOTION;
else
gpccs->flStyle &= ~SS_INMOTION;
break;
case DID_OK:
EndDialog (hDlg, 1);
break;
}
break;
}
return FALSE;
}
/******************************************************************************\
*
* FUNCTION: GetStringRes (int id INPUT ONLY)
*
* COMMENTS: Load the resource string with the ID given, and return a
* pointer to it. Notice that the buffer is common memory so
* the string must be used before this call is made a second time.
*
\******************************************************************************/
LPTSTR GetStringRes (int id)
{
static TCHAR buffer[MAX_PATH];
buffer[0]=0;
LoadString (GetModuleHandle (NULL), id, buffer, MAX_PATH);
return buffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -