如果您能告诉我如何在下面的代码中创建
LSpaceA()
和
LSpaceB()
函数来更改行距,我将不胜感激。还有,需要
LoadLibrary("riched20.dll")
线吗?这是从 ChatGPT 上的一个问题中获得的稍微修改过的代码。 在 DevC++ 中编译。
在Visual Studio中编译时,出现
strcat
和
sprintf
相关的各种错误,无奈只好在DevC++中测试。本来我是在寻找一种不使用RichEdit的情况下改变行距的方法,但是找不到不使用RichEdit的方法,所以打算使用RichEdit,但是网上很难找到样本来测试是否可以RichEdit 工作正常。
即使您通过更改下面代码中的
bLineSpacingRule
和
LSpaceA()
函数中的
LSpaceB()
等变量值进行测试,行距也完全没有变化。
#include <stdio.h>
#include <windows.h>
#include <richedit.h>
#define ID_EDIT 1
#define ID_BUTTON 2
HWND hEdit, hButton;
HINSTANCE hInstance;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
char sbuf[900];
void MkBuf()
{
char sta[300];
int ii;
strcpy(sbuf,"");
for(ii=0; ii<6; ii++){
sprintf(sta,"%dth line Test WinApi Test WinApi Test WinApi Test WinApi Test WinApi%c%c", 1+ii,0xD,0xA);
strcat(sbuf,sta);
}
}
void LSpaceA()
{
PARAFORMAT2 pf2;
pf2.cbSize = sizeof(PARAFORMAT2);
//SendMessage(hEdit, EM_GETPARAFORMAT, 0, (LPARAM)&pf2);
pf2.dwMask = PFM_LINESPACING;
//pf2.dwMask = PFM_SPACEBEFORE;
//pf2.bLineSpacingRule = 4;
pf2.dyLineSpacing = 400; // (!)
SendMessage(hEdit, EM_SETPARAFORMAT, 0, (LPARAM)&pf2);
}
void LSpaceB(HWND hwnd, int space)
{
PARAFORMAT2 pf;
pf.cbSize = sizeof(PARAFORMAT2);
pf.dwMask = PFM_LINESPACING;
pf.dyLineSpacing = space;
pf.bLineSpacingRule = 5; // (!)
SendMessage(hwnd, EM_SETPARAFORMAT, 0, (LPARAM)&pf);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
LoadLibrary("riched20.dll"); // for using rich edit
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "EditorWindowClass";
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(0, "EditorWindowClass", "Text Editor", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
hEdit = CreateWindowEx(0, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE,
0, 0, 800, 500, hwnd, (HMENU)ID_EDIT, hInstance, NULL);
hButton = CreateWindowEx(0, "BUTTON", "Change Line Spacing", WS_CHILD | WS_VISIBLE,
10, 510, 150, 30, hwnd, (HMENU)ID_BUTTON, hInstance, NULL);
MkBuf();
SetWindowText(hEdit, sbuf);
LSpaceA();
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_COMMAND:
if (LOWORD(wParam) == ID_BUTTON) {
LSpaceB(hEdit, 20);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
EDIT
控件,而不是
RICHEDIT
控件。这就是为什么你的功能不起作用,因为
EDIT
不支持
EM_SETPARAFORMAT