我正在使用Win32 api编写程序,并且具有供用户键入的编辑控件。我创建了按钮,其想法是用户可以单击这些按钮以使用上标/下标,并在以下情况下将其停用完成,但是我正在努力实现它们。我认为可以创建一个较小的新HFONT,但是当我发送WM_SETFONT消息时,它会更改所有文本,因此所有内容都很小,然后在禁用它时又又变成了全尺寸。
如何更改下一个键入的字符的字体,但不能更改控件中的所有文本?
答案 0 :(得分:1)
您不能。 EDIT控件的所有文本都具有单个字体。它不允许您将格式设置应用于部分文本。是全有还是全无。
如果要格式化,则需要使用更复杂的控件,例如Rich Edit控件。
答案 1 :(得分:0)
正如@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以获得更多详细信息。
答案 2 :(得分:0)
嘿,试试这个来设置格式:
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);
}