Rover12421's Blog

The End.

Unicode下粘贴板操作(VS2008->MFC)

界面如下

image

源码:

Code Snippet
  1. void CClipBoardAppDlg::OnBnClickedBtnSend()
  2. {
  3.  
  4.     //Unicode to ANSI ,Using ANSI
  5.     //if (OpenClipboard())
  6.     //{
  7.     //    CString str;
  8.     //    HANDLE hClip;
  9.     //    char *pBuf;
  10.     //    EmptyClipboard();
  11.     //    GetDlgItemText(IDC_EDIT_SEND, str);
  12.     //    USES_CONVERSION;
  13.     //    char *szstr = T2A(str);
  14.     //    hClip = GlobalAlloc(GMEM_MOVEABLE, strlen(szstr) + 1);
  15.     //    pBuf = (char *)GlobalLock(hClip);
  16.     //    strcpy_s(pBuf, strlen(szstr) + 1, szstr);
  17.     //    GlobalUnlock(hClip);
  18.     //    SetClipboardData(CF_TEXT, hClip);
  19.     //    CloseClipboard();
  20.     //}
  21.  
  22.     //Using Unicode, Don't convert ANSI
  23.     if (OpenClipboard())
  24.     {
  25.         CString str;
  26.         HANDLE hClip;
  27.         TCHAR *pBuf;
  28.         EmptyClipboard();
  29.         GetDlgItemText(IDC_EDIT_SEND, str);
  30.         hClip = GlobalAlloc(GMEM_MOVEABLE, str.GetLength()*2 + 1);
  31.         pBuf = (TCHAR *)GlobalLock(hClip);
  32.         wcscpy_s(pBuf, str.GetLength() + 1, str);
  33.         GlobalUnlock(hClip);
  34.         SetClipboardData(CF_UNICODETEXT, hClip);
  35.         CloseClipboard();
  36.     }
  37. }
  38.  
  39. void CClipBoardAppDlg::OnBnClickedBtnRecv()
  40. {
  41.  
  42.     //Unicode to ANSI ,Using ANSI
  43.     //if (OpenClipboard())
  44.     //{
  45.     //    if (IsClipboardFormatAvailable(CF_TEXT))
  46.     //    {
  47.     //        HANDLE hClip;
  48.     //        char *pBuf;
  49.     //        hClip = GetClipboardData(CF_TEXT);
  50.     //        pBuf = (char *)GlobalLock(hClip);
  51.     //        GlobalUnlock(hClip);
  52.     //        USES_CONVERSION;
  53.     //        SetDlgItemText(IDC_EDIT_RECV, A2T(pBuf));
  54.     //        CloseClipboard();
  55.     //    }
  56.     //}
  57.  
  58.     //Using Unicode, Don't convert ANSI
  59.     if (OpenClipboard())
  60.     {
  61.         if (IsClipboardFormatAvailable(CF_UNICODETEXT))
  62.         {
  63.             HANDLE hClip;
  64.             TCHAR *pBuf;
  65.             hClip = GetClipboardData(CF_UNICODETEXT);
  66.             pBuf = (TCHAR *)GlobalLock(hClip);
  67.             GlobalUnlock(hClip);
  68.             SetDlgItemText(IDC_EDIT_RECV, pBuf);
  69.             CloseClipboard();
  70.         }
  71.     }
  72. }

最要是字符处理。

Comments