我發現這個問題(如其他一些),但是這是一個到目前爲止,我已經實現了:增強十字光標可能嗎?
Crosshair cursor with additional lines in C#
因爲它規定,我可以直接用股票光標「跨界」 IDE。這是一種非常好的做事方式。在上面的答案中指定的答案在給定的寬度/高度處在屏幕上繪製了一個十字。例如:
private Cursor crossCursor(Pen pen, Brush brush, int x, int y)
{
var pic = new Bitmap(x, y);
Graphics gr = Graphics.FromImage(pic);
var pathX = new GraphicsPath();
var pathY = new GraphicsPath();
pathX.AddLine(0, y/2, x, y/2);
pathY.AddLine(x/2, 0, x/2, y);
gr.DrawPath(pen, pathX);
gr.DrawPath(pen, pathY);
IntPtr ptr = pic.GetHicon();
var c = new Cursor(ptr);
return c;
}
我的問題是,我想我的十字線,以擴大到的界的可視面積。這裏提供上下文,我有:
//Form
//TableLayoutPanel
//UserControl (fills the TableLayoutPanel visible area)
因此,如何能調整我的光標,線(在CAD pacakages很像)延伸?
感謝。
更新:我已經打過電話,從這裏的方法:
protected override void OnLoad(System.EventArgs e)
{
Cursor = crossCursor(Pens.WhiteSmoke, Brushes.WhiteSmoke, Bounds.Width, Bounds.Height);
}
但它也不行,因爲在時間界限是150返回的150的尺寸這一點是不是TableLayoutPanel
的大小。
更新:我已經adjuted它使用調整大小處理程序,而不是和它改善的事情:
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Cursor = crossCursor(Pens.WhiteSmoke, Brushes.WhiteSmoke, Bounds.Width, Bounds.Height);
}
唯一的問題,現在(和它種是有道理的,我想)是將光標只有當視圖爲中心時才佔用視圖的全部寬度和高度。只要我移動光標不調整的視圖。我總是希望通過鼠標位置的水平/垂直線(不只是最初的十字)。
參見:
十字的需要延伸(較厚的紅線)。當鼠標移動或以另一種方式構建兩條線時,我需要不斷創建光標。該怎麼辦?
我碰到這樣的:
因此,而不是改變光標對象我現在繪製控件的MouseMove處理線:
Region r = new Region();
r.Union(new Rectangle(0, lastY, this.Width, 1));
r.Union(new Rectangle(lastX, 0, 1, this.Height));
this.Invalidate(r);
this.Update();
Graphics g = Graphics.FromHwnd(this.Handle);
g.DrawLine(Pens.White, 0, e.Y, this.Width, e.Y);
g.DrawLine(Pens.White, e.X, 0, e.X, this.Height);
int intDiameter = 20;//the diameter of this circle
g.DrawEllipse(Pens.White, e.X - intDiameter/2, e.Y - intDiameter/2, 20, 20);
//to draw the circle
lastX = e.X;
lastY = e.Y;
它的工作原理,但我得到noticiable屏幕閃爍這樣做。
貌似這正是它應該做的。你應該通過'界限。寬度'爲X和'Bounds.Height'爲Y. – DonBoitnott
@DonBoitnott謝謝,請參閱我的更新問題。 –
你應該掛鉤一個'Resize'事件而不是'Load'事件。這樣,您不僅可以第一次獲得正確的「Bounds」,但每次調整窗體大小時都可以進行更新。 – DonBoitnott