C++ 在编辑控件中使用上标和下标
我正在使用Win32 api制作一个程序,我有一个编辑控件供用户输入。我创建按钮的想法是,用户可以单击这些按钮来使用超级/下标,并在完成后停用它们,但我正在努力实际实现它们。我想我可以创建一个更小的新HFONT,但是当我发送WM_SETFONT消息时,它会更改所有文本,因此所有内容都很小,然后当我禁用它时,所有内容都会再次变为全尺寸C++ 在编辑控件中使用上标和下标,c++,winapi,fonts,C++,Winapi,Fonts,我正在使用Win32 api制作一个程序,我有一个编辑控件供用户输入。我创建按钮的想法是,用户可以单击这些按钮来使用超级/下标,并在完成后停用它们,但我正在努力实际实现它们。我想我可以创建一个更小的新HFONT,但是当我发送WM_SETFONT消息时,它会更改所有文本,因此所有内容都很小,然后当我禁用它时,所有内容都会再次变为全尺寸 如何更改下一个键入字符的字体,而不是控件中的所有文本?您不能。编辑控件的所有文本都只有一种字体。它不允许您对文本的某些部分应用格式。要么全力以赴,要么一事无成 如果
如何更改下一个键入字符的字体,而不是控件中的所有文本?您不能。编辑控件的所有文本都只有一种字体。它不允许您对文本的某些部分应用格式。要么全力以赴,要么一事无成
如果需要格式化,则需要使用更复杂的控件,如控件。正如@IInspectable所说,使用是一个不错的选择 此外,如果需要实现上标和下标函数,可以使用来模拟键盘按键 SendInput:合成击键、鼠标移动和按钮单击 例如: Superscript=>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));
}
//Test\u RichEdit.cpp:定义应用程序的入口点。
//
#包括“stdafx.h”
#包括“Test_RichEdit.h”
#包括
#定义最大加载字符串100
//全局变量:
HINSTANCE hInst;//当前实例
WCHAR szTitle[MAX_LOADSTRING];//标题栏文本
WCHAR szWindowClass[最大加载字符串];//主窗口类名称
HWND上标掼式;
HWND下标_样式;
HWND-hwndeit;
HWND去激活;
//转发此代码模块中包含的函数声明:
ATOM MyRegisterClass(HINSTANCE HINSTANCE);
BOOL InitInstance(HINSTANCE,int);
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
INT_PTR回调关于(HWND、UINT、WPARAM、LPARAM);
HWND CreateRichEdit(HWND hwndOwner,//对话框句柄。
int x,int y,//位置。
整数宽度,整数高度,//尺寸。
HINSTANCE hinst);//应用程序或DLL实例。
无效上标_style();
空下标_style();
国际货币基金组织,
_在当前情况下,
_在LPWSTR lpCmdLine中,
_In_uuint(nCmdShow)
{
未引用的_参数(HPPreInstance);
未引用的_参数(lpCmdLine);
//TODO:将代码放在这里。
//初始化全局字符串
LoadStringW(hInstance、IDS\U APP\U TITLE、szTitle、MAX\U LOADSTRING);
LoadStringW(hInstance、IDC_TESTRICHEDIT、szWindowClass、MAX_LOADSTRING);
MyRegisterClass(hInstance);
//执行应用程序初始化:
如果(!InitInstance(hInstance,nCmdShow))
{
返回FALSE;
}
HACCEL hAccelTable=LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_TESTRICHEDIT));
味精;
//主消息循环:
while(GetMessage(&msg,nullptr,0,0))
{
if(!TranslateAccelerator(msg.hwnd、hAccelTable和msg))
{
翻译信息(&msg);
发送消息(&msg);
}
}
返回(int)msg.wParam;
}
//
//函数:MyRegisterClass()
//
//目的:注册窗口类。
//
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_箭头);
wcex.hbrBackground=(HBRUSH)(彩色窗口+1);
wcex.lpszMenuName=MAKEINTRESOURCEW(IDC_TESTRICHEDIT);
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));
返回寄存器CLASSEXW(&wcex);
}
//
//函数:InitInstance(HINSTANCE,int)
//
//用途:保存实例句柄并创建主窗口
//
//评论:
//
//在这个函数中,我们将实例句柄保存在一个全局变量中,然后
//创建并显示主程序窗口。
//
BOOL InitInstance(HINSTANCE HINSTANCE,int nCmdShow)
{
hInst=hInstance;//将实例句柄存储在全局变量中
HWND HWND=CreateWindowW(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,
CW_usefault,0,CW_usefault,0,nullptr,nullptr,hInstance,nullptr);
CreateRichEdit(hWnd,50,50200200,hInst);
上标_style=CreateWindowW(L“按钮”,
L“上标”,
WS|u CHILD | WS|u VISIBLE | BS|u def按钮| WS|u CLIPSIBLINGS | WS|u TABSTOP,
400, 60, 80, 30,
hWnd,(HMENU)1,空,空);
下标样式=CreateWindowW(L“按钮”,
L“下标”,
WS|u CHILD | WS|u VISIBLE | BS|u def按钮| WS|u CLIPSIBLINGS | WS|u TABSTOP,
400, 130, 80, 30,
hWnd,(HMENU)2,空,空);
停用=CreateWindowW(L“按钮”,
L“停用”,
WS|u CHILD | WS|u VISIBLE | BS|u def按钮| WS|u CLIPSIBLINGS | WS|u TABSTOP,
400, 200, 80, 30,
hWnd,(HMENU)3,空,空);
如果(!hWnd)
{
返回FALSE;
}
显示窗口(hWnd、nCmdShow);
更新窗口(hWnd);
返回TRUE;
}
//
//功能:WndProc(HWND、UINT、WPARAM、LPARAM)
//
//用途:处理主窗口的消息。
//
//WM_命令-处理应用程序菜单
//WM_绘制-绘制主窗口
//WM_DESTROY-发布退出消息并返回
//
//
LRESULT回调WndProc(HWND HWND,UINT消息,WPARAM WPARAM,LPARAM LPARAM)
{
静态布尔标志_sup=false;
静态布尔标志_sub=false;
开关(信息)
{
case WM_命令:
{
int wmId=低ORD(wParam);
//解析
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);
}