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

📄 uconvert.c

📁 <Win2k系统编程>源码.次数为国人自编,内容丰富,还是不错的.
💻 C
📖 第 1 页 / 共 4 页
字号:
    *
    * lParam - szName of source (file, clipboard, ...)
    *
    * global - nBytesSource
    *
    * "user message."  Set the text of the Source windows
    \**********************************************************************/
    case WMU_ADJUSTFORNEWSOURCE: {
      LPVOID szName;

      szName = (LPVOID) lParam;

      /* Set Window text appropriately */
      SetWindowText (hwndName0, szName);
      wsprintf (buffer, LoadResourceString(IDS_BYTES), nBytesSource);
      SetWindowText (hwndSize0, buffer);
      SetWindowText (hwndByteOrder0, szBlank);

      /* Clear the destination data if any to avoid user confusion. */
      SendMessage (hwnd, WM_COMMAND, MID_CLEARDESTINATION, 0);

      /* Reset the "type strings" based on new gTypeSource. */
      SendMessage (hwnd, WMU_SETTYPESTRINGS, 0,0);
    } break;




    /**********************************************************************\
    *  WMU_SETTYPESTRINGS
    *
    * "user message."  Set the text of the "type" windows to reflect
    *  the state stored in gTypeSource and gi*CodePage.
    *
    \**********************************************************************/
    case WMU_SETTYPESTRINGS:
      switch (gTypeSource) {
        case TYPEUNICODE:
          SetWindowText (hwndCodePage0, TEXT("Unicode"));
          wsprintf (buffer, LoadResourceString(IDS_CODE_PAGE),
                  giDestinationCodePage);
          SetWindowText (hwndCodePage1, buffer);
          SetWindowText (hwnd, TitleWCToMB);
        break;
        case TYPECODEPAGE:
          wsprintf (buffer, LoadResourceString(IDS_CODE_PAGE),
                  giSourceCodePage);
          SetWindowText (hwndCodePage0, buffer);
          SetWindowText (hwndCodePage1, TEXT("Unicode"));
          SetWindowText (hwnd, TitleMBToWC);
        break;
        case TYPEUNKNOWN:
          SetWindowText (hwndCodePage0, szBlank);
          SetWindowText (hwndCodePage1, szBlank);
          SetWindowText (hwnd, TitleUnknown);
        break;
      } /* end switch gTypeSource */
    break;


    /**********************************************************************\
    *  WM_INITMENU
    *
    * Manage the enabled state of all of the menus.
    *  Notice that the button enabled state is taken care of in ManageMemory().
    *
    * In general, this is dependent upon pSourceData & pDestinationData.
    *  They are either NULL or non-NULL, and menu items are dependent upon
    *  this state.
    *
    * One exception is the "Paste from Clipboard menu" which is enabled
    *  conditional upon there being text data in the clipboard.
    *
    \**********************************************************************/
    case WM_INITMENU:

      /* Adjust the "Paste from Clipboard menu" */
      OpenClipboard (hwnd);
      if (IsClipboardFormatAvailable (CF_UNICODETEXT) ||
          IsClipboardFormatAvailable (CF_OEMTEXT) ||
          IsClipboardFormatAvailable (CF_TEXT))
        EnableMenuItem (GetMenu (hwnd),MID_PASTESOURCE,      MF_ENABLED);
      else
        EnableMenuItem (GetMenu (hwnd),MID_PASTESOURCE,      MF_GRAYED);
      CloseClipboard ();


      /* Adjust the source data dependent menus. */
      if (pSourceData != NULL) {
        EnableMenuItem (GetMenu (hwnd),MID_SOURCEOPT,        MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_SWAPSOURCE,       MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_CLEARSOURCE,      MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_CONVERTNOW,       MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_CONVERSIONOPT,    MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_DESTINATIONOPT,   MF_ENABLED);
      } else {
        EnableMenuItem (GetMenu (hwnd),MID_SOURCEOPT,        MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_SWAPSOURCE,       MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_CLEARSOURCE,      MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_CONVERTNOW,       MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_CONVERSIONOPT,    MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_DESTINATIONOPT,   MF_GRAYED);
      }


      /* Adjust the destination data dependent menus. */
      if (pDestinationData != NULL) {
        EnableMenuItem (GetMenu (hwnd),MID_SAVEAS,           MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_SWAPDESTINATION,  MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_COPYDESTINATION,  MF_ENABLED);
        EnableMenuItem (GetMenu (hwnd),MID_CLEARDESTINATION, MF_ENABLED);
      } else {
        EnableMenuItem (GetMenu (hwnd),MID_SAVEAS,           MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_SWAPDESTINATION,  MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_COPYDESTINATION,  MF_GRAYED);
        EnableMenuItem (GetMenu (hwnd),MID_CLEARDESTINATION, MF_GRAYED);
      }

    break;





    /**********************************************************************\
    *  WM_COMMAND
    *
    * Just switch() on the command ID.  Notice that menu and button
    *  command messages are treated the same.
    *
    \**********************************************************************/
    case WM_COMMAND:
      switch (LOWORD(wParam)) {


        /******************************************************************\
        *  WM_COMMAND, MID_OPEN
        *
        * Put up common dialog, try to open & read file.
        *  Fill windows with correct text & fill pSourceData.
        \******************************************************************/
        case MID_OPEN  : {
          HANDLE hFile;
          DWORD nBytesRead;
          TCHAR szFile[MAX_PATH],szFileTitle[MAX_PATH];

          /* First set up the structure for the GetOpenFileName
           *  common dialog.
           */
          {
            OPENFILENAME OpenFileName;
            /* buffers for the file names. */

            wsprintf (szFile, szBlank);
            wsprintf (szFileTitle, szBlank);


            OpenFileName.lStructSize       = sizeof(OPENFILENAME);
            OpenFileName.hwndOwner         = hwnd;
            OpenFileName.hInstance         = (HANDLE) hInst;
            OpenFileName.lpstrFilter       = szFilter; // built in WM_CREATE
            OpenFileName.lpstrCustomFilter = NULL;
            OpenFileName.nMaxCustFilter    = 0L;
            OpenFileName.nFilterIndex      = 1L;
            OpenFileName.lpstrFile         = szFile;
            OpenFileName.nMaxFile          = MAX_PATH;
            OpenFileName.lpstrFileTitle    = szFileTitle;
            OpenFileName.nMaxFileTitle     = MAX_PATH;
            OpenFileName.lpstrInitialDir   = NULL;
            OpenFileName.lpstrTitle        = LoadResourceString(IDS_OPEN_FILE_TITLE);

            OpenFileName.nFileOffset       = 0;
            OpenFileName.nFileExtension    = 0;
            OpenFileName.lpstrDefExt       = NULL;

            OpenFileName.lCustData         = 0;
            OpenFileName.lpfnHook          = NULL;
            OpenFileName.lpTemplateName    = NULL;

            OpenFileName.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST;

            if (!GetOpenFileName(&OpenFileName)) return 0;
          }


          /* User has filled in the file information.
           *  Try to open that file for reading.
           */
          hFile = CreateFile(szFile,
                       GENERIC_READ,
                       0,
                       NULL,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
          if (hFile == INVALID_HANDLE_VALUE) {
            MessageBox (hwnd, LoadResourceString(IDS_OPEN_FILE_FAILED),
                    MBTitle, MBFlags);
            return 0;
          }


          /* make sure file is not too big... i.e. > 2^32
           *  If it is OK, write the file size in hwndSize0
           */
          {
            BY_HANDLE_FILE_INFORMATION bhfi;

            GetFileInformationByHandle (hFile, &bhfi);
            if (bhfi.nFileSizeHigh != 0) {
              MessageBox (hwnd, LoadResourceString(IDS_FILE_TOO_BIG),
                      MBTitle, MBFlags);
              CloseHandle (hFile);
              return 0;
            }

            nBytesSource= bhfi.nFileSizeLow;

          }

          /* Allocate space for string, including potential UNICODE_NULL */
          pSourceData = ManageMemory (MMALLOC, MMSOURCE, nBytesSource +2, pSourceData);
          if (pSourceData == NULL) {
            CloseHandle (hFile);
            return 0;
          }

          if (nBytesSource < SIZEOFBOM) {
              gTypeSource = TYPEUNKNOWN;
              goto no_bom;
          }

          /* first read two bytes and look for BOM */
          if (!ReadFile (hFile, pSourceData,SIZEOFBOM, &nBytesRead, NULL)) {
            MessageBox (hwnd, LoadResourceString(IDS_READFILE_FAILED),
                    MBTitle, MBFlags);
            CloseHandle (hFile);
            pSourceData = ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
            return 0;
          }



          /* If file begins with BOM, then we know the type,
           *  we'll decrement the number of bytes by two,
           *  and read the rest of the data.
           */
          if (IsBOM (pSourceData)) {
            gTypeSource = TYPEUNICODE;
            nBytesSource -=SIZEOFBOM;

          /* If file begins with Reverse BOM, then we know the type,
           *  we'll decrement the number of bytes by two,
           *  and read the rest of the data, and post a message so
           *  that we know to swap the order later.
           */
          } else if (IsRBOM (pSourceData)) {
            gTypeSource = TYPEUNICODE;
            nBytesSource -=SIZEOFBOM;
            MessageBox (hwnd, LoadResourceString(IDS_SWAPPING_BYTE_ORDER),
                    MBTitle, MBFlags);
            PostMessage (hwnd, WM_COMMAND, MID_SWAPSOURCE, 0);

          /* Oops, does not begin with BOM.
           *  Reset file pointer, and read data.
           */
          } else {
            gTypeSource = TYPEUNKNOWN;
            SetFilePointer (hFile, -SIZEOFBOM, NULL, FILE_CURRENT);
          }

          no_bom:


          /* try to read all of it into memory */
          if (!ReadFile (hFile, pSourceData,nBytesSource, &nBytesRead, NULL)) {
            MessageBox (hwnd, LoadResourceString(IDS_READFILE_FAILED),
                    MBTitle, MBFlags);
            CloseHandle (hFile);
            pSourceData = ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
            return 0;
          }

          CloseHandle (hFile);

          /* If we don't know the file type at this point,
           *  try to determine if it is unicode.
           */
          if (gTypeSource == TYPEUNKNOWN) {
            if (IsUnicode (pSourceData)) {
              gTypeSource = TYPEUNICODE;
              pSourceData[nBytesSource]   = 0;  // UNICODE_NULL
              pSourceData[nBytesSource+1] = 0;
            } else {
              gTypeSource = TYPECODEPAGE;
              pSourceData[nBytesSource] = 0;
            }
          }

          SendMessage (hwnd, WMU_ADJUSTFORNEWSOURCE, 0, (LPARAM)szFile);



        } break;  /* end case MID_OPEN */



        /******************************************************************\
        *  WM_COMMAND, MID_SAVEAS
        *
        * Put up common dialog, try to open file, and write data to it.
        \******************************************************************/
        case MID_SAVEAS: {
          HANDLE hFile;
          DWORD nBytesRead;
          TCHAR szFile[MAX_PATH],szFileTitle[MAX_PATH];

          if (nBytesDestination == NODATA ) {
            MessageBox (hwnd, LoadResourceString(IDS_NOTEXT_TO_SAVE),
                    MBTitle, MBFlags);
            return 0;
          }


          /* Set up the structure for the GetSaveFileName
           *  common dialog.
           */
          {
            OPENFILENAME OpenFileName;
            /* buffers for the file names. */

            wsprintf (szFile, szBlank);
            wsprintf (szFileTitle, szBlank);

            OpenFileName.lStructSize       = sizeof(OPENFILENAME);
            OpenFileName.hwndOwner         = hwnd;
            OpenFileName.hInstance         = (HANDLE) hInst;
            OpenFileName.lpstrFilter       = szFilter;
            OpenFileName.lpstrCustomFilter = NULL;
            OpenFileName.nMaxCustFilter    = 0L;
            OpenFileName.nFilterIndex      = 1L;
            OpenFileName.lpstrFile         = szFile;
            OpenFileName.nMaxFile          = MAX_PATH;
            OpenFileName.lpstrFileTitle    = szFileTitle;
            OpenFileName.nMaxFileTitle     = MAX_PATH;
            OpenFileName.lpstrInitialDir   = NULL;
            OpenFileName.lpstrTitle        = LoadResourceString(IDS_SAVE_AS_TITLE);

            OpenFileName.nFileOffset       = 0;
            OpenFileName.nFileExtension    = 0;
            OpenFileName.lpstrDefExt       = NULL;

            OpenFileName.lCustData         = 0;
            OpenFileName.lpfnHook          = NULL;
            OpenFileName.lpTemplateName    = NULL;

            OpenFileName.Flags = OFN_HIDEREADONLY;

            if (!GetSaveFileName(&OpenFileName)) return 0;
          }


          /* User has filled in the file information.
           *  Try to open that file for writing.
           */
          hFile = CreateFile(szFile,
                      GENERIC_WRITE,
                      0,

⌨️ 快捷键说明

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