// // Because DBCS characters are usually generated by IMEs (as two // PostMessages), if a lead byte comes in, the trail byte should // arrive very soon after. We wait here for the trail byte and // store them into the text buffer together.
if ( IsDBCSLeadByte( ch ) ) {
// // Wait an arbitrary amount of time for the trail byte to // arrive. If it doesn't, then discard the lead byte. // // This could happen if the IME screwed up. Or, more likely, // the user generated the lead byte through ALT-numpad. //
MSG msg; int i = 10;
while (!PeekMessage((LPMSG)&msg, hWnd, WM_CHAR, WM_CHAR, PM_REMOVE)) { if ( --i == 0 ) return; Yield(); }
// // Throw away. Already handled at WM_KEYDOWN time. //
break;
default: StoreChar( hWnd, ch ); break; } } } /************************************************************************ * * IsDBCSTrailByte - returns TRUE if the given byte is a DBCS trail byte * * The algorithm searchs backward in the string, to some * known character boundary, counting consecutive bytes * in the lead byte range. An odd number indicates the * current byte is part of a two byte character code. * * INPUT: PCHAR - pointer to a preceding known character boundary. * PCHAR - pointer to the character to test. * * OUTPUT:BOOL - indicating truth of p==trailbyte. * ************************************************************************/ BOOL IsDBCSTrailByte( char *base, char *p ) { int lbc = 0; // lead byte count
assert(base <= p);
while ( p > base ) { if ( !IsDBCSLeadByte(*(--p)) ) break; lbc++; }
return (lbc & 1); } /************************************************************************ * * StoreChar - Stores one SBCS character into text buffer and advances * cursor * ************************************************************************/ void StoreChar( HWND hWnd, BYTE ch ) { int i; HDC hdc;
// // If insert mode, move rest of line to the right by one //
if ( fInsertMode ) { for ( i = LASTCOL; i > xPos; i-- ) textbuf[yPos][i] = textbuf[yPos][i-1];
// // If the row ends on a lead byte, blank it out // To do this we must first traverse the string // starting from a known character boundry until // we reach the last column. If the last column // is a character boundry then the last character // is either a single byte or a lead byte //
for ( i = xPos+1; i < LASTCOL; ) { if ( IsDBCSLeadByte( textbuf[yPos][i] ) ) i++; i++; } if (i==LASTCOL) if ( IsDBCSLeadByte( textbuf[yPos][LASTCOL] ) ) textbuf[yPos][LASTCOL] = ' ';
} else { // overtype mode
if ( IsDBCSLeadByte( textbuf[yPos][xPos] ) )
// // Blank out trail byte //
textbuf[yPos][xPos+1] = ' ';
// // or shift line left on character and blank last column // // for ( i = xPos+1; i < LASTCOL; i++ ) // textbuf[yPos][i] = textbuf[yPos][i+1]; // textbuf[yPos][LASTCOL] = ' ';
}
// // Store input character at current caret position //
WM_CHAR
你用 WM_CHAR 或 WM_IMM_CHAR 可以得到输入法给你的字符。
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
TEXTMETRIC tm;
dc.GetTextMetrics(&tm); if(0x0d==nChar) //回车
{
...
}
else if(0x08==nChar) //退格
{
...
}
else
{
m_strLine += (TCHAR)nChar; //累加输入的字符
} CSize sz=dc.GetTextExtent(m_strLine);
CPoint pt;
pt.x=m_ptOrigin.x+sz.cx;
pt.y=m_ptOrigin.y;
SetCaretPos(pt); //光标重新定位 dc.TextOutW(m_ptOrigin.x, m_ptOrigin.y, m_strLine); //输出 CView::OnChar(nChar, nRepCnt, nFlags);
}
{
afxDump << nChar << "\n";
}
汉字输入时(2个字节)
在WM_CHAR 要:
查是不是LeadingByte 即第1字节是不是 》0x80
还要用 PeekMessage (WM_CHAR) 再取一个字符。
2个字节都>0x80
就是汉字
再显示帮助中有个sdk 的 editor 叫 halfime 你找找看。
直接用吧!
而且你想改变记事本的尺寸直接响应OnSize就行了
然后CEdit的容器你用CFormView
不要用CDialog了
帮助中搜索“halfime 和 Fullime”
halfime 是一个sdk的editor。
可以输入汉字
case WM_CHAR:
CharHandler( hWnd, wParam );
break; /************************************************************************
*
* CharHandler - WM_CHAR handler
*
************************************************************************/
void CharHandler( HWND hWnd, UINT wParam )
{
unsigned char ch = (unsigned char)wParam;
//
// Because DBCS characters are usually generated by IMEs (as two
// PostMessages), if a lead byte comes in, the trail byte should
// arrive very soon after. We wait here for the trail byte and
// store them into the text buffer together.
if ( IsDBCSLeadByte( ch ) ) {
//
// Wait an arbitrary amount of time for the trail byte to
// arrive. If it doesn't, then discard the lead byte.
//
// This could happen if the IME screwed up. Or, more likely,
// the user generated the lead byte through ALT-numpad.
//
MSG msg;
int i = 10;
while (!PeekMessage((LPMSG)&msg, hWnd, WM_CHAR, WM_CHAR, PM_REMOVE)) {
if ( --i == 0 )
return;
Yield();
}
StoreDBCSChar( hWnd, (WORD)(((unsigned)(msg.wParam)<<8) | (unsigned)ch ));
} else {
switch( ch )
{
case '\r':
case '\t':
case '\b':
//
// Throw away. Already handled at WM_KEYDOWN time.
//
break;
default:
StoreChar( hWnd, ch );
break;
}
}
} /************************************************************************
*
* IsDBCSTrailByte - returns TRUE if the given byte is a DBCS trail byte
*
* The algorithm searchs backward in the string, to some
* known character boundary, counting consecutive bytes
* in the lead byte range. An odd number indicates the
* current byte is part of a two byte character code.
*
* INPUT: PCHAR - pointer to a preceding known character boundary.
* PCHAR - pointer to the character to test.
*
* OUTPUT:BOOL - indicating truth of p==trailbyte.
*
************************************************************************/ BOOL IsDBCSTrailByte( char *base, char *p )
{
int lbc = 0; // lead byte count
assert(base <= p);
while ( p > base ) {
if ( !IsDBCSLeadByte(*(--p)) )
break;
lbc++;
}
return (lbc & 1);
} /************************************************************************
*
* StoreChar - Stores one SBCS character into text buffer and advances
* cursor
*
************************************************************************/ void StoreChar( HWND hWnd, BYTE ch )
{
int i;
HDC hdc;
//
// If insert mode, move rest of line to the right by one
//
if ( fInsertMode ) {
for ( i = LASTCOL; i > xPos; i-- )
textbuf[yPos][i] = textbuf[yPos][i-1];
//
// If the row ends on a lead byte, blank it out
// To do this we must first traverse the string
// starting from a known character boundry until
// we reach the last column. If the last column
// is a character boundry then the last character
// is either a single byte or a lead byte
//
for ( i = xPos+1; i < LASTCOL; ) {
if ( IsDBCSLeadByte( textbuf[yPos][i] ) )
i++;
i++;
}
if (i==LASTCOL)
if ( IsDBCSLeadByte( textbuf[yPos][LASTCOL] ) )
textbuf[yPos][LASTCOL] = ' ';
} else { // overtype mode
if ( IsDBCSLeadByte( textbuf[yPos][xPos] ) )
//
// Blank out trail byte
//
textbuf[yPos][xPos+1] = ' ';
//
// or shift line left on character and blank last column
//
// for ( i = xPos+1; i < LASTCOL; i++ )
// textbuf[yPos][i] = textbuf[yPos][i+1];
// textbuf[yPos][LASTCOL] = ' ';
}
//
// Store input character at current caret position
//
textbuf[yPos][xPos] = ch;
//
// Display input character.
//
hdc = GetDC( hWnd );
HideCaret( hWnd );
TextOut( hdc, xPos*cxMetrics, yPos*cyMetrics,
&(textbuf[yPos][xPos]), MAXCOL-xPos );
ShowCaret( hWnd );
ReleaseDC( hWnd, hdc );
SendMessage( hWnd, WM_KEYDOWN, VK_RIGHT, 1L );
}