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

Windows编程

开发平台:

Visual C++

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /*      IMEAPPS.C                                                     */
  4. /*                                                                    */
  5. /*      Copyright (c) 1995-1997  Microsoft Corporation                */
  6. /*                                                                    */
  7. /**********************************************************************/
  8. #include "windows.h"
  9. #include "commdlg.h"
  10. #include "commctrl.h"
  11. #include "imm.h"
  12. #include "resource.h"
  13. #include "imeapps.h"
  14. /**********************************************************************/
  15. /*                                                                    */
  16. /*    WinMain(HINSTANCE, HINSTANCE, LPSTR, int)                             */
  17. /*                                                                    */
  18. /**********************************************************************/
  19. int APIENTRY WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  20. HINSTANCE hInstance;
  21. HINSTANCE hPrevInstance;
  22. LPSTR lpCmdLine;
  23. int nCmdShow;
  24. {
  25.     MSG msg;
  26.     if (!hPrevInstance)  
  27.         if (!InitApplication(hInstance))
  28.             return (FALSE);
  29.     if (!InitInstance(hInstance, nCmdShow))
  30. return (FALSE);
  31.     
  32.     while (GetMessage(&msg, NULL, 0, 0))
  33.     {
  34. TranslateMessage(&msg);
  35. DispatchMessage(&msg);
  36.     }
  37.     return (msg.wParam);
  38. }
  39. /**********************************************************************/
  40. /*                                                                    */
  41. /*    InitApplication(HANDLE)                                         */
  42. /*                                                                    */
  43. /**********************************************************************/
  44. BOOL InitApplication(hInstance)
  45. HANDLE hInstance;
  46. {
  47.     WNDCLASS  wc;
  48.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  49.     wc.lpfnWndProc   = MainWndProc;
  50.     wc.cbClsExtra    = 0;
  51.     wc.cbWndExtra    = 0;
  52.     wc.hInstance     = hInstance;
  53.     wc.hIcon         = LoadIcon(hInstance,"MyIcon");
  54.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  55.     wc.hbrBackground = GetStockObject(LTGRAY_BRUSH); 
  56.     wc.lpszMenuName  = "ImeAppsMenu";
  57.     wc.lpszClassName = "ImeAppsWClass";
  58.     if (! RegisterClass (&wc))
  59. return FALSE;
  60.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  61.     wc.lpfnWndProc   = CompStrWndProc;
  62.     wc.cbClsExtra    = 0;
  63.     wc.cbWndExtra    = 4;
  64.     wc.hInstance     = hInstance;
  65.     wc.hIcon         = NULL;
  66.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  67.     wc.hbrBackground = GetStockObject(LTGRAY_BRUSH); 
  68.     wc.lpszMenuName  = NULL;
  69.     wc.lpszClassName = "CompStrWndClass";
  70.     if (! RegisterClass (&wc))
  71. return FALSE;
  72.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  73.     wc.lpfnWndProc   = CandListWndProc;
  74.     wc.cbClsExtra    = 0;
  75.     wc.cbWndExtra    = 0;
  76.     wc.hInstance     = hInstance;
  77.     wc.hIcon         = NULL;
  78.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  79.     wc.hbrBackground = GetStockObject(LTGRAY_BRUSH); 
  80.     wc.lpszMenuName  = NULL;
  81.     wc.lpszClassName = "CandListWndClass";
  82.     if (! RegisterClass (&wc))
  83. return FALSE;
  84.     hFont = GetStockObject(DEFAULT_GUI_FONT);
  85.     return TRUE;
  86. }
  87. /**********************************************************************/
  88. /*                                                                    */
  89. /*    InitInstance(HANDLE, int)                                       */
  90. /*                                                                    */
  91. /**********************************************************************/
  92. BOOL InitInstance(hInstance, nCmdShow)
  93.     HANDLE          hInstance;
  94.     int             nCmdShow;
  95. {
  96. #ifdef USEWAPI
  97.     char szTitle[] =  "ImeApps W API version";
  98. #else
  99.     char szTitle[] =  "ImeApps";
  100. #endif
  101.     hInst = hInstance;
  102.     ptImeUIPos.x = 0;
  103.     ptImeUIPos.y = 0;
  104.     if (!(hWndMain = CreateWindowEx(0L,
  105.     "ImeAppsWClass", (LPSTR)szTitle,
  106.     WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  107.     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  108.     NULL, NULL, hInstance, NULL)))
  109. return FALSE;
  110.     /* display each windows */
  111.     ShowWindow (hWndMain, nCmdShow);
  112.     UpdateWindow (hWndMain);
  113.     return TRUE;
  114. }
  115. /**********************************************************************/
  116. /*                                                                    */
  117. /*    InitIMEUIPosition(HWND)                                         */
  118. /*                                                                    */
  119. /**********************************************************************/
  120. void InitIMEUIPosition(hWnd)
  121. HWND hWnd;
  122. {
  123.     HDC hDC;
  124.     int y;
  125.     SIZE sz;
  126.     HFONT hOldFont;
  127.     HFONT hDefFont;
  128.     int height, defheight;
  129.     hDefFont = GetStockObject(DEFAULT_GUI_FONT);
  130.     hOldFont = NULL;
  131.     y = ORG_Y;
  132.     hDC = GetDC(hWnd);
  133.     if (hFont)
  134.         hOldFont = SelectObject(hDC,hDefFont);
  135.     // Get the height of the default gui font.
  136.     GetTextExtentPoint(hDC,"A",1,&sz);
  137.     defheight = sz.cy + 1;
  138.     // Get the height of the font.
  139.     SelectObject(hDC,hFont);
  140.     GetTextExtentPoint(hDC,"A",1,&sz);
  141.     height = sz.cy + 1;
  142.     SelectObject(hDC,hDefFont);
  143.     y += defheight;
  144.     y += height;
  145.     y += height;
  146.     y += defheight;
  147.     y += height;
  148.     y += height;
  149.     ptImeUIPos.y = y;
  150.     SelectObject(hDC,hOldFont);
  151.     ReleaseDC(hWnd, hDC);
  152.     return;
  153. }
  154. /**********************************************************************/
  155. /*                                                                    */
  156. /*    MainWndProc(HWND, UINT, WPARAM, LPARAM)                         */
  157. /*                                                                    */
  158. /**********************************************************************/
  159. LRESULT CALLBACK MainWndProc(hWnd, message, wParam, lParam)
  160. HWND hWnd;
  161. UINT message;
  162. WPARAM wParam;
  163. LPARAM lParam;
  164. {
  165.     FARPROC lpProc;
  166.     RECT rect;
  167.     HMENU hMenu;
  168.     HIMC hIMC;
  169.     BOOL fOpen;
  170.     switch (message) {
  171. case WM_CREATE:
  172.             // Create Status Window
  173.             CreateTBar(hWnd);
  174.             CreateStatus(hWnd);
  175.             // Create Child Window
  176.             GetClientRect(hWnd, &rect);
  177.             if (!(hWndCompStr = CreateWindowEx(WS_EX_CLIENTEDGE,
  178.             "CompStrWndClass", NULL,
  179.             WS_CHILD | WS_VISIBLE, 
  180.             // 0, 0, rect.right, rect.bottom - nStatusHeight,
  181.             0, 0, 0, 0,
  182.             hWnd, NULL, hInst, NULL)))
  183.         return FALSE;
  184.             InitIMEUIPosition(hWndCompStr);
  185.             if (!(hWndCandList = CreateWindowEx(WS_EX_CLIENTEDGE,
  186.             "CandListWndClass", NULL,
  187.             WS_CHILD | WS_VISIBLE, 
  188.             0, 0, 0, 0,
  189.             hWnd, NULL, hInst, NULL)))
  190.         return FALSE;
  191.             ShowWindow(hWndCompStr, SW_SHOW);
  192.             ShowWindow(hWndCandList, SW_SHOWNA);
  193.             SetStatusItems(hWnd);
  194.             hMenu = GetMenu(hWnd);
  195.             CheckMenuItem(hMenu,IDM_SHOWCAND,
  196.                                 (fShowCand ? MF_CHECKED : MF_UNCHECKED));
  197.     break;
  198. case WM_COMMAND:
  199.     switch(LOWORD(wParam))
  200.     {
  201. case IDM_ABOUT:
  202.     lpProc = MakeProcInstance(AboutDlg, hInst);
  203.     DialogBox(hInst, "ABOUTBOX", hWnd, (DLGPROC)lpProc);
  204.     FreeProcInstance(lpProc);
  205.     break;
  206. case IDM_FONT:
  207.                     {
  208.                     CHOOSEFONT cf;
  209.                     LOGFONT lfT;
  210.                     /* Set all structure fields to zero. */
  211.                     memset(&cf, 0, sizeof(CHOOSEFONT));
  212.                     memcpy(&lfT, &lf, sizeof(LOGFONT));
  213.                     cf.lStructSize = sizeof(CHOOSEFONT);
  214.                     cf.hwndOwner = hWnd;
  215.                     cf.lpLogFont = &lfT;
  216.                     cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
  217.                     cf.nFontType = SCREEN_FONTTYPE;
  218.                     if (ChooseFont(&cf))
  219.                     {
  220.                         if (hFont)
  221.                             DeleteObject(hFont);
  222.                         memcpy(&lf, &lfT, sizeof(LOGFONT));
  223.                         hFont = CreateFontIndirect(&lf);
  224.                         InvalidateRect(hWndCompStr,NULL,TRUE);
  225.                         UpdateWindow(hWndCompStr);
  226.                     }
  227.                     }
  228.                     break;
  229.                 case IDM_SHOWCAND:
  230.                     hMenu = GetMenu(hWnd);
  231.                     fShowCand = !fShowCand;
  232.                     CheckMenuItem(hMenu,IDM_SHOWCAND,
  233.                                 (fShowCand ? MF_CHECKED : MF_UNCHECKED));
  234.                     MoveCompCandWindow(hWnd);
  235.                     UpdateShowCandButton();
  236.                     break;
  237.                 case IDM_OPENSTATUS:
  238.                     hIMC = ImmGetContext(hWndCompStr);
  239.                     fOpen = ImmGetOpenStatus(hIMC);
  240.                     ImmSetOpenStatus(hIMC,!fOpen);
  241.                     UpdateShowOpenStatusButton(!fOpen);
  242.                     ImmReleaseContext(hWndCompStr,hIMC);
  243.                     hMenu = GetMenu(hWnd);
  244.                     CheckMenuItem(hMenu,IDM_OPENSTATUS,
  245.                                 (fOpen ? MF_UNCHECKED : MF_CHECKED));
  246.                     break;
  247.                 case IDM_NATIVEMODE:
  248.                 case IDM_FULLHALF:
  249.                 case IDM_ROMAN:
  250.                 case IDM_CHARCODE:
  251.                 case IDM_HANJA:
  252.                 case IDM_SOFTKBD:
  253.                 case IDM_EUDC:
  254.                 case IDM_SYMBOL:
  255.                     HandleModeCommand(hWnd,wParam,lParam);
  256.                     break;
  257.  
  258.                 case IDM_CONVERT:
  259.                 case IDM_CANCEL:
  260.                 case IDM_REVERT:
  261.                 case IDM_COMPLETE:
  262.                 case IDM_OPENCAND:
  263.                 case IDM_CLOSECAND:
  264.                 case IDM_NEXTCAND:
  265.                 case IDM_PREVCAND:
  266.                     HandleConvertCommand(hWnd,wParam,lParam);
  267.     break;
  268.                 case IDM_NEXTCLAUSE:
  269.                 case IDM_PREVCLAUSE:
  270.                     HandleChangeAttr(hWnd,(LOWORD(wParam) == IDM_NEXTCLAUSE));
  271.     break;
  272.                 default:
  273.     break;
  274.     }
  275.     break;
  276.         case WM_SETFOCUS:
  277.             SetFocus(hWndCompStr);
  278.     break;
  279.         case WM_NOTIFY:
  280.             SetTooltipText(hWnd, lParam);
  281.             break;
  282.         case WM_SIZE:
  283.             SendMessage(hWndStatus,message,wParam,lParam);
  284.             SendMessage(hWndToolBar,message,wParam,lParam);
  285.             if (wParam != SIZE_MINIMIZED)
  286.                 MoveCompCandWindow(hWnd);
  287.     break;
  288.         case WM_IME_NOTIFY:
  289.             switch (wParam)
  290.             {
  291.                 case IMN_OPENSTATUSWINDOW:
  292.                 case IMN_CLOSESTATUSWINDOW:
  293.                     break;
  294.                 case IMN_SETCONVERSIONMODE:
  295.             return (DefWindowProc(hWnd, message, wParam, lParam));
  296.             }
  297.     break;
  298. case WM_DESTROY:
  299.             if (hFont)
  300.                 DeleteObject(hFont);
  301.     PostQuitMessage(0);
  302.     break;
  303. default:
  304.     return (DefWindowProc(hWnd, message, wParam, lParam));
  305.     }
  306.     return 0L;
  307. }
  308. /**********************************************************************/
  309. /*                                                                    */
  310. /*    CompStrWndProc(HWND, UINT, WPARAM, LPARAM)                      */
  311. /*                                                                    */
  312. /**********************************************************************/
  313. LRESULT CALLBACK CompStrWndProc(hWnd, message, wParam, lParam)
  314. HWND hWnd;
  315. UINT message;
  316. WPARAM wParam;
  317. LPARAM lParam;
  318. {
  319.     HIMC hIMC;
  320.     HIMC hOldIMC;
  321.     switch (message) {
  322. case WM_CREATE:
  323.             hIMC = ImmCreateContext();
  324.             hOldIMC = ImmAssociateContext(hWnd,hIMC);
  325.             SetWindowLong(hWnd, 0, (LONG)hOldIMC);
  326.             fdwProperty = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
  327.     break;
  328.         case WM_CHAR:
  329.             HandleChar(hWnd,wParam,lParam);
  330.     break;
  331.         case WM_LBUTTONUP:
  332.         case WM_RBUTTONUP:
  333.             if (hIMC = ImmGetContext(hWnd))
  334.             {
  335.                 HMENU hMenu;
  336.                 InitMenuItemIDTable();
  337.                 hMenu = CreateImeMenu(hWnd, hIMC, NULL,(message == WM_RBUTTONUP));
  338.                 if (hMenu)
  339.                 {
  340.                     DWORD dwItemData;
  341.                     DWORD dwPos = (DWORD)GetMessagePos();
  342.                     int nCmd;
  343.                   
  344.                     nCmd = TrackPopupMenuEx(hMenu,
  345.                            TPM_RETURNCMD | TPM_NONOTIFY | 
  346.                            TPM_LEFTBUTTON | TPM_LEFTALIGN | TPM_TOPALIGN, 
  347.                            LOWORD(dwPos), HIWORD(dwPos), 
  348.                            hWnd, NULL);
  349.                     if (nCmd)
  350.                     {
  351.                         nCmd -= IDM_STARTIMEMENU;
  352.                         dwItemData = FindItemData(nCmd);
  353.                         ImmNotifyIME(hIMC, NI_IMEMENUSELECTED, nCmd, dwItemData);
  354.                     }
  355.                 }
  356.                 EndMenuItemIDTable();
  357.                 DestroyMenu(hMenu);
  358.             }
  359.     break;
  360.         case WM_IME_SETCONTEXT:
  361.             if (fShowCand)
  362.                 lParam &= ~ISC_SHOWUICANDIDATEWINDOW;
  363.             if (fdwProperty & IME_PROP_SPECIAL_UI)
  364.             {
  365.             }
  366.             else if (fdwProperty & IME_PROP_AT_CARET)
  367.             {
  368.                 lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
  369.             }
  370.             else
  371.             {
  372.             }
  373.     return (DefWindowProc(hWnd, message, wParam, lParam));
  374.         case WM_IME_STARTCOMPOSITION:
  375.             // Normally, we should not call into HandleStartComposition
  376.             // for IME_PROP_SPECIAL_UI and not IME_PROP_AT_CARET IMEs
  377.             // we should pass this message to DefWindowProc directly for
  378.             // this kind of IMEs
  379.             HandleStartComposition(hWnd,wParam,lParam);
  380.             // pass this message to DefWindowProc for IME_PROP_SPECIAL_UI
  381.             // and not IME_PROP_AT_CARET IMEs
  382.             if (fdwProperty & IME_PROP_SPECIAL_UI)
  383.             {
  384.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  385.             }
  386.             else if (fdwProperty & IME_PROP_AT_CARET)
  387.             {
  388.             }
  389.             else
  390.             {
  391.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  392.             }
  393.     break;
  394.         case WM_IME_ENDCOMPOSITION:
  395.             // Normally, we should not call into HandleEndComposition
  396.             // for IME_PROP_SPECIAL_UI and not IME_PROP_AT_CARET IMEs
  397.             // we should pass this message to DefWindowProc directly for
  398.             // this kind of IMEs
  399.             HandleEndComposition(hWnd,wParam,lParam);
  400.             // pass this message to DefWindowProc for IME_PROP_SPECIAL_UI
  401.             // and not IME_PROP_AT_CARET IMEs
  402.             if (fdwProperty & IME_PROP_SPECIAL_UI)
  403.             {
  404.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  405.             }
  406.             else if (fdwProperty & IME_PROP_AT_CARET)
  407.             {
  408.             }
  409.             else
  410.             {
  411.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  412.             }
  413.     break;
  414.         case WM_IME_COMPOSITION:
  415.             // Normally, we should not call into HandleComposition
  416.             // for IME_PROP_SPECIAL_UI and not IME_PROP_AT_CARET IMEs
  417.             // we should pass this message to DefWindowProc directly for
  418.             // this kind of IMEs
  419.             HandleComposition(hWnd,wParam,lParam);
  420.             // pass this message to DefWindowProc for IME_PROP_SPECIAL_UI
  421.             // and not IME_PROP_AT_CARET IMEs
  422.             if (fdwProperty & IME_PROP_SPECIAL_UI)
  423.             {
  424.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  425.             }
  426.             else if (fdwProperty & IME_PROP_AT_CARET)
  427.             {
  428.             }
  429.             else
  430.             {
  431.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  432.             }
  433.     break;
  434. case WM_PAINT:
  435.             HandlePaint(hWnd,wParam,lParam);
  436.     break;
  437.         case WM_IME_NOTIFY:
  438.             {
  439.                 LRESULT lRet;
  440.                 // Normally, we should not call into HandleNotify
  441.                 // for IME_PROP_SPECIAL_UI and not IME_PROP_AT_CARET IMEs
  442.                 // we should pass this message to DefWindowProc directly for
  443.                 // this kind of IMEs
  444.                 lRet = HandleNotify(hWnd, message, wParam, lParam);
  445.  
  446.                 // pass this message to DefWindowProc for IME_PROP_SPECIAL_UI
  447.                 // and not IME_PROP_AT_CARET IMEs
  448.                 if (fdwProperty & IME_PROP_SPECIAL_UI)
  449.                 {
  450.                     return (DefWindowProc(hWnd, message, wParam, lParam));
  451.                 }
  452.                 else if (fdwProperty & IME_PROP_AT_CARET)
  453.                 {
  454.                 }
  455.                 else
  456.                 {
  457.                     return (DefWindowProc(hWnd, message, wParam, lParam));
  458.                 }
  459.                 return lRet;
  460.             }
  461.         case WM_DESTROY:
  462.             hOldIMC = (HIMC)GetWindowLong(hWnd, 0);
  463.             hIMC = ImmAssociateContext(hWnd, hOldIMC);
  464.             ImmDestroyContext(hIMC);
  465.     break;
  466.         case WM_INPUTLANGCHANGE:
  467.             fdwProperty = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
  468.             if (hIMC = ImmGetContext(hWnd))
  469.             {
  470.                 CANDIDATEFORM cdf;
  471.                 if (fdwProperty & IME_PROP_AT_CARET)
  472.                 {
  473.                     cdf.dwIndex = 0;
  474.                     cdf.dwStyle = CFS_CANDIDATEPOS;
  475.                     cdf.ptCurrentPos.x = ptImeUIPos.x;
  476.                     cdf.ptCurrentPos.y = ptImeUIPos.y;
  477.                     ImmSetCandidateWindow(hIMC, &cdf);
  478.                 }
  479.                 else
  480.                 {
  481.                     UINT i;
  482.                     // The candidate position should be decided by a near caret
  483.                     // IME. There are 4 candidate form in the input context
  484.                     for (i = 0; i < 4; i++)
  485.                     {
  486.                         if (!ImmGetCandidateWindow(hIMC, i, &cdf))
  487.                         {
  488.                             continue;
  489.                         }
  490.                         if (cdf.dwStyle == CFS_DEFAULT)
  491.                         {
  492.                             continue;
  493.                         }
  494.                         cdf.dwStyle = CFS_DEFAULT;
  495.                         ImmSetCandidateWindow(hIMC, &cdf);
  496.                     }
  497.                 }
  498.                 ImmReleaseContext(hWnd, hIMC);
  499.             }
  500.     return (DefWindowProc(hWnd, message, wParam, lParam));
  501. default:
  502.     return (DefWindowProc(hWnd, message, wParam, lParam));
  503.     }
  504.     return 0L;
  505. }
  506. /**********************************************************************/
  507. /*                                                                    */
  508. /*    CandListWndProc(HWND, UINT, WPARAM, LPARAM)                     */
  509. /*                                                                    */
  510. /**********************************************************************/
  511. LRESULT CALLBACK CandListWndProc(hWnd, message, wParam, lParam)
  512. HWND hWnd;
  513. UINT message;
  514. WPARAM wParam;
  515. LPARAM lParam;
  516. {
  517.     switch (message) {
  518. case WM_PAINT:
  519.             HandleCandPaint(hWnd,wParam,lParam);
  520.     break;
  521.         case WM_SETFOCUS:
  522.             SetFocus(hWndCompStr);
  523.     break;
  524. default:
  525.     return (DefWindowProc(hWnd, message, wParam, lParam));
  526.     }
  527.     return 0L;
  528. }
  529. /**********************************************************************/
  530. /*                                                                    */
  531. /*    About(HWND, UINT, WPARAM, LPARAM)                               */
  532. /*                                                                    */
  533. /**********************************************************************/
  534. LRESULT CALLBACK AboutDlg(hDlg, message, wParam, lParam)
  535. HWND hDlg;
  536. unsigned message;
  537. WPARAM wParam;
  538. LPARAM lParam;
  539. {
  540.     switch (message) {
  541. case WM_INITDIALOG:
  542.     return (TRUE);
  543. case WM_COMMAND:
  544.     if (wParam == IDOK
  545.                 || wParam == IDCANCEL) {
  546. EndDialog(hDlg, TRUE);
  547. return (TRUE);
  548.     }
  549.     break;
  550.     }
  551.     return (FALSE);
  552. }