TextEdit.h
上传用户:starrett
上传日期:2020-11-17
资源大小:1474k
文件大小:16k
- //#include"resource.h"
- LRESULT CALLBACK TextEditProc(HWND, UINT, WPARAM, LPARAM);
- static DWORD dwCharSet = DEFAULT_CHARSET ;
- static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, xCaret, yCaret, iVertPos, iHorzPos, xFocu;
- static TCHAR * pBuffer = NULL ;
- static RowNode* PRow[ROWSMAX];
- HDC hdc ;
- int x=0,y=0,j=0, i=0, iPaintBeg, iPaintEnd ;
- //long x=0,y=0;
- POINT point;
- PAINTSTRUCT ps ;
- TEXTMETRIC tm ;
- SCROLLINFO si ;
- bool bDown;
- HWND TextEdit(HINSTANCE hInstance, HWND hwnd)
- {
- static TCHAR TextEditClass[] = TEXT("TextEdit") ;
- HWND hwndEdit ;
- WNDCLASS wndclass ;
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = TextEditProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION) ;
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
- wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
- wndclass.lpszMenuName = NULL ;
- wndclass.lpszClassName = TextEditClass ;
- RegisterClass(&wndclass) ;
- hwndEdit = CreateWindow (TextEditClass, NULL,
- WS_CHILDWINDOW | WS_VISIBLE | WS_BORDER,
- 0, 0, 0, 0,
- hwnd, NULL,
- NULL, NULL) ;
- return hwndEdit ;
- }
- LRESULT CALLBACK TextEditProc (HWND hwndEdit, UINT message, WPARAM wParam, LPARAM lParam)
- {
- /*static DWORD dwCharSet = DEFAULT_CHARSET ;
- static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, xCaret, yCaret, iVertPos, iHorzPos, xFocu;
- static TCHAR * pBuffer = NULL ;
- static RowNode* PRow[ROWSMAX];
- HDC hdc ;
- int x=0,y=0,j=0, i=0, iPaintBeg, iPaintEnd ;
- //long x=0,y=0;
- POINT point;
- PAINTSTRUCT ps ;
- TEXTMETRIC tm ;
- SCROLLINFO si ;
- bool bDown;*/
- PAINTSTRUCT ps ;
- TEXTMETRIC tm ;
- static DWORD dwCharSet = DEFAULT_CHARSET ;
- PTSTR text=NULL;
- SCROLLINFO si ;
- static POINT point;
- int row=0, //行增加
- column=0; //列增加
- switch (message)
- {
- case WM_INPUTLANGCHANGE:
- dwCharSet = wParam ;
- return 0 ;
- case WM_CREATE:
- InsertRow(PRow,ROWSMAX);
- hdc = GetDC (hwndEdit) ;
- SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
- dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;
-
- GetTextMetrics (hdc, &tm) ;
- cxChar = tm.tmAveCharWidth ;
- cyChar = tm.tmHeight ;
-
- DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
- ReleaseDC (hwndEdit, hdc) ;
- return 0 ;
-
- case WM_SIZE:
- cxClient = LOWORD (lParam) ;
- cyClient = HIWORD (lParam) ;
- cxBuffer = max (1, cxClient / cxChar) ;
- cyBuffer = max (1, cyClient / cyChar) ;
- SetScroll(hwndEdit, cyBuffer, SB_VERT) ;
- InvalidateRect (hwndEdit, NULL, TRUE) ;
- return 0 ;
-
- /*case WM_LBUTTONDOWN:
- point.x = LOWORD(lParam);
- point.y = HIWORD(lParam);
- SetCaretPos(point.x, point.y);
- return 0;*/
- case WM_SETFOCUS:
- CreateCaret (hwndEdit, NULL, 1, cyChar) ;
- SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
- ShowCaret (hwndEdit) ;
- return 0 ;
-
- case WM_KILLFOCUS:
- HideCaret (hwndEdit) ;
- DestroyCaret () ;
- return 0 ;
- case WM_KEYDOWN:
- for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
- {
- switch (wParam)
- {
- case VK_HOME:
- xFocu=xCaret = 0 ;
- break ;
-
- case VK_END:
- xCaret = PRow[yCaret]->number ;
- xFocu = PRow[yCaret]->width ;
- break ;
-
- case VK_PRIOR:
- yCaret = 0 ;
- break ;
-
- case VK_NEXT:
- yCaret = RowSum - 1 ;
- break ;
-
- case VK_LEFT:
- if(xCaret == 0 && yCaret != 0)
- {
- yCaret-- ;
- xCaret=PRow[yCaret]->number ;
- xFocu=PRow[yCaret]->width ;
- }
- else
- {
- if((xCaret - 1)>=0)
- {
- xCaret-- ;
- if((PRow[yCaret]->date[xCaret] >> 8)>0)
- xFocu -= cxChar*2 ;
- else
- xFocu -= cxChar ;
- }
- }
- break ;
-
- case VK_RIGHT:
- if(xCaret == PRow[yCaret]->number && yCaret != RowSum-1)
- {
- yCaret++;
- xFocu=xCaret=0;
- }
- else
- {
- if((xCaret + 1)<=PRow[yCaret]->number)
- {
- if((PRow[yCaret]->date[xCaret] >> 8)>0)
- xFocu += cxChar*2 ;
- else
- xFocu += cxChar ;
- xCaret++ ;
- }
- }
- break ;
-
- case VK_UP:
- yCaret = max (yCaret - 1, 0) ;
- if(xFocu >= PRow[yCaret]->width)
- {
- xCaret=PRow[yCaret]->number ;
- xFocu=PRow[yCaret]->width ;
- }
- else
- {
- while(x<xFocu)
- {
- x+=CharWidth(PRow[yCaret]->date[i])*cxChar ;
- i++ ;
- }
- xCaret=i ;
- xFocu=x ;
- }
- break ;
-
- case VK_DOWN:
- yCaret = min (yCaret + 1, RowSum - 1) ;
- if(xFocu >= PRow[yCaret]->width)
- {
- xCaret=PRow[yCaret]->number ;
- xFocu=PRow[yCaret]->width ;
- }
- else
- {
- while(x<xFocu)
- {
- x+=CharWidth(PRow[yCaret]->date[i])*cxChar ;
- i++ ;
- }
- xCaret=i ;
- xFocu=x ;
- }
- break ;
-
- case VK_DELETE:
- if(xCaret<PRow[yCaret]->number)
- {
- DeleteChar(PRow,yCaret,xCaret);
- HideCaret (hwndEdit) ;
- hdc = GetDC (hwndEdit) ;
- SetFont(hdc, dwCharSet) ;
- ClsChar(hdc , xFocu, (yCaret-iVertPos) * cyChar, min(cxBuffer*cxChar,PRow[yCaret]->width)+1, (yCaret-iVertPos+1) * cyChar) ;
- TextOut (hdc, xFocu, (yCaret-iVertPos) * cyChar,PRow[yCaret]->date+xCaret, PRow[yCaret]->number-xCaret) ;
- DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
- if((PRow[yCaret]->date[xCaret] >> 8)>0)
- PRow[yCaret]->width -= cxChar*2 ;
- else
- PRow[yCaret]->width -= cxChar ;
- ReleaseDC (hwndEdit, hdc) ;
- ShowCaret (hwndEdit) ;
- }
- return 0;
- }
- }
- SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
- return 0 ;
- case WM_CHAR:
- for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
- {
- switch (wParam)
- {
- case 'b': // backspace
- if (xCaret > 0)
- {
- xCaret-- ;
- if((PRow[yCaret]->date[xCaret] >> 8)>0)
- xFocu -= cxChar*2 ;
- else
- xFocu -= cxChar ;
- SendMessage (hwndEdit, WM_KEYDOWN, VK_DELETE, 1) ;
- }
- else if(xCaret == 0 && yCaret != 0)
- {
- x=PRow[yCaret-1]->number ;
- xFocu=PRow[yCaret-1]->width ;
- CopyRow(PRow,yCaret,xCaret,yCaret-1,PRow[yCaret-1]->number);
- PRow[yCaret-1]->width=xFocu + PRow[yCaret]->width;
- DeleteRow(PRow,yCaret);
- xCaret=x;yCaret--;
- HideCaret (hwndEdit) ;
- hdc = GetDC (hwndEdit) ;
- SetFont(hdc, dwCharSet) ;
- ClsChar(hdc , 0 , (yCaret-iVertPos) * cyChar , cxBuffer*cxChar, (RowSum-iVertPos+1) * cyChar);
- for (y = yCaret ; y < RowSum ; y++)
- TextOut (hdc, 0, (y-iVertPos) * cyChar,PRow[y]->date, PRow[y]->number) ;
- DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
- ReleaseDC (hwndEdit, hdc) ;
- ShowCaret (hwndEdit) ;
- SendMessage(hwndEdit, WM_VSCROLL, SB_LINEUP, 0);
- SetScroll(hwndEdit, cyBuffer, SB_VERT) ;
- }
- break ;
-
- case 't': // tab
- break ;
-
- case 'n': // line feed
- break ;
-
- case 'r': // carriage return
- xFocu = 0 ;
- InsertRow(PRow,yCaret+1);
- HideCaret (hwndEdit) ;
- hdc = GetDC (hwndEdit) ;
- SetFont(hdc, dwCharSet) ;
- if(xCaret < PRow[yCaret]->number) CopyRow(PRow,yCaret,xCaret,yCaret+1,0);
-
- ClsChar(hdc , 0 , (yCaret-iVertPos) * cyChar , cxBuffer*cxChar, (RowSum-iVertPos) * cyChar);
- for (y = yCaret ; y < RowSum ; y++)
- TextOut (hdc, 0, (y-iVertPos) * cyChar,PRow[y]->date, PRow[y]->number) ;
- DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
- yCaret++;
- ReleaseDC (hwndEdit, hdc) ;
- ShowCaret (hwndEdit) ;
- xCaret=0;
- if(RowSum > cyBuffer)
- SetScroll(hwndEdit, cyBuffer, SB_VERT) ;
- break ;
-
- case 'x1B': // escape
- hdc = GetDC (hwndEdit) ;
- ClsChar(hdc,0,0,cxBuffer*cxChar,(RowSum-iVertPos) * cyChar);
- // HideCaret (hwndEdit) ;
- xCaret=yCaret=0;
- xFocu=0;
- // ShowCaret (hwndEdit) ;
- SetCaretPos(xCaret,yCaret);
- for(j;j<RowSum;j++)
- for(i;i<cxBuffer;i++)
- PRow[j]->date[i]=' ';
- // for(j;j<cyBuffer;j++)
- // TextOut(hdc,0,0,PRow[j]->date,PRow[j]->number);
- ReleaseDC (hwndEdit, hdc) ;
- break ;
-
-
- default:
- InputChar(PRow,yCaret,xCaret,(TCHAR)wParam);
- HideCaret (hwndEdit) ;
- hdc = GetDC (hwndEdit) ;
- SetFont(hdc, dwCharSet) ;
- TextOut (hdc, xFocu, (yCaret-iVertPos) * cyChar,PRow[yCaret]->date+xCaret, PRow[yCaret]->number-xCaret) ;
- DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
- ReleaseDC (hwndEdit, hdc) ;
- ShowCaret (hwndEdit) ;
- xCaret++ ;
- if((wParam >> 8)>0)
- {
- PRow[yCaret]->width += cxChar*2 ;
- xFocu += cxChar*2 ;
- }
- else
- {
- PRow[yCaret]->width += cxChar ;
- xFocu += cxChar ;
- }
- break;
- }
- }
- if(yCaret >= cyBuffer+iVertPos)
- {
- x=yCaret-cyBuffer+1 ;
- SendMessage(hwndEdit, WM_VSCROLL, SB_THUMBTRACK, x+1);
- return 0;
- }
- else if(yCaret < iVertPos)
- {
- SendMessage(hwndEdit, WM_VSCROLL, SB_THUMBTRACK, yCaret+1);
- return 0;
- }
- SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
- return 0 ;
-
- case WM_LBUTTONDOWN:
- /*SetCursor(LoadCursor(NULL, IDC_IBEAM));
- bDown=true;//左键被按下
- x=LOWORD(lParam);
- y=HIWORD(lParam);
- /*
- if(y%cyChar==0)
- yCaret=y;
- else yCaret=y/cyChar+cyChar;
- if(x>=PRow[yCaret]->width)
- xCaret=PRow[yCaret]->width;
- else if(x%cxChar==0)
- xCaret=x;
- else xCaret=x/cxChar;
-
- SetCaretPos(xCaret,yCaret);
- */
- point.x = LOWORD(lParam);
- point.y = HIWORD(lParam);
- SetCaretPos(point.x, point.y);
- return 0;
- break;
- case WM_LBUTTONUP:
- SetCursor(LoadCursor(NULL, IDC_IBEAM));
- bDown=false;
- x=LOWORD(lParam);
- y=HIWORD(lParam);
- /*
- if((x%cxChar==0))
- xCaret=x;
- else
- xCaret=x/cxChar;
- if(y%cyChar==0)
- yCaret=y;
- else yCaret=y/cyChar;
- SetCaretPos(xCaret,yCaret);
- */
- break;
- case WM_MOUSEMOVE:
- SetCursor(LoadCursor(NULL, IDC_IBEAM));
- x=LOWORD(lParam);
- y=HIWORD(lParam);
- break;
- case WM_VSCROLL:
- si.cbSize = sizeof (si) ;
- si.fMask = SIF_ALL ;
- GetScrollInfo (hwndEdit, SB_VERT, &si) ;
- iVertPos = si.nPos ;
- switch (LOWORD (wParam))
- {
- case SB_TOP:
- si.nPos = si.nMin ;
- break ;
-
- case SB_BOTTOM:
- si.nPos = si.nMax ;
- break ;
-
- case SB_LINEUP:
- si.nPos -= 1 ;
- break ;
-
- case SB_LINEDOWN:
- si.nPos += 1 ;
- break ;
-
- case SB_PAGEUP:
- si.nPos -= si.nPage ;
- break ;
-
- case SB_PAGEDOWN:
- si.nPos += si.nPage ;
- break ;
-
- case SB_THUMBTRACK:
- if(lParam)
- si.nPos = lParam-1 ;
- else
- si.nPos = si.nTrackPos ;
- break ;
-
- default:
- break ;
- }
- si.fMask = SIF_POS ;
- SetScrollInfo (hwndEdit, SB_VERT, &si, TRUE) ;
- GetScrollInfo (hwndEdit, SB_VERT, &si) ;
- if (si.nPos != iVertPos)
- {
- HideCaret (hwndEdit) ;
- ScrollWindow (hwndEdit, 0, cyChar * (iVertPos - si.nPos), NULL, NULL) ;
- ShowCaret (hwndEdit) ;
- SetCaretPos (xCaret * cxChar, (yCaret-si.nPos) * cyChar) ;
- iVertPos=si.nPos;
- UpdateWindow (hwndEdit) ;
- }
- return 0 ;
- case WM_PAINT:
- hdc = BeginPaint (hwndEdit, &ps) ;
- SetFont(hdc, dwCharSet) ;
- SetWindowOrgEx(hdc,cxChar * iHorzPos,cyChar * iVertPos,NULL);
- iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
- iPaintEnd = min (RowSum - 1, iVertPos + ps.rcPaint.bottom / cyChar) ;
- for (y = iPaintBeg ; y <= iPaintEnd ; y++)
- TextOut (hdc, 0, y * cyChar,PRow[y]->date, PRow[y]->number) ;
- DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
- EndPaint (hwndEdit, &ps) ;
- return 0 ;
- case WM_DESTROY:
- PostQuitMessage (0) ;
- return 0 ;
- }
- return DefWindowProc (hwndEdit, message, wParam, lParam) ;
- }