MULTIWND.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:17k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /*      MULTIWND.C                                                    */
  4. /*                                                                    */
  5. /*      Copyright (c) 1995-1996  Microsoft Corporation                     */
  6. /*                                                                    */
  7. /**********************************************************************/
  8. #include "windows.h"
  9. #include "imm.h"
  10. #include "resource.h"
  11. #include "multiui.h"
  12. /**********************************************************************/
  13. /*                                                                    */
  14. /*    InitStringBuffer(HWND)                                          */
  15. /*                                                                    */
  16. /**********************************************************************/
  17. void FAR PASCAL InitStringBuffer(hWnd)
  18. HWND hWnd;
  19. {
  20.     LPSTR lpStr = NULL;
  21.     if (lpStr = GlobalAlloc(GPTR,MAXSIZE_STR))
  22.     {
  23.         SetWindowLong(hWnd,MYGWL_LPSTR,(LONG)lpStr);
  24.     }
  25.     else
  26.     {
  27.         SetWindowLong(hWnd,MYGWL_LPSTR,(LONG)0L);
  28.     }
  29. }
  30. /**********************************************************************/
  31. /*                                                                    */
  32. /*    DestroyStringBuffer(HWND)                                       */
  33. /*                                                                    */
  34. /**********************************************************************/
  35. void FAR PASCAL DestroyStringBuffer(hWnd)
  36. HWND hWnd;
  37. {
  38.     LPSTR lpStr = NULL;
  39.     lpStr = (LPSTR)GetWindowLong(hWnd,MYGWL_LPSTR);
  40.     if (lpStr)
  41.     {
  42.         GlobalFree((HANDLE)lpStr);
  43.     }
  44. }
  45. /**********************************************************************/
  46. /*                                                                    */
  47. /*    MyDrawString(HWND,WPARAM,LPARAM)                                */
  48. /*                                                                    */
  49. /**********************************************************************/
  50. void FAR PASCAL MyDrawString(hWnd,wParam,lParam)
  51. HWND hWnd;
  52. WPARAM wParam;
  53. LPARAM lParam;
  54. {
  55.     LPSTR lpStr = NULL;
  56.     HDC   hDC;
  57.     PAINTSTRUCT ps;
  58.     HFONT hOldFont;
  59.     HFONT hFont;
  60.     DWORD dwHeight;
  61.     TEXTMETRIC      tm;
  62.     RECT rc;
  63.     lpStr = (LPSTR)GetWindowLong(hWnd,MYGWL_LPSTR);
  64.     if (!lpStr)
  65.         return;
  66.     hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  67.     if (!hFont)
  68.         return;
  69.     hDC = BeginPaint(hWnd, &ps);
  70.     hOldFont = SelectObject(hDC, hFont);
  71.     lpStr = (LPSTR)GetWindowLong(hWnd,MYGWL_LPSTR);
  72.     // Get dwHeight.
  73.     GetTextMetrics(hDC,&tm);
  74.     dwHeight = tm.tmHeight + tm.tmExternalLeading;
  75.     GetClientRect(hWnd,&rc);
  76.     rc.bottom -= dwHeight;
  77.     DrawText(hDC,lpStr,-1,&rc,DT_LEFT | DT_WORDBREAK);
  78.     SelectObject(hDC, hOldFont);
  79.     EndPaint(hWnd, &ps);
  80. }
  81. /**********************************************************************/
  82. /*                                                                    */
  83. /*    MySetCompositionFont(HWND)                                      */
  84. /*                                                                    */
  85. /**********************************************************************/
  86. void FAR PASCAL MySetCompositionFont(hWnd)
  87. HWND hWnd;
  88. {
  89.     HIMC hIMC;
  90.     if (hIMC = ImmGetContext(hWnd))
  91.     {
  92.         LOGFONT lf;
  93.         HFONT   hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  94.         GetObject(hFont,sizeof(lf),&lf);
  95.         ImmSetCompositionFont(hIMC,&lf);
  96.         ImmReleaseContext(hWnd, hIMC);
  97.     }
  98. }
  99. /**********************************************************************/
  100. /*                                                                    */
  101. /*    MySetCompositionForm(HWND)                                      */
  102. /*                                                                    */
  103. /**********************************************************************/
  104. void FAR PASCAL MySetCompositionForm(hWnd)
  105. HWND hWnd;
  106. {
  107.     RECT            rc;
  108.     HIMC            hIMC;
  109.     HDC             hDC;
  110.     TEXTMETRIC      tm;
  111.     DWORD           dwHeight;
  112.     HFONT           hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  113.     HFONT           hOldFont;
  114.     COMPOSITIONFORM CompForm;
  115.     if (!hFont)
  116.         return;
  117.     if (hIMC = ImmGetContext(hWnd))
  118.     {
  119.         // Get dwHeight.
  120.         hDC = GetDC(hWnd);
  121.         hOldFont = SelectObject(hDC, hFont);
  122.         GetTextMetrics(hDC,&tm);
  123.         dwHeight = tm.tmHeight + tm.tmExternalLeading;
  124.         SelectObject(hDC, hOldFont);
  125.         ReleaseDC(hWnd,hDC);
  126.         GetClientRect(hWnd,&rc);
  127.         CompForm.dwStyle = CFS_POINT;
  128.         CompForm.ptCurrentPos.x = rc.left;
  129.         CompForm.ptCurrentPos.y = rc.bottom - dwHeight;
  130.         ImmSetCompositionWindow(hIMC,&CompForm);
  131.         ImmReleaseContext(hWnd, hIMC);
  132.     }
  133. }
  134. /**********************************************************************/
  135. /*                                                                    */
  136. /*    MyChangeFont(HWND)                                              */
  137. /*                                                                    */
  138. /**********************************************************************/
  139. void FAR PASCAL MyChangeFont(hWnd)
  140. HWND hWnd;
  141. {
  142.     LOGFONT lf;
  143.     CHOOSEFONT cf;
  144.     HFONT hFont;
  145.     /* Set all structure fields to zero. */
  146.     memset(&cf, 0, sizeof(CHOOSEFONT));
  147.     cf.lStructSize = sizeof(CHOOSEFONT);
  148.     cf.hwndOwner = hWnd;
  149.     cf.lpLogFont = &lf;
  150.     cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
  151.     cf.rgbColors = RGB(0, 255, 255); /* light blue */
  152.     cf.nFontType = SCREEN_FONTTYPE;
  153.     if (ChooseFont(&cf))
  154.     {
  155.         hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  156.         DeleteObject(hFont);
  157.         hFont = CreateFontIndirect(&lf);
  158.         SetWindowLong(hWnd,MYGWL_FONT,(LONG)hFont);
  159.         MySetCompositionForm(hWnd);
  160.         MySetCompositionFont(hWnd);
  161.     }
  162.     SetFocus(hWnd);
  163. }
  164. /**********************************************************************/
  165. /*                                                                    */
  166. /*    GetDefaultGUIFont(void)                                         */
  167. /*                                                                    */
  168. /**********************************************************************/
  169. HFONT GetDefaultGUIFont(void)
  170. {
  171.     static HFONT hFont;
  172.     OSVERSIONINFO VerInfo;
  173.     if (hFont)
  174.         return hFont;
  175.     VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  176.     GetVersionEx(&VerInfo);
  177.     if (VerInfo.dwMajorVersion < 4) {
  178.         NONCLIENTMETRICS ncm;
  179.         ncm.cbSize = sizeof(NONCLIENTMETRICS);
  180.         SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
  181.         hFont = CreateFontIndirect(&ncm.lfStatusFont);
  182.         if (!hFont)
  183.             hFont = GetStockObject(SYSTEM_FONT);
  184.     } else {
  185.         hFont = GetStockObject(DEFAULT_GUI_FONT);
  186.     }
  187.     return hFont;
  188. }
  189. /**********************************************************************/
  190. /*                                                                    */
  191. /*    OverFlowText(HWND,LPSTR,LPSTR)                                  */
  192. /*                                                                    */
  193. /**********************************************************************/
  194. BOOL FAR PASCAL OverFlowText(hWnd,lpStr,lpResultStr)
  195. HWND hWnd;
  196. LPSTR lpStr;
  197. LPSTR lpResultStr;
  198. {
  199.     BOOL            bRet = FALSE;
  200.     RECT            rc;
  201.     HDC             hDC;
  202.     TEXTMETRIC      tm;
  203.     int             nHeight;
  204.     HFONT           hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  205.     HFONT           hOldFont;
  206.     LPSTR           lpTemp;
  207.     if (!hFont)
  208.         return FALSE;
  209.     if (lstrlen(lpStr) + lstrlen(lpResultStr) > MAXSIZE_STR)
  210.     {
  211.         bRet = TRUE;
  212.     }
  213.     else
  214.     {
  215.         lpTemp = GlobalAlloc(GPTR,MAXSIZE_STR);
  216.         if (lpTemp)
  217.         {
  218.             lstrcpy(lpTemp,lpStr);
  219.             lstrcat(lpTemp,lpResultStr);
  220.             GetClientRect(hWnd,&rc);
  221.             hDC = GetDC(hWnd);
  222.             hOldFont = SelectObject(hDC, hFont);
  223.             GetTextMetrics(hDC,&tm);
  224.             rc.bottom -= tm.tmHeight + tm.tmExternalLeading;
  225.             nHeight = rc.bottom;
  226.             if (nHeight < DrawText(hDC,lpTemp,-1,&rc,DT_LEFT | DT_WORDBREAK | DT_CALCRECT))
  227.             {
  228.                 bRet = TRUE;
  229.             }
  230.             SelectObject(hDC, hOldFont);
  231.             ReleaseDC(hWnd,hDC);
  232.             GlobalFree((HANDLE)lpTemp);
  233.         }
  234.     }
  235.     return bRet;
  236. }
  237. /**********************************************************************/
  238. /*                                                                    */
  239. /*    AddResultString(HWND)                                           */
  240. /*                                                                    */
  241. /**********************************************************************/
  242. void FAR PASCAL AddResultString(hWnd)
  243. HWND hWnd;
  244. {
  245.     HIMC  hIMC;
  246.     DWORD dwLen;
  247.     LPSTR lpStr;
  248.     LPSTR lpResultStr;
  249.     lpStr = (LPSTR)GetWindowLong(hWnd,MYGWL_LPSTR);
  250.     hIMC = ImmGetContext(hWnd);
  251.     if (!hIMC)
  252.         return;
  253.     dwLen = ImmGetCompositionString(hIMC,GCS_RESULTSTR,NULL,0L);
  254.     if (dwLen)
  255.     {
  256.         lpResultStr = GlobalAlloc(GPTR,dwLen+1);
  257.         if (lpResultStr)
  258.         {
  259.             ImmGetCompositionString(hIMC,GCS_RESULTSTR,
  260.                                                   lpResultStr,dwLen+1);
  261.             if (OverFlowText(hWnd,lpStr,lpResultStr))
  262.                 lstrcpy(lpStr,lpResultStr);
  263.             else
  264.                 lstrcat(lpStr,lpResultStr);
  265.             InvalidateRect(hWnd,NULL,TRUE);
  266.             GlobalFree((HANDLE)lpResultStr);
  267.         }
  268.     }
  269.     ImmReleaseContext(hWnd,hIMC);
  270. }
  271. /**********************************************************************/
  272. /*                                                                    */
  273. /*    NoUINoIMCWndProc(HWND, UINT, WPARAM, LPARAM)                    */
  274. /*                                                                    */
  275. /**********************************************************************/
  276. long FAR PASCAL NoUINoIMCWndProc(hWnd, message, wParam, lParam)
  277. HWND hWnd;
  278. UINT message;
  279. WPARAM wParam;
  280. LPARAM lParam;
  281. {
  282.     HFONT hFont;
  283.     switch (message) 
  284.     {
  285. case WM_CREATE:
  286.             hFont = GetDefaultGUIFont();
  287.             SetWindowLong(hWnd,MYGWL_FONT,(LONG)hFont);
  288.             InitStringBuffer(hWnd);
  289.     break;
  290.         case WM_LBUTTONDBLCLK:
  291.             MyChangeFont(hWnd);
  292.     break;
  293.         case WM_LBUTTONDOWN:
  294.             SetFocus(hWnd);
  295.     break;
  296.         case WM_SETFOCUS:
  297.             MySetCompositionFont(hWnd);
  298.             MySetCompositionForm(hWnd);
  299.     break;
  300.         case WM_IME_COMPOSITION:
  301.             if (lParam & GCS_RESULTSTR)
  302.             {
  303.                 AddResultString(hWnd);
  304.             }
  305.     return (DefWindowProc(hWnd, message, wParam, lParam));
  306.     break;
  307.         case WM_SIZE:
  308.             switch (wParam)
  309.             {
  310.                 case SIZENORMAL:
  311.                 case SIZEFULLSCREEN:
  312.                     if (hWnd == GetFocus())
  313.                     {
  314.                         MySetCompositionForm(hWnd);
  315.                     }
  316.                     InvalidateRect(hWnd,NULL,TRUE);
  317.                     break;
  318.                 case SIZEICONIC:
  319.             return (DefWindowProc(hWnd, message, wParam, lParam));
  320.                     break;
  321.             }
  322.     break;
  323. case WM_PAINT:
  324.             MyDrawString(hWnd,wParam,lParam);
  325.     break;
  326. case WM_DESTROY:
  327.             hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  328.             DeleteObject(hFont);
  329.             DestroyStringBuffer(hWnd);
  330.     break;
  331. default:
  332.     return (DefWindowProc(hWnd, message, wParam, lParam));
  333.     }
  334.     return 0L;
  335. }
  336. /**********************************************************************/
  337. /*                                                                    */
  338. /*    NoUIOwnIMCWndProc(HWND, UINT, WPARAM, LPARAM)                   */
  339. /*                                                                    */
  340. /**********************************************************************/
  341. long FAR PASCAL NoUIOwnIMCWndProc(hWnd, message, wParam, lParam)
  342. HWND hWnd;
  343. UINT message;
  344. WPARAM wParam;
  345. LPARAM lParam;
  346. {
  347.     HIMC hIMC;
  348.     HFONT hFont;
  349.     switch (message) 
  350.     {
  351. case WM_CREATE:
  352.             hFont = GetDefaultGUIFont();
  353.             SetWindowLong(hWnd,MYGWL_FONT,(LONG)hFont);
  354.             hIMC = ImmCreateContext();
  355.             ImmAssociateContext(hWnd,hIMC);
  356.             // Save hIMC into WndExtra.
  357.             SetWindowLong(hWnd,MYGWL_IMC,(LONG)hIMC);
  358.             MySetCompositionFont(hWnd);
  359.             MySetCompositionForm(hWnd);
  360.             InitStringBuffer(hWnd);
  361.     break;
  362.         case WM_LBUTTONDBLCLK:
  363.             MyChangeFont(hWnd);
  364.     break;
  365.         case WM_LBUTTONDOWN:
  366.             SetFocus(hWnd);
  367.     break;
  368.         case WM_IME_COMPOSITION:
  369.             if (lParam & GCS_RESULTSTR)
  370.             {
  371.                 AddResultString(hWnd);
  372.             }
  373.     return (DefWindowProc(hWnd, message, wParam, lParam));
  374.     break;
  375.         case WM_SIZE:
  376.             switch (wParam)
  377.             {
  378.                 case SIZENORMAL:
  379.                 case SIZEFULLSCREEN:
  380.                     MySetCompositionForm(hWnd);
  381.                     InvalidateRect(hWnd,NULL,TRUE);
  382.                     break;
  383.                 case SIZEICONIC:
  384.             return (DefWindowProc(hWnd, message, wParam, lParam));
  385.                     break;
  386.             }
  387.     break;
  388. case WM_PAINT:
  389.             MyDrawString(hWnd,wParam,lParam);
  390.     break;
  391. case WM_DESTROY:
  392.             hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  393.             DeleteObject(hFont);
  394.             hIMC = (HIMC)GetWindowLong(hWnd,MYGWL_IMC);
  395.             ImmDestroyContext(hIMC);
  396.             DestroyStringBuffer(hWnd);
  397.     break;
  398. default:
  399.     return (DefWindowProc(hWnd, message, wParam, lParam));
  400.     }
  401.     return 0L;
  402. }
  403. /**********************************************************************/
  404. /*                                                                    */
  405. /*    OwnUIOwnIMCWndProc(HWND, UINT, WPARAM, LPARAM)                  */
  406. /*                                                                    */
  407. /**********************************************************************/
  408. long FAR PASCAL OwnUIOwnIMCWndProc(hWnd, message, wParam, lParam)
  409. HWND hWnd;
  410. UINT message;
  411. WPARAM wParam;
  412. LPARAM lParam;
  413. {
  414.     HFONT hFont;
  415.     HIMC  hIMC = 0L;
  416.     HWND  hIMEWnd = (HWND)GetWindowLong(hWnd,MYGWL_IMEWND);
  417.     if (IsWindow(hIMEWnd) && ImmIsUIMessage(hIMEWnd,message,wParam,lParam))
  418.     {
  419.         switch (message) 
  420.         {
  421.             case WM_IME_COMPOSITION:
  422.                 if (lParam & GCS_RESULTSTR)
  423.                 {
  424.                     AddResultString(hWnd);
  425.                 }
  426.         break;
  427.             default:
  428.         break;
  429.         }
  430. return 0L;
  431.     }
  432.     switch (message) 
  433.     {
  434. case WM_CREATE:
  435.             hFont = GetDefaultGUIFont();
  436.             SetWindowLong(hWnd,MYGWL_FONT,(LONG)hFont);
  437.             hIMC = ImmCreateContext();
  438.             ImmAssociateContext(hWnd,hIMC);
  439.             // Save hIMC into WndExtra.
  440.             SetWindowLong(hWnd,MYGWL_IMC,(LONG)hIMC);
  441.             if (!(hIMEWnd = CreateWindow("Ime", "",
  442.                     WS_POPUP | WS_DISABLED,
  443.             0,0,0,0,
  444.             hWnd, NULL, hInst, NULL)))
  445.         return -1;
  446.             // Save hIMEWnd into WndExtra.
  447.             SetWindowLong(hWnd,MYGWL_IMEWND,(LONG)hIMEWnd);
  448.             MySetCompositionFont(hWnd);
  449.             MySetCompositionForm(hWnd);
  450.             InitStringBuffer(hWnd);
  451.     break;
  452.         case WM_LBUTTONDBLCLK:
  453.             MyChangeFont(hWnd);
  454.     break;
  455.         case WM_LBUTTONDOWN:
  456.             SetFocus(hWnd);
  457.     break;
  458.         case WM_SIZE:
  459.             switch (wParam)
  460.             {
  461.                 case SIZENORMAL:
  462.                 case SIZEFULLSCREEN:
  463.                     MySetCompositionForm(hWnd);
  464.                     InvalidateRect(hWnd,NULL,TRUE);
  465.                     break;
  466.                 case SIZEICONIC:
  467.             return (DefWindowProc(hWnd, message, wParam, lParam));
  468.                     break;
  469.             }
  470.     break;
  471. case WM_PAINT:
  472.             MyDrawString(hWnd,wParam,lParam);
  473.     break;
  474. case WM_DESTROY:
  475.             hFont = (HFONT)GetWindowLong(hWnd,MYGWL_FONT);
  476.             DeleteObject(hFont);
  477.             hIMC = (HIMC)GetWindowLong(hWnd,MYGWL_IMC);
  478.             ImmDestroyContext(hIMC);
  479.             DestroyStringBuffer(hWnd);
  480.     break;
  481. default:
  482.     return (DefWindowProc(hWnd, message, wParam, lParam));
  483.     }
  484.     return 0L;
  485. }