【问题标题】:Using superscripts and subscripts in edit controls在编辑控件中使用上标和下标
【发布时间】:2020-03-19 22:39:30
【问题描述】:

我正在使用 Win32 api 制作一个程序,并且我有一个编辑控件供用户输入。我创建了按钮,其想法是用户可以单击这些按钮来使用上标/下标,并在何时停用它们完成了,但我正在努力实际实施它们。我想我可以创建一个更小的新 HFONT,但是当我发送 WM_SETFONT 消息时,它会更改所有文本,所以一切都很小,然后当我禁用它时,一切又是全尺寸。

如何更改下一个键入的字符的字体,而不是控件中的所有文本?

【问题讨论】:

    标签: c++ winapi fonts


    【解决方案1】:

    你不能。 EDIT 控件的所有文本都使用一种字体。它不允许您将格式应用于文本部分。要么全有,要么全无。

    如果要格式化,则需要使用更复杂的控件,例如 Rich Edit 控件。

    【讨论】:

    • 谢谢!我已经想出了如何更改为丰富的编辑控件,但我仍在努力使上标/下标真正起作用。有什么功能可以帮助解决这个问题吗?
    • 如果您有新问题,请点击 按钮。
    【解决方案2】:

    正如@IInspectable 所说,使用RichEdit 是一个不错的选择。

    另外,如果需要实现上标和下标功能,可以使用SendInput模拟键盘击键。

    SendInput:合成击键、鼠标移动和按钮点击。

    如:

    上标 => Ctrl + Shift + '='

    下标 => Ctrl + '='

    模拟效果:

    代码示例:

    // Test_RichEdit.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "Test_RichEdit.h"
    #include <Richedit.h>
    
    #define MAX_LOADSTRING 100
    
    // Global Variables:
    HINSTANCE hInst;                                // current instance
    WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
    WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
    HWND superscript_style;
    HWND subscript_style;
    HWND hwndEdit;
    HWND deactivate;
    
    // Forward declarations of functions included in this code module:
    ATOM                MyRegisterClass(HINSTANCE hInstance);
    BOOL                InitInstance(HINSTANCE, int);
    LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
    HWND CreateRichEdit(HWND hwndOwner,        // Dialog box handle.
        int x, int y,          // Location.
        int width, int height, // Dimensions.
        HINSTANCE hinst);       // Application or DLL instance.
    void Superscript_style();
    void Subscript_style();
    
    int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                         _In_opt_ HINSTANCE hPrevInstance,
                         _In_ LPWSTR    lpCmdLine,
                         _In_ int       nCmdShow)
    {
        UNREFERENCED_PARAMETER(hPrevInstance);
        UNREFERENCED_PARAMETER(lpCmdLine);
    
        // TODO: Place code here.
    
        // Initialize global strings
        LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadStringW(hInstance, IDC_TESTRICHEDIT, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);
    
        // Perform application initialization:
        if (!InitInstance (hInstance, nCmdShow))
        {
            return FALSE;
        }
    
        HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTRICHEDIT));
    
        MSG msg;
    
        // Main message loop:
        while (GetMessage(&msg, nullptr, 0, 0))
        {
            if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
    
        return (int) msg.wParam;
    }
    
    
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        WNDCLASSEXW wcex;
    
        wcex.cbSize = sizeof(WNDCLASSEX);
    
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTRICHEDIT));
        wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_TESTRICHEDIT);
        wcex.lpszClassName  = szWindowClass;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
        return RegisterClassExW(&wcex);
    }
    
    //
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       hInst = hInstance; // Store instance handle in our global variable
    
       HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
    
       CreateRichEdit(hWnd, 50,50,200,200, hInst);
    
       superscript_style =  CreateWindowW(L"BUTTON",
           L"Superscript",
           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS | WS_TABSTOP,
           400, 60, 80, 30,
           hWnd, (HMENU)1, NULL, NULL);
       subscript_style = CreateWindowW(L"BUTTON",
           L"Subscript",
           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS | WS_TABSTOP,
           400, 130, 80, 30,
           hWnd, (HMENU)2, NULL, NULL);
       deactivate = CreateWindowW(L"BUTTON",
           L"deactivate",
           WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | WS_CLIPSIBLINGS | WS_TABSTOP,
           400, 200, 80, 30,
           hWnd, (HMENU)3, NULL, NULL);
    
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE: Processes messages for the main window.
    //
    //  WM_COMMAND  - process the application menu
    //  WM_PAINT    - Paint the main window
    //  WM_DESTROY  - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static bool flag_sup = false;
        static bool flag_sub = false;
        switch (message)
        {
        case WM_COMMAND:
            {
                int wmId = LOWORD(wParam);
                // Parse the menu selections:
                switch (wmId)
                {
                case IDM_ABOUT:
                    DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                    break;
                case IDM_EXIT:
                    DestroyWindow(hWnd);
                    break;
                case 1:
                {
                    flag_sup = true;
                    Superscript_style();
                    break;
                }
                case 2:
                {
                    flag_sub = true;
                    Subscript_style();
                    break;
                }
                case 3:
                {
                    if (flag_sup == true)
                    {
                        flag_sup = false;
                        Superscript_style();
                    }
                    else if (flag_sub == true)
                    {
                        flag_sub = false;
                        Subscript_style();
                    }
                    else
                    {
                        SetFocus(hwndEdit);
                    }
                    break;
                }
    
                default:
                    return DefWindowProc(hWnd, message, wParam, lParam);
                }
            }
            break;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hWnd, &ps);
                // TODO: Add any drawing code that uses hdc here...
                EndPaint(hWnd, &ps);
            }
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        return 0;
    }
    
    // Message handler for about box.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        UNREFERENCED_PARAMETER(lParam);
        switch (message)
        {
        case WM_INITDIALOG:
            return (INT_PTR)TRUE;
    
        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
            {
                EndDialog(hDlg, LOWORD(wParam));
                return (INT_PTR)TRUE;
            }
            break;
        }
        return (INT_PTR)FALSE;
    }
    
    HWND CreateRichEdit(HWND hwndOwner,        // Dialog box handle.
        int x, int y,          // Location.
        int width, int height, // Dimensions.
        HINSTANCE hinst)       // Application or DLL instance.
    {
        LoadLibrary(TEXT("Msftedit.dll"));
    
        hwndEdit = CreateWindowEx(0, MSFTEDIT_CLASS, TEXT(""),
            ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
            x, y, width, height,
            hwndOwner, NULL, hinst, NULL);
    
        return hwndEdit;
    }
    
    void Superscript_style()
    {
        INPUT input[3];
        memset(input, 0, sizeof(input));
        input[0].type = input[1].type = input[2].type = INPUT_KEYBOARD;
        SetFocus(hwndEdit);
        input[0].ki.wVk = VK_CONTROL;
        input[1].ki.wVk = VK_SHIFT;
        input[2].ki.wVk = VK_OEM_PLUS;
        SendInput(3, input, sizeof(INPUT));
        input[0].ki.dwFlags = input[1].ki.dwFlags = input[2].ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(3, input, sizeof(INPUT));
    }
    
    void Subscript_style()
    {
        INPUT input[2];
        memset(input, 0, sizeof(input));
        input[0].type = input[1].type = INPUT_KEYBOARD;
        SetFocus(hwndEdit);
        input[0].ki.wVk = VK_CONTROL;
        input[1].ki.wVk = VK_OEM_PLUS;
        SendInput(2, input, sizeof(INPUT));
        input[0].ki.dwFlags = input[1].ki.dwFlags = KEYEVENTF_KEYUP;
        SendInput(2, input, sizeof(INPUT));
    }
    

    注意:

    如果您不熟悉SendInput,可以参考this了解更多详情。

    【讨论】:

    • @Shawrie777 你好,这个答案对你有用吗?
    【解决方案3】:

    嘿试试这个来设置格式:

    void Subscript(HWND hWindow) {
        CHARFORMAT2 cf;
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFE_SUBSCRIPT;
        cf.dwEffects = CFE_SUBSCRIPT;
        SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
    }
    
    void Superscript(HWND hWindow) {
        CHARFORMAT2 cf;
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_SUPERSCRIPT;
        cf.dwEffects = CFM_SUPERSCRIPT;
        SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
    }
    

    ...这是重置它:

    void ResetSuperSubScript(HWND hWindow) {
        CHARFORMAT2 cf;
        cf.cbSize = sizeof(cf);
        cf.dwMask = CFM_SUPERSCRIPT | CFM_SUBSCRIPT;
        cf.dwEffects = 0;
        SendMessage(hWindow, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多