Snussi » 21.03.2005 (Пн) 10:19
Статья из MSDN как конвертнуть PWI в RTF
Appendix: Converting RichInk PWI Data
The following pages describe a process for converting the RichInk data found in .pwi (Pocket Word Ink) files to RTF or Text formats. These instructions are intended for knowledgeable Windows programmers who are familiar with the messaging architecture in Windows.
Preliminary notes
The RichInk DLL is installed on the desktop with the Windows CE Services and exists in ROM on Windows CE devices. The RichInk.dll can be used for converting from the RichInk internal format (.pwi files) to either .rtf or text file formats.
On the desktop, ink data containing ink strokes is converted to RTF metafile data. This technique is not currently supported on the device, so data converted on the device will lose all non-text ink data.
If you want to use RichInk technology to convert files on the desktop, use InkEng.dll (found in the ActiveSync folder) rather than RichInk.dll. InkEng.dll maintains the same interfaces for EM_STREAMIN and EM_STREAMOUT as the device uses.
Conversion process
// Defines and typedefs needed for the conversion:
#define EM_STREAMIN (WM_USER + 73)
#define EM_STREAMOUT (WM_USER + 74)
typedef DWORD (CALLBACK *EDITSTREAMCALLBACK)
(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb);
typedef struct _editstream
{
DWORD dwCookie; /* user value passed to callback as first parameter */
DWORD dwError; /* last error */
EDITSTREAMCALLBACK pfnCallback;
} EDITSTREAM;
typedef struct tagCOOKIE
{
HANDLE hFile;
LPBYTE pbStart;
LPBYTE pbCur;
LONG bCount;
DWORD dwError;
} COOKIE, * PCOOKIE;
// Stream formats:
#define SF_TEXT 0x0001
#define SF_RTF 0x0002
#define SF_UNICODE 0x0010 // Unicode data of some kind
#define SF_UTEXT SF_TEXT | SF_UNICODE // Unicode text file
#define SFF_PWI 0x0800
#define SF_PWI ( SF_RTF | SFF_PWI | 0x010000 ) // PWI format
The first step is to load the RichInk DLL:
HINSTANCE hLib;
hLib = LoadLibrary( TEXT(“richink.dll” ) );
if (!hLib)
return ERROR;
The next step is to create a RichInk window:
HWND hwndInk = NULL;
hwndInk = CreateWindow(“richink”, ES_READONLY, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL, 0, NULL, NULL);
if (!hwndInk)
return ERROR;
With the RichInk window we can send EM_STREAMIN and EM_STREAMOUT messages in a manner very similar to the RichEdit desktop control:
EDITSTREAM es;
COOKIE cookie;
// Stream in the Ink Data from a memory block.
memset(&cookie, 0, sizeof(cookie));
cookie.dwError = 0;
cookie.pbStart = (LPBYTE)pb; // Pointer to memory block
cookie.pbCur = cookie.pbStart;
cookie.bCount = cb; // Size of ink data
es.dwCookie = (DWORD)&cookie;
es.dwError = 0;
es.pfnCallback = BufferReadCallback;
lResult = SendMessage (hwndInk, EM_STREAMIN, (WPARAM)SF_PWI, (LPARAM)&es);
// Next call stream out to get the size of the Unicode text.
cookie.dwError = 0;
cookie.pbStart = 0;
cookie.pbCur = 0;
cookie.bCount = 0;
es.dwCookie = (DWORD)&cookie;
es.dwError = 0;
es.pfnCallback = BufferWriteCallback;
lResult = SendMessage (hwndInk, EM_STREAMOUT, (WPARAM)SF_UTEXT, (LPARAM)&es);
// Allocate memory and stream out the Unicode text to it.
sz = (LPWSTR)LocalAlloc(LPTR, (cbSz = Cookie.pbCur - Cookie.pbStart) + 2);
// Check for alloc err.
cookie.dwError = 0;
cookie.pbStart = (LPBYTE)sz;
cookie.pbCur = cookie.pbStart;
cookie.bCount = cbSz;
es.dwCookie = (DWORD)&cookie;
es.dwError = 0;
es.pfnCallback = BufferWriteCallback;
lResult = SendMessage (hwndInk, EM_STREAMOUT, (WPARAM)SF_UTEXT, (LPARAM)&es);
Now finally some sample Callback functions:
static DWORD CALLBACK
BufferReadCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
PCOOKIE pCookie = (PCOOKIE)dwCookie;
LONG bytesLeft;
LONG bytesRead;
// Calc the bytes read.
bytesRead = pCookie->pbCur - pCookie->pbStart;
// Calc bytes left to read.
if (bytesRead < pCookie->bCount)
{
// Calc the bytes left to read.
bytesLeft = pCookie->bCount - bytesRead;
}
else
{
bytesLeft = 0;
}
// Don’t read past the end of buffer.
if (cb > bytesLeft) cb = bytesLeft;
// Set bytes read.
*pcb = cb;
// Copy any bytes.
if (cb)
{
memcpy(pbBuff, pCookie->pbCur, cb);
pCookie->pbCur += cb;
}
// Return no error.
return 0;
} // BufferReadCallback()
static DWORD CALLBACK
BufferWriteCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
PCOOKIE pCookie = (PCOOKIE)dwCookie;
PCOOKIE pCookie = (PCOOKIE)dwCookie;
// Do not overwrite the end of the buffer.
// If there is no output buffer, then we’re
// only here to determine the space required
// for the stream out.
If (pCookie->pbStart &&
(pCookie->pbCur + cb > pCookie->pbStart + pCookie->bCount))
{
// Writing all this data would overflow the buffer.
// So only write what will fit.
cb = pCookie->pbStart + pCookie->bCount - pCookie->pbCur;
}
*pcb = cb;
if (pCookie->pbStart)
memcpy(pCookie->pbCur, pbBuff, cb); pCookie->pbCur += cb;
return 0;
} // BufferWriteCallback()
The foregoing example streams out Unicode text, but could be used to stream out RTF or ANSI text by changing the Stream Format to SF_RTF or SF_TEXT. By changing the callback functions, data can be read from and/or written to a file.
Sample callback functions for reading/writing
Here are some sample callback functions for file reading and writing:
static DWORD CALLBACK
ReadCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
PCOOKIE pCookie = (PCOOKIE)dwCookie;
// Zero the bytes read.
*pcb = 0;
// If there is an error return it.
if (ReadFile(pCookie->hFile, pbBuff, cb, pcb, 0) == 0)
return (pCookie->dwError = GetLastError());
// Return no error.
return 0;
} // ReadCallback()
static DWORD CALLBACK
WriteCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
PCOOKIE pCookie = (PCOOKIE)dwCookie;
if (WriteFile(pCookie->hFile, pbBuff, cb, pcb, 0) == 0)
return (pCookie->dwError = GetLastError());
return 0;
} // WriteCallback()
Compatibility issues
The previous code describes the interface for desktop Windows CE Services 2.2. This interface is also the same for Handheld PC 2.11 and Palm-sized PC 1.2 devices. For previous versions of Windows CE Services or for HPC 2.0 or Palm-size PC 1.0 devices, the following change must be made:
Earlier versions passed a pointer to a variable containing the Stream Format rather than just the Stream Format constant directly. For example:
DWORDwpSF = SF_PWI;
SendMessage (hwndInk, EM_STREAMIN, (WPARAM)&wpSF, (LPARAM)&es);
Earlier versions may also require the use of the following constant when writing Unicode text files:
#define SF_UTEXT 0x0005