copydata.c

来自「一本已经绝版的好书」· C语言 代码 · 共 168 行

C
168
字号
/*************************************************************
Module name: CopyData.C
Notices: Copyright (c) 1995-1997 Jeffrey Richter
*************************************************************/


#include "..\CmnHdr.H"                  /* See Appendix C. */
#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include "Resource.H"


//////////////////////////////////////////////////////////////


// Microsoft does not include message cracker macros for 
// the WM_COPYDATA message in WINDOWSX.H. 
// I have written them here....

/* BOOL Cls_OnCopyData(HWND hwnd, HWND hwndFrom, 
      PCOPYDATASTRUCT cds) */

#define HANDLE_WM_COPYDATA(hwnd, wParam, lParam, fn) \
    ((fn)((hwnd), (HWND)(wParam), \
     (PCOPYDATASTRUCT)lParam), 0L)

#define FORWARD_WM_COPYDATA(hwnd, hwndFrom, cds, fn) \
    (BOOL)(UINT)(DWORD)(fn)((hwnd), WM_COPYDATA, \
      (WPARAM)(hwndFrom), (LPARAM)(cds))


/////////////////////////////////////////////////////////////


BOOL Dlg_OnCopyData(HWND hwnd, HWND hwndFrom, 
   PCOPYDATASTRUCT cds) {
   
   Edit_SetText(
      GetDlgItem(hwnd, cds->dwData ? IDC_DATA2 : IDC_DATA1), 
      cds->lpData);
   return(TRUE);
}


//////////////////////////////////////////////////////////////


BOOL Dlg_OnInitDialog (HWND hwnd, HWND hwndFocus, 
   LPARAM lParam) {

   // Associate an icon with the dialog box.
   chSETDLGICONS(hwnd, IDI_COPYDATA, IDI_COPYDATA);

   // Initialize the edit control with some test data.
   Edit_SetText(GetDlgItem(hwnd, IDC_DATA1), 
      __TEXT("Some test data"));
   Edit_SetText(GetDlgItem(hwnd, IDC_DATA2), 
      __TEXT("Some more test data"));

   return(TRUE);
}


//////////////////////////////////////////////////////////////


void Dlg_OnCommand (HWND hwnd, int id, HWND hwndCtl, 
   UINT codeNotify) {

   HWND hwndEdit, hwndSibling;
   COPYDATASTRUCT cds;
   TCHAR szCaption[100], szCaptionSibling[100];

   switch (id) {
      case IDC_COPYDATA1:
      case IDC_COPYDATA2:
         if (codeNotify != BN_CLICKED)
            break;

         hwndEdit = GetDlgItem(hwnd,
            (id == IDC_COPYDATA1) ? IDC_DATA1 : IDC_DATA2);

         // Prepare the contents of the COPYDATASTRUCT.
         // 0 = ID_DATA1, 1 = ID_DATA2
         cds.dwData = (DWORD) ((id == IDC_COPYDATA1) ? 0 : 1);

         // Get the length of the data block 
         // that we are sending.
         cds.cbData = (Edit_GetTextLength(hwndEdit) + 1) * 
            sizeof(TCHAR);

         // Allocate a block of memory to hold the string.
         cds.lpData = HeapAlloc(GetProcessHeap(),
            HEAP_ZERO_MEMORY, cds.cbData);

         // Put the edit control's string in the data block.
         Edit_GetText(hwndEdit, cds.lpData, cds.cbData);

         // Find the first overlapped window in the list.
         hwndSibling = GetFirstSibling(hwnd);

         // Get the caption of our window.
         GetWindowText(hwnd, szCaption, 
            chDIMOF(szCaption));

         while (IsWindow(hwndSibling)) {
            // Get the caption of the potential 
            // window to send the data to.
            GetWindowText(hwndSibling, szCaptionSibling,
               chDIMOF(szCaptionSibling));

            if (_tcscmp(szCaption, szCaptionSibling) == 0) {
               // If the window's caption is the same as ours, 
               // send the data. This may mean that we are 
               // sending the message to ourselves. This is OK;
               // it demonstrates that WM_COPYDATA can be 
               // used to send data to ourselves.
               FORWARD_WM_COPYDATA(hwndSibling, hwnd, 
                  &cds, SendMessage);
            }

            // Get the handle of the next overlapped window.
            hwndSibling = GetNextSibling(hwndSibling);
         }

         // Free the data buffer.
         HeapFree(GetProcessHeap(), 0, cds.lpData);
         break;

      case IDCANCEL:
         EndDialog(hwnd, id);
         break;
   }
}


//////////////////////////////////////////////////////////////


BOOL CALLBACK Dlg_Proc (HWND hwnd, UINT uMsg, 
   WPARAM wParam, LPARAM lParam) {
   
   switch (uMsg) {
      chHANDLE_DLGMSG(hwnd, WM_INITDIALOG, Dlg_OnInitDialog);
      chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
      chHANDLE_DLGMSG(hwnd, WM_COPYDATA, Dlg_OnCopyData);
   }
   return(FALSE);
}


//////////////////////////////////////////////////////////////


int WINAPI _tWinMain (HINSTANCE hinstExe,
   HINSTANCE hinstPrev, LPTSTR pszCmdLine, int nCmdShow) {

   chWARNIFUNICODEUNDERWIN95();
   DialogBox(hinstExe, MAKEINTRESOURCE(IDD_COPYDATA), 
      NULL, Dlg_Proc);

   return(0);
}


/////////////////////// End Of File ////////////////////////

⌨️ 快捷键说明

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