📄 uconvert.c
字号:
/**************************************************************************\
* uconvert.c -- convert to/from unicode using
* MulitByteToWideChar & WideCharToMulitByte
*
*
\**************************************************************************/
#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[] = "\377\376"; // 0xFF, 0xFE // leave off TEXT() macro.
char szRBOM[] = "\376\377"; // 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("\0"));
}
} break; /* end WM_CREATE */
/**********************************************************************\
* WM_DESTROY
*
* Release the Online help, and free allocated memory if any.
\**********************************************************************/
case WM_DESTROY:
WinHelp( hwnd, szHelpPathName, (UINT) HELP_QUIT, (DWORD) NULL );
ManageMemory (MMFREE, MMSOURCE, 0, pSourceData);
ManageMemory (MMFREE, MMDESTINATION, 0, pDestinationData);
PostQuitMessage(0);
break;
/**********************************************************************\
* WM_CTLCOLOR*
*
* Set the background of the child controls to be gray here.
\**********************************************************************/
case WM_CTLCOLORBTN:
case WM_CTLCOLORSTATIC: {
HDC hdc;
hdc = (HDC) wParam;
SetBkMode (hdc, TRANSPARENT);
return (LRESULT)GetStockObject (LTGRAY_BRUSH);
} break;
/**********************************************************************\
* WM_PAINT
*
* Simply draw the two vertical divider lines, and 3D frame the children.
*
\**********************************************************************/
case WM_PAINT: {
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
GetClientRect (hwnd, &clientrect);
/* draw vertical separator line in the center */
rect.left = (clientrect.right - clientrect.left ) /2 -1;
rect.top = clientrect.top;
rect.right = rect.left +1;;
rect.bottom = clientrect.bottom;
FrameRect (hdc, &rect, GetStockObject (GRAY_BRUSH));
SelectObject (hdc, GetStockObject (WHITE_PEN));
MoveToEx (hdc, rect.right, rect.top, NULL);
LineTo (hdc,rect.right, rect.bottom);
/* draw 3D outlines of child windows. */
framechildwindow (hdc, hwnd, hwndName0);
framechildwindow (hdc, hwnd, hwndSize0);
framechildwindow (hdc, hwnd, hwndCodePage0);
framechildwindow (hdc, hwnd, hwndByteOrder0);
framechildwindow (hdc, hwnd, hwndName1);
framechildwindow (hdc, hwnd, hwndSize1);
framechildwindow (hdc, hwnd, hwndCodePage1);
framechildwindow (hdc, hwnd, hwndByteOrder1);
/* underline the labels */
underlinechildwindow (hdc, hwnd, hwndLabel0);
underlinechildwindow (hdc, hwnd, hwndLabel1);
EndPaint (hwnd, &ps);
} break; /* end WM_PAINT */
/**********************************************************************\
* WMU_ADJUSTFORNEWSOURCE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -