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

RichEdit

开发平台:

Visual C++

  1. // 记事本.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include <windows.h>
  5. #include <stdlib.h>
  6. #include "resource.h"
  7. #include <commdlg.h>
  8. #include <windowsx.h>
  9. #include "func.h"
  10. #include <commctrl.h>
  11. #include "shlwapi.h"
  12. #define STRSIZE1 300
  13. #define STRSIZE2 30
  14. #define FILEPATHSIZE 2048
  15. #define FILENAMESIZE 280
  16. #define MAXPATTERNLEN 200
  17. char * strFileName;
  18. char * strFilePath;
  19. TCHAR strWindowTitle[STRSIZE2];
  20. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  21. void OpenFileProc(HWND);
  22. BOOL OpenFileDlg(HWND,char *,char *);
  23. void SaveFileProc(HWND,PTSTR,PTSTR);
  24. static TCHAR szAppName[] = TEXT ("记事本") ;
  25. TCHAR szDefaultText[] = TEXT ("welcome to use this") ;
  26. #define CF_TCHAR CF_UNICODETEXT
  27. static HWND hDlgModeless;
  28. PTSTR text=NULL; 
  29. PTSTR Text=NULL;
  30. PTSTR       GetText(HWND);
  31. HWND FindFindDlg     (HWND) ;
  32. HWND FindReplaceDlg  (HWND) ;
  33. BOOL FindFindText    (HWND, int *, LPFINDREPLACE) ;
  34. BOOL FindReplaceText (HWND, int *, LPFINDREPLACE) ;
  35. BOOL FindValidFind   (void) ;
  36. int APIENTRY WinMain(HINSTANCE hInstance,
  37.                      HINSTANCE hPrevInstance,
  38.                      LPSTR     lpCmdLine,
  39.                      int       nCmdShow)
  40. {
  41.   // TODO: Place code here.
  42.      HWND         hwnd ;
  43.      MSG          msg ;
  44.      WNDCLASS     wndclass ;
  45.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  46.      wndclass.lpfnWndProc   = WndProc ;
  47.      wndclass.cbClsExtra    = 0 ;
  48.      wndclass.cbWndExtra    = 0 ;
  49.      wndclass.hInstance     = hInstance ;
  50.      wndclass.hIcon         = LoadIcon (hInstance,"记事本") ;
  51.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  52.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  53.      wndclass.lpszMenuName  = "记事本" ;
  54.      wndclass.lpszClassName = szAppName ;
  55.      
  56.      if (!RegisterClass (&wndclass))
  57.           return 0 ;
  58.      
  59.      
  60.      hwnd = CreateWindow (szAppName, TEXT ("记事本"),
  61.                           WS_OVERLAPPEDWINDOW,
  62.                           CW_USEDEFAULT, CW_USEDEFAULT,
  63.                           CW_USEDEFAULT, CW_USEDEFAULT,
  64.                           NULL, NULL, hInstance, NULL) ;
  65.      
  66.      ShowWindow (hwnd, nCmdShow) ;
  67.      UpdateWindow (hwnd) ;
  68. // SendMessage(GetDlgItem(hwnd,IDC_EDIT1),WM_SETFONT,(WPARAM)hFont,TRUE);
  69.      
  70.      while (GetMessage (&msg, NULL, 0, 0))
  71.      {
  72.           TranslateMessage (&msg) ;
  73.           DispatchMessage (&msg) ;
  74.      }
  75.      return msg.wParam ;
  76. }
  77. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  78. {
  79.      static int cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,
  80.                     xCaret, yCaret , iVertPos, iHorzPos, xFocu;
  81.   //   static TCHAR * pBuffer = NULL ;
  82.  static TCHAR     szFileName[MAX_PATH], szTitleName[MAX_PATH] ;
  83.  static RowNode* PRow[ROWSMAX];
  84.      HDC            hdc ;
  85.      int            x=0, y=0, i=0 ;
  86.   PAINTSTRUCT    p;
  87.      TEXTMETRIC     tm ;
  88.  POINT        point ;
  89.      int         iSelect, iEnable, iPaintBeg, iPaintEnd ;
  90.      static DWORD   dwCharSet = DEFAULT_CHARSET ;
  91.   static CHOOSEFONT cf ;
  92.    static LOGFONT    lf ;
  93.     static int       iOffset ;
  94. int row=0,cols=0;
  95. static PTSTR pText ;//用来保存需要复制,剪切,删除的内容
  96.      BOOL         bEnable ;
  97.      HGLOBAL      hGlobal ;
  98.      PTSTR        pGlobal ;
  99.      switch (message)
  100.      {
  101.      case WM_INPUTLANGCHANGE:
  102.           dwCharSet = wParam ;
  103.   return 0;
  104.      case WM_CREATE:
  105.   InsertRow(PRow,ROWSMAX);
  106.           hdc = GetDC (hwnd) ;
  107.   SetFont(hdc, dwCharSet) ;
  108.           GetTextMetrics (hdc, &tm) ;
  109.           cxChar =tm.tmAveCharWidth ;
  110.           cyChar =tm.tmHeight+ tm.tmInternalLeading ;
  111.           
  112.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  113.           ReleaseDC (hwnd, hdc) ;
  114.             return 0 ;
  115.                             // fall through                
  116.      case WM_SIZE:
  117.                // obtain window size in pixels
  118.                cxClient = LOWORD (lParam) ;
  119.                cyClient = HIWORD (lParam) ;
  120.          
  121.              // calculate window size in characters       
  122.           cxBuffer = max (1, cxClient / cxChar) ;
  123.           cyBuffer = max (1, cyClient / cyChar) ;
  124.           InvalidateRect (hwnd, NULL, TRUE) ;
  125.   return 0;
  126.     case   WM_LBUTTONDOWN :
  127.  point.x = LOWORD (lParam) ;
  128.          point.y = HIWORD (lParam) ;
  129.  
  130.  //xFocu=point.y;
  131.  //xFocu=point.x;
  132.  return 0;
  133.    case   WM_LBUTTONUP ://左键放开后操作
  134.  point.x = LOWORD (lParam) ;//得到此点的坐标
  135.          point.y = HIWORD (lParam) ; 
  136.  xFocu=(point.x/cxChar)*cxChar;
  137.          yCaret=point.y/cyChar;//转换成一个字节的高度和宽度,即不出现在字符里面
  138.          xCaret=point.x/cxChar;
  139.  SetCaretPos (xFocu, yCaret*cyChar) ;//显示鼠标  
  140.  return 0;
  141.      case WM_SETFOCUS:
  142.                // create and show the caret
  143.           
  144.           CreateCaret (hwnd, NULL, 1, cyChar) ;
  145.   SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  146.   // SetCaretPos (xFocu, yCaret * cyChar) ;
  147.           ShowCaret (hwnd) ;
  148.           return 0 ;
  149.      case WM_KILLFOCUS:
  150.                // hide and destroy the caret
  151.           HideCaret (hwnd) ;
  152.           DestroyCaret () ;
  153.           return 0 ;
  154.      case WM_KEYDOWN:
  155.   for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
  156.   {
  157.             switch (wParam)
  158. {
  159.               case VK_LEFT:
  160.                 if(xCaret == 0 && yCaret != 0)
  161.                    {   
  162.                        yCaret-- ;
  163.                        xCaret=PRow[yCaret]->number ;
  164.                        xFocu=PRow[yCaret]->width ;
  165.                    }
  166.                  else
  167.                    {
  168.                        if((xCaret - 1)>=0)
  169.                        {
  170.    xCaret-- ;
  171.                            if((PRow[yCaret]->date[xCaret] >> 8)>0)
  172.                                 xFocu -= cxChar*2 ;
  173.                            else
  174.                                 xFocu -= cxChar ;
  175.    }
  176.  }   
  177.                break ;
  178.                
  179.           case VK_RIGHT:
  180.               if(xCaret == PRow[yCaret]->number && yCaret != RowSum-1)
  181.                    {
  182.                        yCaret++;
  183.                        xFocu=xCaret=0;
  184.                    }
  185.                    else
  186.                    {
  187.                        if((xCaret + 1)<=PRow[yCaret]->number)
  188.                        {
  189.                            if((PRow[yCaret]->date[xCaret] >> 8)>0)
  190.                               xFocu += cxChar*2 ;
  191.                            else
  192.                               xFocu += cxChar ;
  193.    xCaret++ ;
  194.                        }
  195.                    }
  196.                break ;
  197.                
  198.           case VK_UP:
  199.                 yCaret = max (yCaret - 1, 0) ;
  200.                    if(xFocu >= PRow[yCaret]->width)
  201.    {
  202. xCaret=PRow[yCaret]->number ;
  203. xFocu=PRow[yCaret]->width ;
  204.    }
  205.    else
  206.    {
  207.    while(x<xFocu)
  208.    {
  209. x+=CharWidth(PRow[yCaret]->date[i])*cxChar ;
  210. i++ ;
  211.    }
  212.    xCaret=i ;
  213.    xFocu=x ;
  214.    SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  215.    }
  216.                break ;
  217.                
  218.           case VK_DOWN:
  219.                 yCaret = min (yCaret + 1, RowSum - 1) ;
  220.                    if(xFocu >= PRow[yCaret]->width)
  221.    {
  222. xCaret=PRow[yCaret]->number ;
  223. xFocu=PRow[yCaret]->width ;
  224.    }
  225.    else
  226.    {
  227.    while(x<xFocu)
  228.    {
  229. x+=CharWidth(PRow[yCaret]->date[i])*cxChar ;
  230. i++ ;
  231.    }
  232.    xCaret=i ;
  233.    xFocu=x ;
  234.    }
  235.                break ;
  236. case VK_DELETE:
  237.                   if(xCaret<PRow[yCaret]->number)
  238.                   {
  239.                     DeleteChar(PRow,yCaret,xCaret);
  240.                     HideCaret (hwnd) ;
  241.                     hdc = GetDC (hwnd) ;
  242.                     SetFont(hdc, dwCharSet) ;
  243.                     ClsChar(hdc , xFocu, (yCaret-iVertPos-iVertPos) * cyChar, min(cxBuffer*cxChar,PRow[yCaret-iVertPos]->width)+1, (yCaret-iVertPos+1) * cyChar) ;
  244.                     TextOut (hdc, xFocu, (yCaret-iVertPos-iVertPos) * cyChar,PRow[yCaret-iVertPos]->date+xCaret, PRow[yCaret-iVertPos]->number-xCaret) ;
  245.                     DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  246.                     if((PRow[yCaret]->date[xCaret] >> 8)>0)
  247.                         PRow[yCaret]->width -= cxChar*2;
  248.                     else
  249.                         PRow[yCaret]->width -= cxChar ;                   
  250.                     ReleaseDC (hwnd, hdc) ;
  251.                     ShowCaret (hwnd) ;
  252.                   }
  253.                    return 0;
  254.               }
  255.           SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  256.           return 0 ;
  257.   }
  258.          
  259.      case WM_CHAR:
  260.           for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
  261.           {
  262.                switch (wParam)
  263.                {
  264.                case 'b':                    // backspace
  265.                    if (xCaret > 0)
  266.                     {
  267.                          xCaret-- ;
  268.                          if((PRow[yCaret]->date[xCaret] >> 8)>0)
  269.                             xFocu -= cxChar*2 ;
  270.                          else
  271.                             xFocu -= cxChar ;
  272.                          SendMessage (hwnd, WM_KEYDOWN, VK_DELETE, 1) ;
  273.                     }
  274.                     else if(xCaret == 0 && yCaret != 0)
  275.                     {
  276.                         x=PRow[yCaret-1]->number ;
  277. xFocu=PRow[yCaret-1]->width ;
  278.                         CopyRow(PRow,yCaret,xCaret,yCaret-1,PRow[yCaret-1]->number);
  279. PRow[yCaret-1]->width=xFocu + PRow[yCaret]->width;
  280.                         DeleteRow(PRow,yCaret);
  281.                         xCaret=x;yCaret--;
  282.                         HideCaret (hwnd) ;
  283.                         hdc = GetDC (hwnd) ;
  284.                         SetFont(hdc, dwCharSet) ;                        
  285.                         ClsChar(hdc , 0 , (yCaret-iVertPos) * cyChar , cxBuffer*cxChar, (RowSum-iVertPos+1) * cyChar);
  286.                         for (y = yCaret ; y < RowSum ; y++)
  287.                             TextOut (hdc, 0, (y-iVertPos) * cyChar,PRow[y]->date, PRow[y]->number) ;
  288.                         DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  289.                         ReleaseDC (hwnd, hdc) ;
  290.                         ShowCaret (hwnd) ;
  291.                         SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
  292.                     }
  293.                     break ;
  294.                     
  295.                case 't':                    // tab
  296.                     do
  297.                     {
  298.                          SendMessage (hwnd, WM_CHAR, ' ', 1) ;
  299.                     }
  300.                     while (xCaret % 8 != 0) ;
  301.                     break ;
  302.                     
  303.                case 'n':                    // line feed
  304.                     break ;
  305.                     
  306.                case 'r':                    // 回车换行
  307.                     xFocu = 0 ;
  308.                     InsertRow(PRow,yCaret+1); //插入一行
  309.                     HideCaret (hwnd) ;
  310.                     hdc = GetDC (hwnd) ;
  311.                     SetFont(hdc, dwCharSet) ;
  312.                     if(xCaret < PRow[yCaret]->number)//判断是否为在已输入字符间换行
  313. CopyRow(PRow,yCaret,xCaret,yCaret+1,0);//如果是需将后面字符连接到下一行
  314.                     
  315.                     ClsChar(hdc , 0 , (yCaret-iVertPos) * cyChar , cxBuffer*cxChar, (RowSum-iVertPos) * cyChar); //将后面字符用背景色覆盖               
  316.                     for (y = yCaret ; y < RowSum ; y++)
  317.                         TextOut (hdc, 0, (y-iVertPos) * cyChar,PRow[y]->date, PRow[y]->number) ;//重新输出
  318.                     DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  319.                     yCaret++;
  320.                     ReleaseDC (hwnd, hdc) ;
  321.                     ShowCaret (hwnd) ;
  322.                     xCaret=0;
  323.                     break ;
  324.                     
  325.                case 'x1B':                
  326.                     break ;
  327.                          
  328.                default:                      // character codes
  329.                     InputChar(PRow,yCaret,xCaret,(TCHAR)wParam);
  330.                     HideCaret (hwnd) ;
  331.                     hdc = GetDC (hwnd) ;
  332.                     SetFont(hdc, dwCharSet) ;
  333.                     TextOut (hdc, xFocu, (yCaret-iVertPos) * cyChar,PRow[yCaret-iVertPos]->date+xCaret, PRow[yCaret-iVertPos]->number-xCaret) ;
  334.                     DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  335.                     ReleaseDC (hwnd, hdc) ;
  336.                     ShowCaret (hwnd) ;
  337.                     xCaret++  ;
  338.                     if((wParam >> 8)>0)
  339.                     {
  340.                         PRow[yCaret]->width += cxChar*2 ;
  341.                         xFocu += cxChar*2 ;
  342.                     }
  343.                     else
  344.                     {
  345.                         PRow[yCaret]->width += cxChar ;
  346.                         xFocu += cxChar ;
  347.                     }
  348.                     break;
  349.                }
  350.           }
  351.           if(yCaret >= cyBuffer+iVertPos)
  352.           {
  353.               x=yCaret-cyBuffer+1 ;
  354.               SendMessage(hwnd, WM_VSCROLL, SB_THUMBTRACK, x+1);
  355.               return 0;
  356.           }
  357.           else if(yCaret < iVertPos)
  358.           {
  359.               SendMessage(hwnd, WM_VSCROLL, SB_THUMBTRACK, yCaret+1);
  360.               return 0;
  361.           }
  362.           SetCaretPos (xFocu, (yCaret-iVertPos) * cyChar) ;
  363.           return 0 ;
  364.  
  365.   case WM_PAINT:
  366.           hdc = BeginPaint (hwnd, &p) ;
  367.           SetFont(hdc, dwCharSet) ;
  368.           SetWindowOrgEx(hdc,cxChar * iHorzPos,cyChar * iVertPos,NULL);
  369.           iPaintBeg = max (0, iVertPos + p.rcPaint.top / cyChar) ;
  370.           iPaintEnd = min (RowSum - 1, iVertPos + p.rcPaint.bottom / cyChar) ;
  371.           for (y = iPaintBeg ; y <= iPaintEnd ; y++)
  372.               TextOut (hdc, 0, y * cyChar,PRow[y]->date, PRow[y]->number) ;
  373.           DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  374.    if(text!=NULL)
  375.  {
  376.    HideCaret (hwnd) ;
  377.   for(int i=0;i<strlen(text);i++)
  378.  {
  379.  if(text[i]=='r'&&text[i+1]=='n')//处理回车换行符
  380.  {
  381.    row+=cyChar;
  382.    cols=0;
  383.    i++;       //跳过回车换行符
  384.  }
  385.                 else if(text[i]=='t')          //处理制表符
  386. {
  387. cols+=8*cxChar;               
  388.  }
  389.                          else if(IsDBCSLeadByte(text[i]))
  390. {
  391.                    TextOut(hdc,cols,row,&text[i],2);
  392.                            cols+=cxChar*2;
  393. i++;
  394.  }
  395.  else
  396.  {
  397. TextOut(hdc,cols,row,&text[i],1);
  398.  cols+=cxChar;
  399.  }
  400.  }
  401.             
  402.   }
  403.           EndPaint (hwnd, &p) ;
  404.           return 0 ;
  405.    case WM_INITMENUPOPUP:
  406.           if (lParam == 1)
  407.           {
  408.               /* EnableMenuItem ((HMENU) wParam, IDM_EDIT_UNDO,
  409.                     SendMessage (hwnd, EM_CANUNDO, 0, 0) ?
  410.                                    MF_ENABLED : MF_GRAYED) ;
  411.                
  412.                EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE,
  413.                     IsClipboardFormatAvailable (CF_TEXT) ?
  414.                                    MF_ENABLED : MF_GRAYED) ;
  415.                
  416.                iSelect = SendMessage (hwnd, EM_GETSEL, 0, 0) ;
  417.                
  418.                if (HIWORD (iSelect) == LOWORD (iSelect))
  419.                     iEnable = MF_GRAYED ;
  420.                else
  421.                     iEnable = MF_ENABLED ;
  422.                
  423.                EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT,   iEnable) ;
  424.                EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY,  iEnable) ;
  425.                EnableMenuItem ((HMENU) wParam, IDM_EDIT_CLEAR, iEnable) ;
  426.                break;*/
  427.  EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE,
  428.                IsClipboardFormatAvailable (CF_TCHAR) ? MF_ENABLED : MF_GRAYED) ;
  429.           bEnable = pText ? MF_ENABLED : MF_GRAYED ;
  430.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT,   bEnable) ;
  431.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY,  bEnable) ;
  432.           EnableMenuItem ((HMENU) wParam, IDM_EDIT_CLEAR, bEnable) ;
  433.           break ;
  434.           }
  435.   else
  436.   { iEnable = hwnd == NULL ?
  437.                               MF_ENABLED : MF_GRAYED ;
  438.                
  439.                EnableMenuItem ((HMENU) wParam, IDM_SEARCH_FIND,    iEnable) ;
  440.                EnableMenuItem ((HMENU) wParam, IDM_SEARCH_REPLACE, iEnable) ;
  441.                break ;
  442.   }
  443.            return 0;
  444.       case WM_COMMAND:
  445.                // Messages from edit control
  446.           switch (LOWORD (wParam))
  447.           {
  448.   
  449.    case IDM_FILE_OPEN:
  450.   OpenFileProc(hwnd);
  451.           text=GetText(hwnd); 
  452.              InvalidateRect (hwnd, NULL, TRUE) ; 
  453.               break;
  454. case IDM_FILE_SAVE:
  455.                  if     (szFileName[0])
  456.                SaveFileProc(hwnd,szFileName,text);
  457.  else MessageBox(hwnd,"zenmehuishi?",strWindowTitle,MB_ICONEXCLAMATION);
  458.      break;
  459. case IDM_FILE_SAVE_AS: 
  460. break;
  461.          case IDM_APP_EXIT:
  462.                SendMessage (hwnd, WM_CLOSE, 0, 0) ;
  463.                return 0 ;
  464.                
  465.                     // Messages from Edit menu
  466.                
  467.           case IDM_EDIT_UNDO:
  468.              if (pText)
  469.                {
  470.                     free (pText) ;
  471.                     pText = NULL ;
  472.                }
  473.                InvalidateRect (hwnd, NULL, TRUE) ;
  474.                return 0 ;
  475.                
  476.           case IDM_EDIT_CUT:
  477.                SendMessage (hwnd, WM_CUT, 0, 0) ;
  478.                return 0 ;
  479.                
  480.           case IDM_EDIT_COPY:
  481.                if (!pText)
  482.                     return 0 ;
  483.                hGlobal = GlobalAlloc (GHND | GMEM_SHARE, 
  484.                                       (lstrlen (pText) + 1) * sizeof (TCHAR)) ;
  485.                pGlobal =(char *) GlobalLock (hGlobal) ;
  486.                lstrcpy (pGlobal, pText) ;
  487.                GlobalUnlock (hGlobal) ;
  488.                OpenClipboard (hwnd) ;
  489.                EmptyClipboard () ;
  490.                SetClipboardData (CF_TCHAR, hGlobal) ;
  491.                CloseClipboard () ;
  492.                if (LOWORD (wParam) == IDM_EDIT_COPY)
  493.                     return 0 ;        
  494.      
  495.                
  496.           case IDM_EDIT_PASTE:
  497.                 OpenClipboard (hwnd) ;
  498.                if (hGlobal = GetClipboardData (CF_TCHAR))
  499.                {
  500.                     pGlobal = (char *)GlobalLock (hGlobal) ;
  501.                     if (pText)
  502.                     {
  503.                          free (pText) ;
  504.                          pText = NULL ;
  505.                     }
  506.                     pText = (char *)malloc (GlobalSize (hGlobal)) ;
  507.                     lstrcpy (pText, pGlobal) ;
  508.                     InvalidateRect (hwnd, NULL, TRUE) ;
  509.                }
  510.                CloseClipboard () ;
  511.                return 0 ;
  512.                
  513.           case IDM_EDIT_CLEAR:
  514.                SendMessage (hwnd, WM_CLEAR, 0, 0) ;
  515.                return 0 ;
  516.                
  517.           case IDM_EDIT_SELECT_ALL:
  518.                SendMessage (hwnd, EM_SETSEL, 0, -1) ;
  519.                return 0 ;
  520.                
  521.                     // Messages from Search menu
  522.                
  523.         case IDM_SEARCH_FIND:
  524.                SendMessage (hwnd, EM_GETSEL, 0, (LPARAM) &iOffset) ;
  525.                //hDlgModeless = FindFindDlg (hwnd) ;
  526.                return 0 ;
  527.       
  528.                
  529.           case IDM_SEARCH_REPLACE:
  530.                SendMessage (hwnd, EM_GETSEL, 0, (LPARAM) &iOffset) ;
  531.                //hDlgModeless = FindReplaceDlg (hwnd) ;
  532.                return 0 ;
  533.            case IDM_APP_ABOUT:
  534.                MessageBox (hwnd, TEXT ("created by ll,2010_3"),
  535.                            szAppName, MB_OK | MB_ICONINFORMATION) ;
  536.                return 0 ;
  537.  case IDM_FORMAT_FONT:
  538.                if (ChooseFont (&cf))
  539.                     InvalidateRect (hwnd, NULL, TRUE) ;
  540.                return 0 ;
  541.           }
  542.           break ;
  543.           
  544.      case WM_DESTROY:
  545.           PostQuitMessage (0) ;
  546.           return 0 ;
  547.      }
  548.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  549. }
  550. BOOL OpenFileDlg(HWND hWnd,PTSTR pstrFileName, PTSTR pstrTitleName)
  551. {
  552. OPENFILENAME ofn; 
  553. ZeroMemory(&ofn, sizeof(ofn));
  554. ofn.lStructSize = sizeof(ofn);
  555. ofn.hwndOwner = hWnd;
  556. ofn.lpstrFile = pstrFileName;
  557. ofn.lpstrFile[0] = '';
  558. ofn.nMaxFile = FILEPATHSIZE;
  559. ofn.lpstrFilter = "文本文档(*.txt)*.txt所有文件*.*";
  560. ofn.nFilterIndex = 1;
  561. ofn.lpstrFileTitle = pstrTitleName;
  562. ofn.nMaxFileTitle = FILENAMESIZE;
  563. ofn.lpstrInitialDir = NULL;
  564. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  565. if ( GetOpenFileName(&ofn) )
  566. return TRUE;
  567. return FALSE;
  568. }
  569. void OpenFileProc(HWND hwnd)
  570. {
  571.     int MsgBoxRst;
  572. HDC hdc;
  573. char *str1=NULL,*strFileNameTemp,*strFilePathTemp;
  574. HANDLE hFile; 
  575. int filesize;
  576. BOOL IsSaved=TRUE;
  577. hdc = GetDC (hwnd) ;
  578.     DWORD dwRead;
  579.   PBYTE pConv;
  580. if(!IsSaved)
  581. {
  582. str1=(char *)calloc(FILENAMESIZE+30,sizeof(char));
  583. strcpy(str1,"文件 ");
  584. strcat(str1,strFileName);
  585. strcat(str1," 的文字已经改变。nn想保存文件吗?");
  586.         MsgBoxRst=MessageBox(hwnd,str1,strWindowTitle,MB_YESNOCANCEL|MB_ICONEXCLAMATION );
  587. free(str1);
  588. if(MsgBoxRst==IDYES)
  589. SendMessage(hwnd,WM_COMMAND,IDM_FILE_SAVE,0);
  590. }
  591. if(IsSaved|| MsgBoxRst==IDNO)
  592. {
  593. strFileNameTemp=(char *)calloc(FILENAMESIZE,sizeof(char));
  594. strFilePathTemp=(char *)calloc(FILEPATHSIZE,sizeof(char));
  595. if(OpenFileDlg(hwnd,strFilePathTemp,strFileNameTemp))
  596. {
  597. hFile = CreateFile(strFilePathTemp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  598. if (hFile == INVALID_HANDLE_VALUE) 
  599. MessageBox(hwnd,"打开文件失败!",strWindowTitle,MB_ICONEXCLAMATION);
  600. else
  601. {
  602.      filesize=GetFileSize(hFile,NULL);//得到文件长度
  603.  str1=(char *)calloc(filesize+1,sizeof(char));//分配内存
  604.  memset(str1,0,filesize+1); //初始化为空
  605. if(ReadFile(hFile,str1,filesize,&dwRead,NULL))
  606. {
  607.                  
  608. str1[dwRead]=0;
  609. CloseHandle(hFile);
  610.                     pConv =(PBYTE)  malloc (2 *filesize  + 2) ;
  611.                  
  612.  // If the edit control is Unicode, convert ASCII text.
  613.         
  614. #ifdef UNICODE
  615.         
  616.            MultiByteToWideChar (CP_ACP, 0, str1, -1, (PTSTR) pConv, filesize + 1) ;
  617.         
  618.   // If not, just copy buffer       
  619. #else
  620.         
  621.                   lstrcpy ((PTSTR) pConv, str1) ;
  622.         
  623. #endif
  624.                       Text=(char *)pConv;
  625. //TextOut (hdc, 0, 0,str1,filesize) ;
  626. }
  627. else
  628. MessageBox(hwnd,"读取文件失败!",strWindowTitle,MB_ICONEXCLAMATION);
  629. free(str1);
  630. CloseHandle(hFile);
  631. }
  632. }
  633. free(strFileNameTemp);
  634. free(strFilePathTemp);
  635. }
  636. }
  637. void SaveFileProc(HWND hwnd, PTSTR pstrFileName, PTSTR pstrtext)
  638. //(HWND hwnd)
  639. {
  640. char *str1=NULL;
  641. HANDLE hFile; 
  642. int filesize;
  643. BOOL IsSaved;
  644. DWORD         dwWrite ;
  645.     WORD          wByteOrderMark = 0xFEFF ;
  646.   
  647. hFile = CreateFile(pstrFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  648. if (hFile == INVALID_HANDLE_VALUE) 
  649. //str1=(char *)calloc(FILEPATHSIZE+30,sizeof(char));
  650. //strcpy(str1,"不能创建文件 ");
  651. //strcat(str1,strFilePath);
  652. //strcat(str1,"。nn请确认路径和文件名是否正确。");
  653. // MessageBox(hwnd,str1,strWindowTitle,MB_ICONEXCLAMATION);
  654.       MessageBox(hwnd,"不能创建文件",strWindowTitle,MB_ICONEXCLAMATION);
  655. free(str1);
  656. SendMessage(hwnd,WM_COMMAND,IDM_FILE_SAVE_AS,0);
  657. }
  658. else
  659. {
  660.     filesize=strlen(pstrtext); 
  661. str1=(char*)calloc(filesize+1,sizeof(char)); 
  662. //if(WriteFile(hFile,str1,filesize+1,&num1,NULL))
  663. #ifdef UNICODE
  664.         
  665.            WriteFile (hFile, &wByteOrderMark, 2, &dwWrite, NULL) ;
  666.    IsSaved=TRUE;
  667.         
  668.             #endif
  669.         
  670.                   // Get the edit buffer and write that out to the file.
  671.    str1=pstrtext;
  672.         
  673.            WriteFile (hFile, str1,  filesize* sizeof (TCHAR),
  674.         
  675.                                          &dwWrite, NULL) ;
  676.         
  677.     
  678. MessageBox(hwnd,"保存文件失败!",strWindowTitle,MB_ICONEXCLAMATION);
  679. free(str1);
  680. CloseHandle(hFile);
  681. }
  682. }
  683. PTSTR  GetText(HWND hwnd)
  684. {
  685. return Text;
  686. }