如何使wpf文本框看起来像一个控制台 [英] how to make a wpf textbox look like a console
问题描述
我在wpf中创建一个CUI应用程序。
我使用文本框创建一个命令框。
i按以下方式工作:
用户在命令框中输入命令,当他按下Enter键时,处理命令并从文本框中删除。
即时通讯使用黑色背景和字体系列 - Lucida Console给它一个控制台外观。
i还删除了鼠标光标设置textbox.Cursor = Cursors.None;
i到目前为止已经实现了这个目标。
但问题是插入符号。
它破坏了它的外观。
以及用户可以用方向键移动它。
所以我想隐藏插入符号并且也希望它不是按方向键移动。
没有复杂的答案PLZ我是新的。
抱歉,如果它看起来很愚蠢。
谢谢....
i am making a CUI application in wpf.
im using a textbox to make a command box.
i works the following way:
the user enters a command in the command box and when he presses enter the command is processed and also removed from the text box.
im using black background and font family-"Lucida Console" to give it a console look.
i have also removed the mouse cursor bu setting textbox.Cursor = Cursors.None;
i have achieved this so far.
but the problem is the caret.
it spoils its looks.
and also that user can move it with the direction keys.
so i want to hide the caret and also want that it is not moved by the direction keys.
NO COMPLICATED ANSWERS PLZ cuz im new.
sorry if it seems stupid.
thanks....
推荐答案
请参阅我对该问题的评论。据我所知,只有禁用箭头键(或者其他一些键,例如home,end等)可能会有所帮助。初始化组件后,您可以处理控制台UI的事件PreviewKeyDown
,例如
Please see my comment to the question. As far as I can see, only disabling arrow keys (or maybe some other keys, such as home, end, etc) may make some sense. After the components are initialized, you can handle the eventPreviewKeyDown
of your console UI, such as in
public MyMainWindow() {
InitializeComponent();
myTextBoxConsole.PreviewKeyDown += (sender, eventArgs) => {
eventArgs.Handled =
eventArgs.Key == System.Windows.Input.Key.Left
|| eventArgs.Key == System.Windows.Input.Key.Right
|| eventArgs.Key == System.Windows.Input.Key.Up
|| eventArgs.Key == System.Windows.Input.Key.Down
|| eventArgs.Key == System.Windows.Input.Key.Home
|| eventArgs.Key == System.Windows.Input.Key.End
|| eventArgs.Key == System.Windows.Input.Key.PageUp
|| eventArgs.Key == System.Windows.Input.Key.PageDown;
};
//...
} //MyMainWindow
重要提示:可以取消( eventArgs.Handled
),你必须处理 PreviewKeyDown
,而不是 KeyDown
。此代码示例已经过测试,可以正常运行。
Important: to be able to cancel (eventArgs.Handled
), you have to handle PreviewKeyDown
, not KeyDown
. This code sample is tested, it works.
这篇关于如何使wpf文本框看起来像一个控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!