⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spincube.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************\
*
*  MODULE:      SPINCUBE.C
*
*
*  PURPOSE:     To provide a generic Windows NT dynamic link library
*               sample demonstrating the use of DLL entry points, exported
*               variables, using C runtime in a DLL, etc...
*
*               This module also provides a functional example of how
*               to create a custom control library which may be used by
*               applications (i.e. SPINTEST.EXE) and the Dialog Editor.
*
*
*  FUNCTIONS:   DllMain()      - Registers spincube class when a
*                                      process loads this DLL.
*               CustomControlInfoA() - Called by DLGEDIT to initialize
*                                      a CCINFO structure(s).
*               SpincubeStyle()      - Brings up dialog box which allows
*                                      user to modify control style.
*               SpincubeSizeToText() - Called by DLGEDIT if user requests
*                                      that control be sized to fit text.
*               SpincubeWndProc()    - Window procedure for spincube
*                                      control.
*               SpincubeDlgProc()    - Procedure for control style dialog.
*
*
*  COMMMENTS:   The dialog editor interface has changed since Win 3.0.
*               Recommend browsing the NT CUSTCNTL.H file to get an
*               idea of the new interface.
*
\******************************************************************************/

#include <windows.h>
#include <stdlib.h>
#include "spincube.h"


//
// function prototype for C runtime initialization routine
//

BOOL WINAPI _CRT_INIT (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved);


//
// function prototype for Paint() in PAINT.C
//

void Paint (HWND);

//
// function prototype for looking up string resources
//

LPTSTR GetStringRes (int);


//
// Declared below are the module's 2 exported variables.
//
//   giNumSpincubesThisProcess is an instance variable that contains
//   the number of (existing) Spincube controls created by the
//   current process.
//
//   giNumSpincubesAllProcesses is a shared (between processes) variable
//   which contains the total number of (existing) Spincube controls
//   created by all processes in the system.
//
//

int giNumSpincubesThisProcess = 0;

#pragma data_seg(".MYSEG")

  int giNumSpincubesAllProcesses = 0;

#pragma data_seg()


//
// Some global vars for this module
//

HANDLE    ghMod;   // DLL's module handle
LPCCSTYLE gpccs;   // global pointer to a CCSTYLE structure

CCSTYLEFLAGA aSpincubeStyleFlags[] = { { SS_ERASE,    0, "SS_ERASE"    },
                                       { SS_INMOTION, 0, "SS_INMOTION" } };



/******************************************************************************\
*
*  FUNCTION:    DllMain
*
*  INPUTS:      hDLL       - DLL module handle
*               dwReason   - reason being called (e.g. process attaching)
*               lpReserved - reserved
*
*  RETURNS:     TRUE if initialization passed, or
*               FALSE if initialization failed.
*
*  COMMENTS:    On DLL_PROCESS_ATTACH registers the SPINCUBECLASS
*
*               DLL initialization serialization is guaranteed within a
*               process (if multiple threads then DLL entry points are
*               serialized), but is not guaranteed across processes.
*
*               When synchronization objects are created, it is necesaary
*               to check the return code of GetLastError even if the create
*               call succeeded. If the object existed, ERROR_ALREADY_EXISTED
*               will be returned.
*
*               If your DLL uses any C runtime functions then you should
*               always call _CRT_INIT so that the C runtime can initialize
*               itself appropriately. Failure to do this may result in
*               indeterminate behavior. When the DLL entry point is called
*               for DLL_PROCESS_ATTACH & DLL_THREAD_ATTACH circumstances,
*               _CRT_INIT should be called before any other initilization
*               is performed. When the DLL entry point is called for
*               DLL_PROCESS_DETACH & DLL_THREAD_DETACH circumstances,
*               _CRT_INIT should be called after all cleanup has been
*               performed, i.e. right before the function returns.
*
\******************************************************************************/

BOOL WINAPI DllMain (HANDLE hDLL, DWORD dwReason, LPVOID lpReserved)
{
  ghMod = hDLL;
  switch (dwReason)
  {
    case DLL_PROCESS_ATTACH:
    {
      WNDCLASS wc;

      if (!_CRT_INIT (hDLL, dwReason, lpReserved))

        return FALSE;

      wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC |
                         CS_GLOBALCLASS ;
      wc.lpfnWndProc   = (WNDPROC) SpincubeWndProc;
      wc.cbClsExtra    = 0;
      wc.cbWndExtra    = SPINCUBE_EXTRA;
      wc.hInstance     = hDLL;
      wc.hIcon         = NULL;
      wc.hCursor       = LoadCursor (NULL, IDC_ARROW);
      wc.hbrBackground = NULL;
      wc.lpszMenuName  = (LPSTR) NULL;
      wc.lpszClassName = (LPSTR) SPINCUBECLASS;

      if (!RegisterClass (&wc))
      {
        MessageBox (NULL,
                    GetStringRes (IDS_REGCLASSFAIL),
                    (LPCTSTR) "SPINCUBE.DLL",
                    MB_OK | MB_ICONEXCLAMATION);

        return FALSE;
      }

      break;
    }


    case DLL_PROCESS_DETACH:
    {

      if (!_CRT_INIT (hDLL, dwReason, lpReserved))

        return FALSE;

      if (!UnregisterClass ((LPSTR) SPINCUBECLASS, hDLL ))
      {
        MessageBox (NULL,
                    GetStringRes (IDS_UNREGFAIL),
                    (LPCTSTR) "SPINCUBE.DLL",
                    MB_OK | MB_ICONEXCLAMATION);

        return FALSE;
      }

      break;
    }


    default:

      if (!_CRT_INIT (hDLL, dwReason, lpReserved))

        return FALSE;

      break;
  }
  return TRUE;
}



/******************************************************************************\
*
*  FUNCTION:    CustomControlInfoA
*
*  INPUTS:      acci - pointer to an array od CCINFOA structures
*
*  RETURNS:     Number of controls supported by this DLL
*
*  COMMENTS:    See CUSTCNTL.H for more info
*
\******************************************************************************/

UINT CALLBACK CustomControlInfoA (LPCCINFOA acci)
{
  //
  // Dlgedit is querying the number of controls this DLL supports, so return 1.
  //   Then we'll get called again with a valid "acci"
  //

  if (!acci)

    return 1;


  //
  // Fill in the constant calues.
  //

  acci[0].flOptions         = 0;
  acci[0].cxDefault         = 40;      // default width  (dialog units)
  acci[0].cyDefault         = 40;      // default height (dialog units)
  acci[0].flStyleDefault    = WS_CHILD |
                              WS_VISIBLE |
                              SS_INMOTION;
  acci[0].flExtStyleDefault = 0;
  acci[0].flCtrlTypeMask    = 0;
  acci[0].cStyleFlags       = NUM_SPINCUBE_STYLES;
  acci[0].aStyleFlags       = aSpincubeStyleFlags;
  acci[0].lpfnStyle         = SpincubeStyle;
  acci[0].lpfnSizeToText    = SpincubeSizeToText;
  acci[0].dwReserved1       = 0;
  acci[0].dwReserved2       = 0;


  //
  // Copy the strings
  //
  // NOTE: MAKE SURE THE STRINGS COPIED DO NOT EXCEED THE LENGTH OF
  //       THE BUFFERS IN THE CCINFO STRUCTURE!
  //

  lstrcpy (acci[0].szClass, SPINCUBECLASS);
  lstrcpy (acci[0].szDesc,  SPINCUBEDESCRIPTION);
  lstrcpy (acci[0].szTextDefault, SPINCUBEDEFAULTTEXT);


  //
  // Return the number of controls that the DLL supports
  //

  return 1;
}



/******************************************************************************\
*
*  FUNCTION:    SpincubeStyle
*
*  INPUTS:      hWndParent - handle of parent window (dialog editor)
*               pccs       - pointer to a CCSTYLE structure
*
*  RETURNS:     TRUE  if success,
*               FALSE if error occured
*
*  LOCAL VARS:  rc - return code from DialogBox
*
\******************************************************************************/

BOOL CALLBACK SpincubeStyle (HWND hWndParent, LPCCSTYLE pccs)
{
  int rc;

  gpccs = pccs;

  if ((rc = DialogBox (ghMod, "SpincubeStyle", hWndParent,
                       (DLGPROC)SpincubeDlgProc)) == -1)
  {
    MessageBox (hWndParent,
                GetStringRes (IDS_DLGBOXFAIL),
                (LPCTSTR) "Spincube.dll",
                MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL);
    rc = 0;
  }

  return (BOOL) rc;
}



/******************************************************************************\
*
*  FUNCTION:    SpincubeSizeToText
*
*  INPUTS:      flStyle    - control style
*               flExtStyle - control extended style
*               hFont      - handle of font used to draw text
*               pszText    - control text
*
*  RETURNS:     Width (in pixels) control must be to accomodate text, or
*               -1 if an error occurs.
*
*  COMMENTS:    Just no-op here (since we never actually display text in
*               the control it doesn't need to be resized).
*
\******************************************************************************/

INT CALLBACK SpincubeSizeToText (DWORD flStyle, DWORD flExtStyle,
                                 HFONT hFont,   LPSTR pszText)
{
  return -1;
}



/******************************************************************************\
*
*  FUNCTION:    SpincubeWndProc (standard window procedure INPUTS/RETURNS)
*
*  COMMENTS:    This is the window procedure for our custom control. At
*               creation we alloc a SPINCUBEINFO struct, initialize it,
*               and associate it with this particular control. We also
*               start a timer which will invalidate the window every so
*               often; this causes a repaint, and the cube gets drawn in
*               a new position. Left button clicks will turn toggle the
*               erase option, causing a "trail" of cubes to be left when
*               off. Right button clicks will toggle the motion state of
*               the control (and turn the timer on/off).
*
\******************************************************************************/

LRESULT CALLBACK SpincubeWndProc (HWND hwnd, UINT msg, WPARAM wParam,
                                  LPARAM lParam)
{
  switch (msg)
  {
    case WM_CREATE:
    {
      //
      // Alloc & init a SPINCUBEINFO struct for this particular control
      //

      HDC            hdc;
      LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
      PSPINCUBEINFO  pSCI = (PSPINCUBEINFO) LocalAlloc (LPTR,
                                                        sizeof(SPINCUBEINFO));
      if (!pSCI)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -