📄 uconvert.c
字号:
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
MessageBox (hwnd, LoadResourceString(IDS_CREATEFILE_FAILED),
MBTitle, MBFlags);
return 0;
}
/* if destination is unicode, try to write BOM first.
* unless the bytes have been swapped
* (criterion: hwndByteOrder contains text)
* in which case, write a Reverse Byte Order Mark.
*/
if (gTypeSource == TYPECODEPAGE) {
if (GetWindowTextLength (hwndByteOrder1) == 0) {
if (!WriteFile (hFile, szBOM, SIZEOFBOM, &nBytesRead, NULL)) {
MessageBox (hwnd, LoadResourceString(IDS_WRITEFILE_FAILED),
MBTitle, MBFlags);
CloseHandle (hFile);
return 0;
}
}else {
if (!WriteFile (hFile, szRBOM, SIZEOFBOM, &nBytesRead, NULL)) {
MessageBox (hwnd, LoadResourceString(IDS_WRITEFILE_FAILED),
MBTitle, MBFlags);
CloseHandle (hFile);
return 0;
}
}
}
/* try to write all of it into memory */
if (!WriteFile (hFile, pDestinationData,nBytesDestination, &nBytesRead, NULL)) {
MessageBox (hwnd, LoadResourceString(IDS_WRITEFILE_FAILED),
MBTitle, MBFlags);
CloseHandle (hFile);
return 0;
}
SetWindowText (hwndName1, szFile);
CloseHandle (hFile);
} break;
/**********************************************************************\
* WM_COMMAND, MID_PASTESOURCE
*
* Paste the clipboard's prefered data format into the source.
* Fills pSourceData.
\**********************************************************************/
case MID_PASTESOURCE: {
UINT iFormat;
PVOID pData;
OpenClipboard (hwnd);
iFormat = 0;
while (iFormat = EnumClipboardFormats(iFormat))
if ((iFormat == CF_UNICODETEXT) || (iFormat == CF_OEMTEXT) || (iFormat == CF_TEXT)) {
HGLOBAL hMem;
hMem = GetClipboardData (iFormat);
pData = GlobalLock(hMem);
switch (iFormat) {
case CF_UNICODETEXT:
nBytesSource = lstrlenW (pData) *2;
pSourceData= ManageMemory (MMALLOC, MMSOURCE, nBytesSource+2, pSourceData);
lstrcpyW ((LPVOID)pSourceData, pData);
gTypeSource = TYPEUNICODE;
break;
case CF_OEMTEXT:
nBytesSource = lstrlenA (pData);
pSourceData= ManageMemory (MMALLOC, MMSOURCE, nBytesSource+1, pSourceData);
lstrcpyA (pSourceData, pData);
gTypeSource = TYPECODEPAGE;
giSourceCodePage = GetOEMCP();
break;
case CF_TEXT:
nBytesSource = lstrlenA (pData);
pSourceData= ManageMemory (MMALLOC, MMSOURCE, nBytesSource+1, pSourceData);
lstrcpyA (pSourceData, pData);
gTypeSource = TYPECODEPAGE;
giSourceCodePage = GetACP();
break;
default: break; // shouldn't get here
} /* end switch (iFormat) */
SendMessage (hwnd, WMU_ADJUSTFORNEWSOURCE, 0,
(LPARAM)LoadResourceString(IDS_FROM_CLIPBOARD));
GlobalUnlock(hMem);
break; /* break out of while loop. */
} /* end if iFormat */
CloseClipboard ();
} break;
/**********************************************************************\
* WM_COMMAND, MID_COPYDESTINATION
*
* Copy destination data to the clipboard.
\**********************************************************************/
case MID_COPYDESTINATION:
{
HGLOBAL hMem;
if (pDestinationData == NULL) return FALSE;
if (gTypeSource != TYPEUNICODE) {
if (!(hMem = GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
(lstrlenW((LPWSTR)pDestinationData)+1) * 2))) {
return FALSE;
}
lstrcpyW(GlobalLock(hMem), (LPWSTR)pDestinationData);
GlobalUnlock(hMem);
} else {
if (!(hMem = GlobalAlloc(
GMEM_MOVEABLE | GMEM_DDESHARE,
lstrlenA(pDestinationData)+1))) {
return FALSE;
}
lstrcpyA(GlobalLock(hMem), pDestinationData);
GlobalUnlock(hMem);
}
OpenClipboard (hwnd);
EmptyClipboard();
/* if source NOT unicode, then destination is, else look at dest CP */
if (gTypeSource != TYPEUNICODE)
SetClipboardData (CF_UNICODETEXT, hMem);
else if (giDestinationCodePage == GetOEMCP())
SetClipboardData (CF_OEMTEXT, hMem);
else
SetClipboardData (CF_TEXT, hMem);
CloseClipboard ();
break;
}
/******************************************************************\
* WM_COMMAND, MID_CONVERTNOW
*
* This is where the conversion actually takes place.
* In either case, make the call twice. Once to determine how
* much memory is needed, allocate space, and then make the call again.
*
* If conversion succeeds, it fills pDestinationData.
\******************************************************************/
case MID_CONVERTNOW: {
int nBytesNeeded, nWCharNeeded, nWCharSource;
if (nBytesSource == NODATA ) {
MessageBox (hwnd, LoadResourceString(IDS_LOAD_SOURCE_FILE),
MBTitle, MBFlags);
return 0;
}
/* Converting UNICODE -> giDestinationCodePage*/
if (gTypeSource == TYPEUNICODE)
{
nWCharSource = nBytesSource/2;
/* Query the number of bytes required to store the Dest string */
nBytesNeeded = WideCharToMultiByte(giDestinationCodePage, gWCFlags,
(LPWSTR)pSourceData, nWCharSource,
NULL, 0,
glpDefaultChar, &gUsedDefaultChar);
/* Allocate the required amount of space */
if (nBytesNeeded == 0)
{
MessageBox (hwnd, LoadResourceString(IDS_FIRSTCALL_FAILED),
MBTitle, MBFlags);
break;
}
/* We need more 1 byte for '\0' */
pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, nBytesNeeded + 2, pDestinationData);
/* Do the conversion */
nBytesDestination = WideCharToMultiByte(giDestinationCodePage, gWCFlags,
(LPWSTR)pSourceData, nWCharSource,
pDestinationData, nBytesNeeded, glpDefaultChar, &gUsedDefaultChar);
if (nBytesNeeded == 0) {
MessageBox (hwnd, LoadResourceString(IDS_FIRSTCALL_FAILED),
MBTitle, MBFlags);
break;
}
*(LPSTR)((LPSTR)pDestinationData + nBytesNeeded) = '\0';
}
/* converting giSourceCodePage -> UNICODE */
else if (gTypeSource == TYPECODEPAGE)
{
/* Query the number of WChar required to store the Dest string */
nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags,
pSourceData, nBytesSource, NULL, 0 );
/* Allocate the required amount of space */
/* We need more 2 bytes for '\0' */
pDestinationData= ManageMemory (MMALLOC, MMDESTINATION, (nWCharNeeded+1)*2, pDestinationData);
/* Do the conversion */
nWCharNeeded = MultiByteToWideChar(giSourceCodePage, gMBFlags,
pSourceData, nBytesSource,
(LPWSTR)pDestinationData, nWCharNeeded);
*(LPWSTR)((LPWSTR)pDestinationData + nWCharNeeded) = L'\0';
/* MultiByteToWideChar returns # WCHAR, so multiply by 2 */
nBytesDestination = 2*nWCharNeeded ;
} else {
MessageBox (hwnd, LoadResourceString(IDS_SOURCE_TYPE_UNKNOWN),
MBTitle, MBFlags);
return 0;
}
/* code common to all conversions... */
SetWindowText (hwndName1, LoadResourceString(IDS_DATA_NOT_SAVED));
wsprintf (buffer, LoadResourceString(IDS_BYTES), nBytesDestination);
SetWindowText (hwndSize1, buffer);
SetWindowText (hwndByteOrder1, szBlank);
/* Throw up "Save as" dialog to help the user along.
* They can always <esc> if need be.
*/
SendMessage (hwnd, WM_COMMAND, MID_SAVEAS, 0);
} break; /* end case BID_CONVERT */
/******************************************************************\
* WM_COMMAND, BID_VIEWSOURCE
*
\******************************************************************/
case BID_VIEWSOURCE:
if (gTypeSource == TYPEUNICODE)
DialogBoxW (hInst, L"ShowTextDlg", hwnd, (DLGPROC)ViewSourceProc);
else
DialogBoxA (hInst, "ShowTextDlg", hwnd, (DLGPROC)ViewSourceProc);
break;
/******************************************************************\
* WM_COMMAND, BID_VIEWDESTINATION
*
\******************************************************************/
case BID_VIEWDESTINATION:
if (gTypeSource == TYPEUNICODE)
DialogBoxA (hInst, "ShowTextDlg", hwnd, (DLGPROC)ViewDestinationProc);
else
DialogBoxW (hInst, L"ShowTextDlg", hwnd, (DLGPROC)ViewDestinationProc);
break;
/******************************************************************\
* WM_COMMAND, MID_SOURCEOPT
*
* Allows user to change interpretation options for the source data.
*
* Put up appropriate dialog box, and reset window text in response.
\******************************************************************/
case MID_SOURCEOPT:
if (DialogBox (hInst, TEXT("DataOptionsDlg"), hwnd, (DLGPROC)SourceOptionsProc)) {
SendMessage (hwnd, WMU_SETTYPESTRINGS, 0,0);
SendMessage (hwnd, WM_COMMAND, MID_CLEARDESTINATION, 0);
}
break;
/******************************************************************\
* WM_COMMAND, MID_DESTINATIONOPT
*
* Allows user to change options for destination data.
*
* Put up appropriate dialog box, and reset window text in response.
\******************************************************************/
case MID_DESTINATIONOPT:
if (DialogBox (hInst, TEXT("DataOptionsDlg"), hwnd, (DLGPROC)DestinationOptionsProc)) {
SendMessage (hwnd, WMU_SETTYPESTRINGS, 0,0);
SendMessage (hwnd, WM_COMMAND, MID_CLEARDESTINATION, 0);
}
break;
/******************************************************************\
* WM_COMMAND, MID_CONVERSIONOPT
*
\******************************************************************/
case MID_CONVERSIONOPT:
if (DialogBox (hInst, TEXT("ConversionOptionsDlg"), hwnd, (DLGPROC)ConversionOptionsProc)) {
SendMessage (hwnd, WM_COMMAND, MID_CLEARDESTINATION, 0);
}
break;
/******************************************************************\
* WM_COMMAND, MID_SWAPSOURCE
*
* Allows user to reverse byte order of data.
*
\******************************************************************/
case MID_SWAPSOURCE: {
int i, end;
BYTE temp;
if (pSourceData == NULL) return FALSE;
end = nBytesSource - 2;
for (i = 0; i<= end; i+=2) {
temp = pSourceData[i];
pSourceData[i] = pSourceData[i+1];
pSourceData[i+1] = temp;
}
if (GetWindowTextLength (hwndByteOrder0) == 0)
SetWindowText (hwndByteOrder0,
LoadResourceString(IDS_BYTE_ORDER_REVERSED));
else
SetWindowText (hwndByteOrder0, szBlank);
/* Since source is different, invalidate Destination data. */
SendMessage (hwnd, WM_COMMAND, MID_CLEARDESTINATION, 0);
} break;
/******************************************************************\
* WM_COMMAND, MID_SWAPDESTINATION
*
* Allows user to reverse byte order of data.
*
\******************************************************************/
case MID_SWAPDESTINATION: {
int i, end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -