gpt4 book ai didi

c - 如何用C语言实现剪贴板

转载 作者:行者123 更新时间:2023-11-30 18:59:37 25 4
gpt4 key购买 nike

我正在做一个项目,其中我正在 Turbo C (Dos) 中设计一个文本编辑器应用程序。我想在我的应用程序中添加不同的菜单,如文件、编辑、查看等。我已经设计了文件和安全菜单,但我想实现编辑菜单,其中包括撤消、重做、剪切、复制、粘贴等功能,这需要我实现剪贴板。我知道有一种方法可以在 Windows 中使用 Windows 剪贴板来执行此操作,但我不想使用 Windows 提供的剪贴板。我想实现我自己的剪贴板。

请记住,我的应用程序是基于 DOS 的,Windows 剪贴板将不可用。即使有某种方式使用 Windows 剪贴板,这也不是必需的。我想实现我自己的剪贴板。

最佳答案

好吧,让我们假设您的数据结构是这样的:

struct Textview {
char *text;
int startRange;
int endRange;
};

所以,当我们添加剪切功能时:

char clipboard[1024]; // max of 1024 chars in the clipboard.

void cut(struct Textview *textview)
{
// first we copy the text out (assuming you have
int nCpy = textview->endRange - textView->startRange >= 1024 ? 1023 : textview->endRange - textview->startRange;
strncpy(clipboard, textview->text + textview->startRange, nCpy);

// next, we remove that section of the text
memmove(textview->text + textview->startRange, textview->text + textview->endRange, strlen(textview->text + textview->endRange);
}

还有复制功能:

void copy(struct Textview *textview)
{
int nCpy = textview->endRange - textView->startRange >= 1024 ? 1023 : textview->endRange - textview->startRange;
strncpy(clipboard, textview->text + textview->startRange, nCpy);
}

然后是粘贴功能。

void paste(struct Textview *textview)
{
// assuming we have enough space to paste the additional characters in.
char *cpyText = strdup(textview->text); // if strdup isn't available, use malloc + strcpy.
int cpyTextLen = strlen(cpyText);
int clipboardLen = strlen(clipboard);
memcpy(textview->text + textview->startRange, clipboard, clipboardLen);

memcpy(textview->text + textview->startRange + clipboardLen, cpyText + textview->startRange + 1, cpyTextLen) - textView->startRange);

textview->text[textView->startRange + clipboardLen + cpyTextLen + 1] = '\0';

free(cpyText);
}

对于撤消重做,您需要一堆所做的更改。

关于c - 如何用C语言实现剪贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9819750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com