UCONVERT.C
资源名称:Uconvert.zip [点击查看]
上传用户:shijixite
上传日期:2007-01-03
资源大小:108k
文件大小:48k
源码类别:
输入法编程
开发平台:
Visual C++
- /**************************************************************************
- * uconvert.c -- convert to/from unicode using
- * MulitByteToWideChar & WideCharToMulitByte
- *
- * Steve Firebaugh
- * Microsoft Developer Support
- * Copyright (c) 1992-1997 Microsoft Corporation
- *
- **************************************************************************/
- #define UNICODE
- #include <windows.h>
- #include <commdlg.h>
- #include "uconvert.h"
- #include "install.h"
- /**************************************************************************
- * Global variables.
- **************************************************************************/
- HANDLE hInst;
- /* declare global HWNDs for the child windows.
- * They are created at WM_CREATE of the main window.
- * Also used in the "View" dialogs.
- */
- HWND hwndLabel0, hwndLabel1;
- HWND hwndName0, hwndName1;
- HWND hwndSize0, hwndSize1;
- HWND hwndCodePage0, hwndCodePage1;
- HWND hwndByteOrder0, hwndByteOrder1;
- HWND hwndButton0, hwndButton1;
- /* Global variables storing the source and destination "type" information.
- *
- * used to communicate between main wnd proc, and *OptionsProc.
- *
- * gTypeSource - stores the type interpretation of the source data
- * (and implicitly the destination data.)
- * TYPEUNKNOWN: indeterminant... not set. Can not do conversion.
- * TYPEUNICODE: source unicode & destination giDestinationCodePage.
- * TYPECODEPAGE: source giSourceCodePage & destination unicode.
- *
- * giSourceCodePage stores valid source code page iff gTypeSource == TRUE
- * giDestinationCodePage stores valid destination code page iff gTypeSource == FALSE
- *
- */
- int gTypeSource;
- UINT giSourceCodePage;
- UINT giDestinationCodePage;
- /* Pointers to the source and destination data, and the
- * count of bytes in each of the buffers.
- */
- #define NODATA 0
- PBYTE pSourceData = NULL;
- PBYTE pDestinationData = NULL;
- int nBytesSource = NODATA;
- int nBytesDestination = NODATA;
- /* Conversion Options variables. */
- DWORD gMBFlags = MB_PRECOMPOSED;
- DWORD gWCFlags = 0;
- char glpDefaultChar[4] = "?";
- BOOL gUsedDefaultChar = FALSE;
- /* Handling the Byte Order Mark (BOM).
- *
- * If the input file begins with a BOM, then we know it is unicode,
- * we skip over the BOM and decrement the size of data by SIZEOFBOM.
- *
- *
- * Before writing data that we know is unicode, write the szBOM string
- * to the file.
- *
- * Notice that this means that the file sizes we show in the window
- * do NOT include the BOM.
- */
- char szBOM[] = "377376"; // 0xFF, 0xFE // leave off TEXT() macro.
- char szRBOM[] = "376377"; // 0xFF, 0xFE // leave off TEXT() macro.
- #define SIZEOFBOM 2
- /* Title of main window */
- TCHAR TitleMBToWC[]= TEXT("UConvert -- MultiByteToWideChar()");
- TCHAR TitleWCToMB[]= TEXT("UConvert -- WideCharToMultiByte()");
- TCHAR TitleUnknown[]= TEXT("UConvert.");
- /* file name of the online help file */
- TCHAR szHelpPathName[] = TEXT("uconvert.HLP");
- /* Strings used to fill onscreen windows. */
- TCHAR szBlank[] = TEXT("");
- /* MessageBox() strings and flags. */
- TCHAR MBTitle[30]= TEXT("");
- UINT MBFlags = MB_OK | MB_ICONEXCLAMATION;
- /* misc. defines affecting size and placement of child windows */
- #define BORDER GetSystemMetrics (SM_CXFRAME)*4
- #define WHEIGHT GetSystemMetrics (SM_CYMENU)
- /**************************************************************************
- *
- * function: WinMain()
- *
- *
- **************************************************************************/
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
- LPSTR lpCmdLine, int nCmdShow)
- {
- MSG msg;
- WNDCLASS wc;
- HWND hwndMain;
- HACCEL haccel;
- UNREFERENCED_PARAMETER( lpCmdLine );
- UNREFERENCED_PARAMETER( nCmdShow );
- hInst = hInstance;
- /* Check for previous instance. If none, then register class. */
- if (!hPrevInstance) {
- wc.style = 0;
- wc.lpfnWndProc = (WNDPROC)MainWndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = LoadIcon(hInstance, TEXT("uconvertIcon"));
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = GetStockObject (LTGRAY_BRUSH);
- wc.lpszMenuName = TEXT("uconvertMenu");
- wc.lpszClassName = TEXT("uconvert");
- if (!RegisterClass(&wc)) return (FALSE);
- } /* class registered o.k. */
- /* Create the main window. Return false if CreateWindow() fails */
- hwndMain = CreateWindow(
- TEXT("uconvert"),
- TitleUnknown,
- (WS_OVERLAPPEDWINDOW & ~(WS_THICKFRAME | WS_MAXIMIZEBOX)) | WS_VISIBLE,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 512, // Big enough for most of the text.
- 16*WHEIGHT,
- NULL, NULL, hInst, NULL);
- if (!hwndMain) return (FALSE);
- /* Load the accelerator table that provides clipboard support. */
- haccel = LoadAccelerators (hInst, TEXT("uconvertAccel"));
- LoadString(hInst,IDS_APP_WARNING,MBTitle,sizeof(MBTitle));
- /* Loop getting messages and dispatching them. */
- while (GetMessage(&msg, NULL, 0,0)) {
- if (!TranslateAccelerator(hwndMain, haccel, &msg)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (msg.wParam);
- }
- /**************************************************************************
- *
- * function: MainWndProc()
- *
- *
- * On WM_CREATE create all of the child windows.
- * On WM_DESTROY make sure that all dynamically allocated memory is freed.
- * On WM_PAINT, outline many of the child windows.
- * On WM_COMMAND, respond to the command messages properly.
- *
- **************************************************************************/
- LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- /* misc. variables used in multiple messages cases. */
- RECT clientrect;
- RECT rect;
- TCHAR buffer[50];
- static TCHAR szFilter[MAX_PATH];
- switch (message) {
- /**********************************************************************
- * WM_CREATE
- *
- * Create all of the child windows used on this main window.
- * Assign the HWNDs to the correct static variables.
- *
- **********************************************************************/
- case WM_CREATE: {
- GetClientRect (hwnd, &clientrect);
- /* Create Source Windows. */
- CopyRect (&rect, &clientrect);
- rect.right = (clientrect.right - clientrect.left) /2;
- InflateRect (&rect, -2*BORDER, -BORDER);
- createwindows(&rect,
- hwnd,
- &hwndLabel0,
- &hwndName0,
- &hwndSize0,
- &hwndCodePage0,
- &hwndByteOrder0,
- &hwndButton0);
- /* Create Destination Windows. */
- CopyRect (&rect, &clientrect);
- rect.left = (clientrect.right - clientrect.left) /2;
- InflateRect (&rect, -2*BORDER, -BORDER);
- createwindows(&rect,
- hwnd,
- &hwndLabel1,
- &hwndName1,
- &hwndSize1,
- &hwndCodePage1,
- &hwndByteOrder1,
- &hwndButton1);
- /* fill in window information that is different for source/destination */
- SetWindowText (hwndLabel0, LoadResourceString(IDS_SOURCE));
- SetWindowText (hwndLabel1, LoadResourceString(IDS_DESTINATION));
- SetWindowText (hwndButton0, LoadResourceString(IDS_VIEW_SOURCE_BTN));
- SetWindowText (hwndButton1, LoadResourceString(IDS_VIEW_DESTINATION_BTN));
- SetWindowLong (hwndButton0, GWL_ID, BID_VIEWSOURCE );
- SetWindowLong (hwndButton1, GWL_ID, BID_VIEWDESTINATION );
- gTypeSource = TYPEUNKNOWN;
- giSourceCodePage = GetACP(); // Just some reasonable initializer.
- giDestinationCodePage = GetACP(); // Just some reasonable initializer.
- /* initialize source & destination data correctly */
- SendMessage (hwnd, WM_COMMAND, MID_CLEARSOURCE, 0);
- SendMessage (hwnd, WM_COMMAND, MID_CLEARDESTINATION, 0);
- /* Build up the correct filter strings for OPENFILENAME structure
- * Do it here so that we only have to do it once.
- */
- {
- TCHAR *p;
- p = szFilter;
- lstrcpy (buffer,LoadResourceString(IDS_FILE_FILTER_SPEC1));
- lstrcpy (p,buffer);
- p += lstrlen (buffer) +1;
- lstrcpy (buffer,TEXT("*.*"));
- lstrcpy (p,buffer);
- p += lstrlen (buffer) +1;
- lstrcpy (buffer,LoadResourceString(IDS_FILE_FILTER_SPEC2));
- lstrcpy (p,buffer);
- p += lstrlen (buffer) +1;
- lstrcpy (buffer,TEXT("*.txt"));
- lstrcpy (p,buffer);
- p += lstrlen (buffer) +1;
- lstrcpy (buffer,LoadResourceString(IDS_FILE_FILTER_SPEC3));
- lstrcpy (p,buffer);
- p += lstrlen (buffer) +1;
- lstrcpy (buffer,TEXT("*.utf"));
- lstrcpy (p,buffer);
- p += lstrlen (buffer) +1;
- lstrcpy (p,TEXT("