TextBox光标问题

TBAG 2013-08-26 04:00:15

Textbox光标默认是竖着显示的。要怎么样才能向上面图片一样横着显示?
请高人帮忙下。
...全文
256 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
PaulyJiang 2013-08-28
  • 打赏
  • 举报
回复
实际项目中很少用到这样的吧,无聊玩玩可以 代码很给力额
Gavin_cao 2013-08-26
  • 打赏
  • 举报
回复
如果只是研究技术可以试一下
threenewbee 2013-08-26
  • 打赏
  • 举报
回复
横着竖着是有讲究的。 在DOS和控制台下,因为是等宽字体,所以用横的。 在Windows下,输入的字符宽度不确定,是竖着的。 你偏要按照你的想法乱来,不按照约定,让你的软件显得很业余。
五更琉璃 2013-08-26
  • 打赏
  • 举报
回复
引用 1 楼 jshi123 的回复:

	public partial class Form1 : Form
	{
		[DllImport("user32.dll")]
		static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
		[DllImport("user32.dll")]
		static extern bool ShowCaret(IntPtr hWnd);
		[DllImport("user32.dll")]
		static extern bool DestroyCaret();

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			Func<Control, IEnumerable<Control>> descendants = null;
			descendants = control => control.Controls.Cast<Control>().SelectMany(c => new[] {c}.Union(descendants(c)));

			descendants(this).Where(c => c is TextBox).ToList().ForEach(textbox =>
				{
					textbox.GotFocus += textBox_GotFocus;
					textbox.LostFocus += textbox_LostFocus;
				});
		}

		void textBox_GotFocus(object sender, EventArgs e)
		{
			var textbox = (TextBox)sender;
			int w = (int)textbox.Font.Size, h = textbox.Font.Height;
			var bmp = new Bitmap(w, h);
			using (var g = Graphics.FromImage(bmp))
			{
				g.Clear(Color.Black);
				g.DrawLine(new Pen(Color.White, 2), 0, h, w, h);
			}
			CreateCaret(textbox.Handle, bmp.GetHbitmap(), 0, 0);
			ShowCaret(textbox.Handle);
		}

		void textbox_LostFocus(object sender, EventArgs e)
		{
			DestroyCaret(); 
		}
	}
收藏了
hxm20003 2013-08-26
  • 打赏
  • 举报
回复
默认么有这样的吧,需要重新画。
Justin-Liu 2013-08-26
  • 打赏
  • 举报
回复
默认就是竖着的,为什么横过来…… 楼上的方法是在Textbox获得焦点时重绘光标
jshi123 2013-08-26
  • 打赏
  • 举报
回复

	public partial class Form1 : Form
	{
		[DllImport("user32.dll")]
		static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
		[DllImport("user32.dll")]
		static extern bool ShowCaret(IntPtr hWnd);
		[DllImport("user32.dll")]
		static extern bool DestroyCaret();

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			Func<Control, IEnumerable<Control>> descendants = null;
			descendants = control => control.Controls.Cast<Control>().SelectMany(c => new[] {c}.Union(descendants(c)));

			descendants(this).Where(c => c is TextBox).ToList().ForEach(textbox =>
				{
					textbox.GotFocus += textBox_GotFocus;
					textbox.LostFocus += textbox_LostFocus;
				});
		}

		void textBox_GotFocus(object sender, EventArgs e)
		{
			var textbox = (TextBox)sender;
			int w = (int)textbox.Font.Size, h = textbox.Font.Height;
			var bmp = new Bitmap(w, h);
			using (var g = Graphics.FromImage(bmp))
			{
				g.Clear(Color.Black);
				g.DrawLine(new Pen(Color.White, 2), 0, h, w, h);
			}
			CreateCaret(textbox.Handle, bmp.GetHbitmap(), 0, 0);
			ShowCaret(textbox.Handle);
		}

		void textbox_LostFocus(object sender, EventArgs e)
		{
			DestroyCaret(); 
		}
	}

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧