TextEdit.h
上传用户:starrett
上传日期:2020-11-17
资源大小:1474k
文件大小:16k
源码类别:

RichEdit

开发平台:

Visual C++

  1. //#include"resource.h"
  2. LRESULT CALLBACK TextEditProc(HWND, UINT, WPARAM, LPARAM);
  3.      static DWORD   dwCharSet = DEFAULT_CHARSET ;
  4.      static int     cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, xCaret, yCaret, iVertPos, iHorzPos, xFocu;
  5.      static TCHAR * pBuffer = NULL ;
  6.      static RowNode* PRow[ROWSMAX];
  7.      HDC            hdc ;
  8.      int            x=0,y=0,j=0, i=0, iPaintBeg, iPaintEnd ;
  9.  //long           x=0,y=0;
  10.  POINT          point;
  11.      PAINTSTRUCT    ps ;
  12.      TEXTMETRIC     tm ;
  13.      SCROLLINFO     si ;
  14.      bool           bDown;
  15. HWND TextEdit(HINSTANCE hInstance, HWND hwnd)
  16. {
  17.     static TCHAR TextEditClass[] = TEXT("TextEdit") ;
  18.     HWND hwndEdit ;
  19.     WNDCLASS wndclass ;
  20.     wndclass.style           = CS_HREDRAW | CS_VREDRAW ;
  21.     wndclass.lpfnWndProc     = TextEditProc ;
  22.     wndclass.cbClsExtra      = 0 ;
  23.     wndclass.cbWndExtra      = 0 ;
  24.     wndclass.hInstance       = hInstance ;
  25.     wndclass.hIcon           = LoadIcon(NULL, IDI_APPLICATION) ;
  26.     wndclass.hCursor         = LoadCursor(NULL, IDC_ARROW) ;
  27.     wndclass.hbrBackground   = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
  28.     wndclass.lpszMenuName    = NULL ;
  29.     wndclass.lpszClassName   = TextEditClass ;
  30.     RegisterClass(&wndclass) ;
  31.     hwndEdit = CreateWindow (TextEditClass, NULL,
  32.                               WS_CHILDWINDOW | WS_VISIBLE | WS_BORDER,
  33.                               0, 0, 0, 0,
  34.                               hwnd, NULL,
  35.                               NULL, NULL) ;
  36.     return hwndEdit ;
  37. }
  38. LRESULT CALLBACK TextEditProc (HWND hwndEdit, UINT message, WPARAM wParam, LPARAM lParam)
  39. {
  40.      /*static DWORD   dwCharSet = DEFAULT_CHARSET ;
  41.      static int     cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer, xCaret, yCaret, iVertPos, iHorzPos, xFocu;
  42.      static TCHAR * pBuffer = NULL ;
  43.      static RowNode* PRow[ROWSMAX];
  44.      HDC            hdc ;
  45.      int            x=0,y=0,j=0, i=0, iPaintBeg, iPaintEnd ;
  46.  //long           x=0,y=0;
  47.  POINT          point;
  48.      PAINTSTRUCT    ps ;
  49.      TEXTMETRIC     tm ;
  50.      SCROLLINFO     si ;
  51.      bool           bDown;*/
  52.  PAINTSTRUCT    ps ;
  53.      TEXTMETRIC     tm ;
  54.      static DWORD   dwCharSet = DEFAULT_CHARSET ;
  55.      PTSTR          text=NULL;
  56.      SCROLLINFO     si ;
  57.  static POINT   point;
  58.      int            row=0,      //行增加
  59.         column=0;  //列增加
  60.      switch (message)
  61.      {
  62.      case WM_INPUTLANGCHANGE:
  63.           dwCharSet = wParam ;
  64.           return 0 ;
  65.      case WM_CREATE:
  66.           InsertRow(PRow,ROWSMAX);
  67.           hdc = GetDC (hwndEdit) ;
  68.           SelectObject (hdc, CreateFont (0, 0, 0, 0, 0, 0, 0, 0,
  69.                                    dwCharSet, 0, 0, 0, FIXED_PITCH, NULL)) ;
  70.           
  71.           GetTextMetrics (hdc, &tm) ;
  72.           cxChar = tm.tmAveCharWidth ;
  73.           cyChar = tm.tmHeight ;
  74.           
  75.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  76.           ReleaseDC (hwndEdit, hdc) ;
  77.           return 0 ;      
  78.           
  79.      case WM_SIZE:
  80.           cxClient = LOWORD (lParam) ;
  81.           cyClient = HIWORD (lParam) ;
  82.           cxBuffer = max (1, cxClient / cxChar) ;
  83.           cyBuffer = max (1, cyClient / cyChar) ;
  84.           SetScroll(hwndEdit, cyBuffer, SB_VERT) ;
  85.           InvalidateRect (hwndEdit, NULL, TRUE) ;
  86.           return 0 ;
  87.                     
  88.  /*case WM_LBUTTONDOWN:
  89.   point.x = LOWORD(lParam);
  90.           point.y = HIWORD(lParam);
  91.           SetCaretPos(point.x, point.y);
  92.   return 0;*/
  93.      case WM_SETFOCUS:
  94.           CreateCaret (hwndEdit, NULL, 1, cyChar) ;
  95.           SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  96.           ShowCaret (hwndEdit) ;
  97.           return 0 ;
  98.           
  99.      case WM_KILLFOCUS:
  100.           HideCaret (hwndEdit) ;
  101.           DestroyCaret () ;
  102.           return 0 ;
  103.      case WM_KEYDOWN:
  104.           for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
  105.           {
  106.               switch (wParam)
  107.               {
  108.               case VK_HOME:
  109.                    xFocu=xCaret = 0 ;
  110.                    break ;
  111.                
  112.               case VK_END:
  113.                    xCaret = PRow[yCaret]->number ;
  114.    xFocu = PRow[yCaret]->width ;
  115.                    break ;
  116.                
  117.               case VK_PRIOR:
  118.                    yCaret = 0 ;
  119.                    break ;
  120.                
  121.               case VK_NEXT:
  122.                    yCaret = RowSum - 1 ;
  123.                    break ;
  124.                
  125.               case VK_LEFT:
  126.                    if(xCaret == 0 && yCaret != 0)
  127.                    {   
  128.                        yCaret-- ;
  129.                        xCaret=PRow[yCaret]->number ;
  130.                        xFocu=PRow[yCaret]->width ;
  131.                    }
  132.                    else
  133.                    {
  134.                        if((xCaret - 1)>=0)
  135.                        {
  136.    xCaret-- ;
  137.                            if((PRow[yCaret]->date[xCaret] >> 8)>0)
  138.                                 xFocu -= cxChar*2 ;
  139.                            else
  140.                                 xFocu -= cxChar ;
  141.                        }
  142.                    }
  143.                    break ;
  144.                
  145.               case VK_RIGHT:
  146.                    if(xCaret == PRow[yCaret]->number && yCaret != RowSum-1)
  147.                    {
  148.                        yCaret++;
  149.                        xFocu=xCaret=0;
  150.                    }
  151.                    else
  152.                    {
  153.                        if((xCaret + 1)<=PRow[yCaret]->number)
  154.                        {
  155.                            if((PRow[yCaret]->date[xCaret] >> 8)>0)
  156.                               xFocu += cxChar*2 ;
  157.                            else
  158.                               xFocu += cxChar ;
  159.    xCaret++ ;
  160.                        }
  161.                    }
  162.                    break ;
  163.                
  164.               case VK_UP:
  165.                    yCaret = max (yCaret - 1, 0) ;
  166.                    if(xFocu >= PRow[yCaret]->width)
  167.    {
  168. xCaret=PRow[yCaret]->number ;
  169. xFocu=PRow[yCaret]->width ;
  170.    }
  171.    else
  172.    {
  173.    while(x<xFocu)
  174.    {
  175. x+=CharWidth(PRow[yCaret]->date[i])*cxChar ;
  176. i++ ;
  177.    }
  178.    xCaret=i ;
  179.    xFocu=x ;
  180.    }
  181.                    break ;
  182.                
  183.               case VK_DOWN:
  184.                    yCaret = min (yCaret + 1, RowSum - 1) ;
  185.                    if(xFocu >= PRow[yCaret]->width)
  186.    {
  187. xCaret=PRow[yCaret]->number ;
  188. xFocu=PRow[yCaret]->width ;
  189.    }
  190.    else
  191.    {
  192.    while(x<xFocu)
  193.    {
  194. x+=CharWidth(PRow[yCaret]->date[i])*cxChar ;
  195. i++ ;
  196.    }
  197.    xCaret=i ;
  198.    xFocu=x ;
  199.    }
  200.                    break ;
  201.                
  202.               case VK_DELETE:
  203.                   if(xCaret<PRow[yCaret]->number)
  204.                   {
  205.                     DeleteChar(PRow,yCaret,xCaret);
  206.                     HideCaret (hwndEdit) ;
  207.                     hdc = GetDC (hwndEdit) ;
  208.                     SetFont(hdc, dwCharSet) ;
  209.                     ClsChar(hdc , xFocu, (yCaret-iVertPos) * cyChar, min(cxBuffer*cxChar,PRow[yCaret]->width)+1, (yCaret-iVertPos+1) * cyChar) ;
  210.                     TextOut (hdc, xFocu, (yCaret-iVertPos) * cyChar,PRow[yCaret]->date+xCaret, PRow[yCaret]->number-xCaret) ;
  211.                     DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  212.                     if((PRow[yCaret]->date[xCaret] >> 8)>0)
  213.                         PRow[yCaret]->width -= cxChar*2 ;
  214.                     else
  215.                         PRow[yCaret]->width -= cxChar ;                   
  216.                     ReleaseDC (hwndEdit, hdc) ;
  217.                     ShowCaret (hwndEdit) ;
  218.                   }
  219.                    return 0;
  220.               }
  221.           }
  222.           SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  223.           return 0 ;
  224.      case WM_CHAR:
  225.           for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
  226.           {
  227.                switch (wParam)
  228.                {
  229.                case 'b':                    // backspace
  230.                     if (xCaret > 0)
  231.                     {
  232.                          xCaret-- ;
  233.                          if((PRow[yCaret]->date[xCaret] >> 8)>0)
  234.                             xFocu -= cxChar*2 ;
  235.                          else
  236.                             xFocu -= cxChar ;
  237.                          SendMessage (hwndEdit, WM_KEYDOWN, VK_DELETE, 1) ;
  238.                     }
  239.                     else if(xCaret == 0 && yCaret != 0)
  240.                     {
  241.                         x=PRow[yCaret-1]->number ;
  242. xFocu=PRow[yCaret-1]->width ;
  243.                         CopyRow(PRow,yCaret,xCaret,yCaret-1,PRow[yCaret-1]->number);
  244. PRow[yCaret-1]->width=xFocu + PRow[yCaret]->width;
  245.                         DeleteRow(PRow,yCaret);
  246.                         xCaret=x;yCaret--;
  247.                         HideCaret (hwndEdit) ;
  248.                         hdc = GetDC (hwndEdit) ;
  249.                         SetFont(hdc, dwCharSet) ;                        
  250.                         ClsChar(hdc , 0 , (yCaret-iVertPos) * cyChar , cxBuffer*cxChar, (RowSum-iVertPos+1) * cyChar);
  251.                         for (y = yCaret ; y < RowSum ; y++)
  252.                             TextOut (hdc, 0, (y-iVertPos) * cyChar,PRow[y]->date, PRow[y]->number) ;
  253.                         DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  254.                         ReleaseDC (hwndEdit, hdc) ;
  255.                         ShowCaret (hwndEdit) ;
  256.                         SendMessage(hwndEdit, WM_VSCROLL, SB_LINEUP, 0);
  257.                         SetScroll(hwndEdit, cyBuffer, SB_VERT) ;
  258.                     }
  259.                     break ;
  260.                     
  261.                case 't':                    // tab
  262.                     break ;
  263.                     
  264.                case 'n':                    // line feed
  265.                     break ;
  266.                     
  267.                case 'r':                    // carriage return
  268.     xFocu = 0 ;
  269.                     InsertRow(PRow,yCaret+1);
  270.                     HideCaret (hwndEdit) ;
  271.                     hdc = GetDC (hwndEdit) ;
  272.                     SetFont(hdc, dwCharSet) ;
  273.                     if(xCaret < PRow[yCaret]->number) CopyRow(PRow,yCaret,xCaret,yCaret+1,0);
  274.                     
  275.                     ClsChar(hdc , 0 , (yCaret-iVertPos) * cyChar , cxBuffer*cxChar, (RowSum-iVertPos) * cyChar);                
  276.                     for (y = yCaret ; y < RowSum ; y++)
  277.                         TextOut (hdc, 0, (y-iVertPos) * cyChar,PRow[y]->date, PRow[y]->number) ;
  278.                     DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  279.                     yCaret++;
  280.                     ReleaseDC (hwndEdit, hdc) ;
  281.                     ShowCaret (hwndEdit) ;
  282.                     xCaret=0;
  283.                     if(RowSum > cyBuffer)
  284.                         SetScroll(hwndEdit, cyBuffer, SB_VERT) ;
  285.                     break ;
  286.                     
  287.                case 'x1B':                  // escape
  288.                    hdc = GetDC (hwndEdit) ;
  289.    ClsChar(hdc,0,0,cxBuffer*cxChar,(RowSum-iVertPos) * cyChar);   
  290. //   HideCaret (hwndEdit) ;
  291.    xCaret=yCaret=0;
  292.    xFocu=0;
  293. //   ShowCaret (hwndEdit) ;
  294.    SetCaretPos(xCaret,yCaret);
  295.     for(j;j<RowSum;j++)
  296.        for(i;i<cxBuffer;i++)
  297.    PRow[j]->date[i]=' ';
  298.  //  for(j;j<cyBuffer;j++)
  299. //   TextOut(hdc,0,0,PRow[j]->date,PRow[j]->number);
  300.    ReleaseDC (hwndEdit, hdc) ;
  301.                    break ;
  302.                          
  303.                          
  304.                default:
  305.                     InputChar(PRow,yCaret,xCaret,(TCHAR)wParam);
  306.                     HideCaret (hwndEdit) ;
  307.                     hdc = GetDC (hwndEdit) ;
  308.                     SetFont(hdc, dwCharSet) ;
  309.                     TextOut (hdc, xFocu, (yCaret-iVertPos) * cyChar,PRow[yCaret]->date+xCaret, PRow[yCaret]->number-xCaret) ;
  310.                     DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  311.                     ReleaseDC (hwndEdit, hdc) ;
  312.                     ShowCaret (hwndEdit) ;
  313.                     xCaret++  ;
  314.                     if((wParam >> 8)>0)
  315.                     {
  316.                         PRow[yCaret]->width += cxChar*2 ;
  317.                         xFocu += cxChar*2 ;
  318.                     }
  319.                     else
  320.                     {
  321.                         PRow[yCaret]->width += cxChar ;
  322.                         xFocu += cxChar ;
  323.                     }
  324.                     break;
  325.                }
  326.           }
  327.           if(yCaret >= cyBuffer+iVertPos)
  328.           {
  329.               x=yCaret-cyBuffer+1 ;
  330.               SendMessage(hwndEdit, WM_VSCROLL, SB_THUMBTRACK, x+1);
  331.               return 0;
  332.           }
  333.           else if(yCaret < iVertPos)
  334.           {
  335.               SendMessage(hwndEdit, WM_VSCROLL, SB_THUMBTRACK, yCaret+1);
  336.               return 0;
  337.           }
  338.           SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  339.           return 0 ;
  340.      
  341.      case WM_LBUTTONDOWN:
  342.  /*SetCursor(LoadCursor(NULL, IDC_IBEAM));
  343.  bDown=true;//左键被按下
  344.              x=LOWORD(lParam);
  345.      y=HIWORD(lParam);
  346.  /*
  347.  if(y%cyChar==0)
  348.  yCaret=y;
  349.     else yCaret=y/cyChar+cyChar;
  350. if(x>=PRow[yCaret]->width)
  351. xCaret=PRow[yCaret]->width;
  352. else if(x%cxChar==0)
  353. xCaret=x;
  354.     else xCaret=x/cxChar;
  355.     
  356.     SetCaretPos(xCaret,yCaret);
  357. */
  358.   point.x = LOWORD(lParam);
  359.           point.y = HIWORD(lParam);
  360.           SetCaretPos(point.x, point.y);
  361.   return 0;
  362.   break;
  363.  case WM_LBUTTONUP:
  364.  SetCursor(LoadCursor(NULL, IDC_IBEAM));
  365.          bDown=false;
  366.   x=LOWORD(lParam);
  367.   y=HIWORD(lParam);
  368.   /*
  369.  if((x%cxChar==0))
  370. xCaret=x;
  371.  else 
  372. xCaret=x/cxChar;
  373. if(y%cyChar==0)
  374.  yCaret=y;
  375. else yCaret=y/cyChar;
  376.   SetCaretPos(xCaret,yCaret);
  377.   */
  378.  break; 
  379.  case WM_MOUSEMOVE:
  380.         SetCursor(LoadCursor(NULL, IDC_IBEAM));
  381.   x=LOWORD(lParam);
  382.   y=HIWORD(lParam);
  383.  break;
  384.      case WM_VSCROLL:
  385.           si.cbSize = sizeof (si) ;
  386.           si.fMask  = SIF_ALL ;
  387.           GetScrollInfo (hwndEdit, SB_VERT, &si) ;
  388.           iVertPos = si.nPos ;
  389.           switch (LOWORD (wParam))
  390.           {
  391.           case SB_TOP:
  392.                si.nPos = si.nMin ;
  393.                break ;
  394.                
  395.           case SB_BOTTOM:
  396.                si.nPos = si.nMax ;
  397.                break ;
  398.                
  399.           case SB_LINEUP:
  400.                si.nPos -= 1 ;
  401.                break ;
  402.                
  403.           case SB_LINEDOWN:
  404.                si.nPos += 1 ;
  405.                break ;
  406.                
  407.           case SB_PAGEUP:
  408.                si.nPos -= si.nPage ;
  409.                break ;
  410.                
  411.           case SB_PAGEDOWN:
  412.                si.nPos += si.nPage ;
  413.                break ;
  414.                
  415.           case SB_THUMBTRACK:
  416.                if(lParam)
  417.                    si.nPos = lParam-1 ;
  418.                else
  419.                    si.nPos = si.nTrackPos ;
  420.                break ;
  421.     
  422.           default:
  423.                break ;         
  424.           }
  425.           si.fMask = SIF_POS ;
  426.           SetScrollInfo (hwndEdit, SB_VERT, &si, TRUE) ;
  427.           GetScrollInfo (hwndEdit, SB_VERT, &si) ;
  428.           if (si.nPos != iVertPos)
  429.           {                    
  430.                HideCaret (hwndEdit) ;           
  431.                ScrollWindow (hwndEdit, 0, cyChar * (iVertPos - si.nPos), NULL, NULL) ;
  432.                ShowCaret (hwndEdit) ;
  433.                SetCaretPos (xCaret * cxChar, (yCaret-si.nPos) * cyChar) ;
  434.                iVertPos=si.nPos;
  435.                UpdateWindow (hwndEdit) ;
  436.           }
  437.           return 0 ;       
  438.      case WM_PAINT:
  439.           hdc = BeginPaint (hwndEdit, &ps) ;
  440.           SetFont(hdc, dwCharSet) ;
  441.           SetWindowOrgEx(hdc,cxChar * iHorzPos,cyChar * iVertPos,NULL);
  442.           iPaintBeg = max (0, iVertPos + ps.rcPaint.top / cyChar) ;
  443.           iPaintEnd = min (RowSum - 1, iVertPos + ps.rcPaint.bottom / cyChar) ;
  444.           for (y = iPaintBeg ; y <= iPaintEnd ; y++)
  445.                TextOut (hdc, 0, y * cyChar,PRow[y]->date, PRow[y]->number) ;
  446.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  447.           EndPaint (hwndEdit, &ps) ;
  448.           return 0 ;
  449.      case WM_DESTROY:
  450.           PostQuitMessage (0) ;
  451.           return 0 ;
  452.      }
  453.      return DefWindowProc (hwndEdit, message, wParam, lParam) ;
  454. }