1 Replies - 8542 Views - Last Post: 11 October 2007 - 10:37 AM Rate Topic: -----

#1 cygnusX  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 159
  • Joined: 19-May 07

HideCaret() Windows API function doesn't work properly

Posted 11 October 2007 - 08:05 AM

I want to hide the caret in the RichTextBox control and do that with the win api function HideCaret(),but it doesn't work.The cursor is still visible and blinking.Where i should call the function.I tried in the Enter event of the control but doesn't work again.As far as i know i should call the HideCaret() when the RichTextBox gets the focus.Is that right?
Here is the code:

[DllImport("User32.dll")]
static extern Boolean HideCaret(System.IntPtr hWnd);

private void richTextBox1_Enter(object sender, EventArgs e)
{
HideCaret(richTextBox1.Handle);
}



Is This A Good Question/Topic? 0
  • +

Replies To: HideCaret() Windows API function doesn't work properly

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: HideCaret() Windows API function doesn't work properly

Posted 11 October 2007 - 10:37 AM

Try something like this:

[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern bool CreateCaret(IntPtr hwnd, IntPtr hbmp, int width, int height);
	[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
	public static extern bool ShowCaret(IntPtr hwnd);

	private Bitmap mCaretBmp;

	private void TextBox1_GotFocus(object sender, System.EventArgs e)
	{
		mCaretBmp = new Bitmap(5, 5);
		Graphics gr = Graphics.FromImage(mCaretBmp);
		gr.Clear(Color.Black);
		CreateCaret(TextBox1.Handle, mCaretBmp.GetHbitmap, 5, 5);
		ShowCaret(TextBox1.Handle);
		gr.Dispose();
		}


Make sure you have the following Namespaces

using System.Graphics;
using System.Runtime.InteropServices


Was This Post Helpful? 1

Page 1 of 1