Creating blinking I-Beam
-
Anyone got idea how to create blinking I-Beam cursor like one that has textbox when it receive focus? Can I do it trough unmanaged code or I need to implement it with timers/overriding OnPaint event? Thanks, Ivan.
Hi .... I hope this Example will help you ... I used 2 Files - mycaret.cs (Class) - caretsample.cs ( Example How to use it ) Optional File : mycaret.cs
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Drawing; public class MyCaret { [DllImport("user32.dll")] public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int cy); [DllImport("user32.dll")] public static extern int DestroyCaret(); [DllImport("user32.dll")] public static extern int SetCaretPos(int x, int y); [DllImport("user32.dll")] public static extern int ShowCaret(IntPtr hwnd); [DllImport("user32.dll")] public static extern int HideCaret(IntPtr hwnd); Control ctrl; Size size; Point pos; bool bVisible; public MyCaret(Control ctrl) { this.ctrl = ctrl; Position = Point.Empty; Size = new Size(1, ctrl.Font.Height); Control.GotFocus += new EventHandler(OnGotFocus); Control.LostFocus += new EventHandler(OnLostFocus); if (ctrl.Focused) OnGotFocus(ctrl, new EventArgs()); } public Control Control { get { return ctrl; } } public Size Size { get { return size; } set { size = value; } } public Point Position { get { return pos; } set { pos = value; SetCaretPos(pos.X, pos.Y); } } public bool Visible { get { return bVisible; } set { bVisible = value; if (bVisible) ShowCaret(Control.Handle); else HideCaret(Control.Handle); } } public void Dispose() { if (ctrl.Focused) OnLostFocus(ctrl, new EventArgs()); Control.GotFocus -= new EventHandler(OnGotFocus); Control.LostFocus -= new EventHandler(OnLostFocus); } private void OnGotFocus(object sender, EventArgs e) { CreateCaret(Control.Handle, IntPtr.Zero, Size.Width, Size.Height); SetCaretPos(Position.X, Position.Y); Visible = true; } private void OnLostFocus(object sender, EventArgs e) { Visible = false; DestroyCaret(); } }
caretsample.csusing System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; public class MyForm : Form { private Container components = null; MyCaret caret; string[] lines; Point cur; Point org; bool bSel; public MyFor
-
Hi .... I hope this Example will help you ... I used 2 Files - mycaret.cs (Class) - caretsample.cs ( Example How to use it ) Optional File : mycaret.cs
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Drawing; public class MyCaret { [DllImport("user32.dll")] public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int cy); [DllImport("user32.dll")] public static extern int DestroyCaret(); [DllImport("user32.dll")] public static extern int SetCaretPos(int x, int y); [DllImport("user32.dll")] public static extern int ShowCaret(IntPtr hwnd); [DllImport("user32.dll")] public static extern int HideCaret(IntPtr hwnd); Control ctrl; Size size; Point pos; bool bVisible; public MyCaret(Control ctrl) { this.ctrl = ctrl; Position = Point.Empty; Size = new Size(1, ctrl.Font.Height); Control.GotFocus += new EventHandler(OnGotFocus); Control.LostFocus += new EventHandler(OnLostFocus); if (ctrl.Focused) OnGotFocus(ctrl, new EventArgs()); } public Control Control { get { return ctrl; } } public Size Size { get { return size; } set { size = value; } } public Point Position { get { return pos; } set { pos = value; SetCaretPos(pos.X, pos.Y); } } public bool Visible { get { return bVisible; } set { bVisible = value; if (bVisible) ShowCaret(Control.Handle); else HideCaret(Control.Handle); } } public void Dispose() { if (ctrl.Focused) OnLostFocus(ctrl, new EventArgs()); Control.GotFocus -= new EventHandler(OnGotFocus); Control.LostFocus -= new EventHandler(OnLostFocus); } private void OnGotFocus(object sender, EventArgs e) { CreateCaret(Control.Handle, IntPtr.Zero, Size.Width, Size.Height); SetCaretPos(Position.X, Position.Y); Visible = true; } private void OnLostFocus(object sender, EventArgs e) { Visible = false; DestroyCaret(); } }
caretsample.csusing System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; public class MyForm : Form { private Container components = null; MyCaret caret; string[] lines; Point cur; Point org; bool bSel; public MyFor
-
Hi .... I hope this Example will help you ... I used 2 Files - mycaret.cs (Class) - caretsample.cs ( Example How to use it ) Optional File : mycaret.cs
using System; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Drawing; public class MyCaret { [DllImport("user32.dll")] public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int cy); [DllImport("user32.dll")] public static extern int DestroyCaret(); [DllImport("user32.dll")] public static extern int SetCaretPos(int x, int y); [DllImport("user32.dll")] public static extern int ShowCaret(IntPtr hwnd); [DllImport("user32.dll")] public static extern int HideCaret(IntPtr hwnd); Control ctrl; Size size; Point pos; bool bVisible; public MyCaret(Control ctrl) { this.ctrl = ctrl; Position = Point.Empty; Size = new Size(1, ctrl.Font.Height); Control.GotFocus += new EventHandler(OnGotFocus); Control.LostFocus += new EventHandler(OnLostFocus); if (ctrl.Focused) OnGotFocus(ctrl, new EventArgs()); } public Control Control { get { return ctrl; } } public Size Size { get { return size; } set { size = value; } } public Point Position { get { return pos; } set { pos = value; SetCaretPos(pos.X, pos.Y); } } public bool Visible { get { return bVisible; } set { bVisible = value; if (bVisible) ShowCaret(Control.Handle); else HideCaret(Control.Handle); } } public void Dispose() { if (ctrl.Focused) OnLostFocus(ctrl, new EventArgs()); Control.GotFocus -= new EventHandler(OnGotFocus); Control.LostFocus -= new EventHandler(OnLostFocus); } private void OnGotFocus(object sender, EventArgs e) { CreateCaret(Control.Handle, IntPtr.Zero, Size.Width, Size.Height); SetCaretPos(Position.X, Position.Y); Visible = true; } private void OnLostFocus(object sender, EventArgs e) { Visible = false; DestroyCaret(); } }
caretsample.csusing System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; using System.Runtime.InteropServices; public class MyForm : Form { private Container components = null; MyCaret caret; string[] lines; Point cur; Point org; bool bSel; public MyFor
I see this same #$%# in lotsa managed code (see SharpDevelop too)! Why??? A managed caret is just as easy to make...
**
xacc.ide-0.2.0.57 - now with C# 2.0 parser and seamless VS2005 solution support!
**