如何在Windows应用程序中使用c#添加标尺指南,如MS Paint或Ms Word,具有“click”和“move”两项功能 [英] How to Add Ruler Guideline Using c# in Windows Application like MS Paint Or Ms Word having the both feature 'click' and 'move'
问题描述
我需要使用我在我的Windows应用程序中添加的标尺上的指南。
请提供我可以获得的任何解决方案。
我有用标题来自链接http://www.codeproject.com/Articles/4369/Ruler-Control
谢谢
这可能会让你开始:使用System.Drawing;
使用System.Windows.Forms;
命名空间Jan14_2014_GuideLine
{
public enum GuideOrientation
{
horizontal,
vertical
}
public partial class Guideline:UserControl
{
public准则()
{
InitializeComponent();
}
public GuideOrientation Orientation {get; set;}
public int Thickness {get; set; }
public Color GColor {get; set;}
private bool mDown = false;
private int mX,mY;
公共指南(GuideOrientation gOrientation,int gThick,Color gColor):base()
{
Orientation = gOrientation;
厚度= gThick;
GColor = gColor;
if(Orientation == GuideOrientation.horizontal)
{
this.Height = gThick;
}
其他
{
this.Width = gThick;
}
this.BackColor = gColor;
//注意VS 2013中的必要性,FrameWork 4.5
//在这里连接这些EventHandler!
//问问Microsoft为什么:)
this.MouseDown + = Guideline_MouseDown;
this.MouseUp + = Guideline_MouseUp;
this.MouseMove + = Guideline_MouseMove;
this.Click + = Guideline_Click;
}
void Guideline_Click(对象发送者,System.EventArgs e)
{
//仅用于测试...
MessageBox.Show( GuideLine点击);
}
private void Guideline_MouseDown(object sender,MouseEventArgs e)
{
mDown = true;
mX = e.X;
mY = e.Y;
}
private void Guideline_MouseMove(object sender,MouseEventArgs e)
{
if(mDown)
{
this.Left + = eX - mX;
this.Top + = e.Y - mY;
}
}
private void Guideline_MouseUp(object sender,MouseEventArgs e)
{
mDown = false;
}
}
}如何向表格添加水平和垂直的GuideLine示例:
指南gLineH;
指南gLineV;
private void Form1_Load(object sender,EventArgs e)
{
gLineH = new guideline(GuideOrientation.horizontal,1,Color.Red);
this.Controls.Add(gLineH);
gLineH.Location = new Point(0,100);
gLineH.Width = this.Width;
gLineH.BringToFront();
gLineV =新指南(GuideOrientation.vertical,1,Color.Red);
this.Controls.Add(gLineV);
gLineV.Location = new Point(100,0);
gLineV.Height = this.Height;
gLineV.BringToFront();
}讨论:
1.这些可以通过点击拖动在运行时移动;如果您使宽度或高度为1像素,具体取决于您的系统(显示器,分辨率),您可能会遇到一些难以点击鼠标来启动它们。在这个例子中,指南被设置为表格的宽度或高度。
2.这个的逻辑扩展是使指南重新在运行时大小不会那么难。
3.其他不错的功能是:
a。以增量(缓和)移动,可能被停靠到标尺控件
b。点击时,直观地显示一些被选中的迹象
c。维护一系列可在运行时编辑的指南
d。在指南上放置一个工具提示,显示位置;或者...发烧友......张贴一个标签,跟踪被移动的指南,显示不断变化的坐标。
其他方法:
1.通过油漆事件在表格的表面上绘图......我会避免这种情况。
2.使用Visual Basic的形状绘制工具创建指南:这在C#中并不难做到。
3.你可以,如果你愿意,可以使用Label而不是UserControl。
使用一堆UserControls作为指南是浪费还是昂贵:imho,无
I need to use guideline on my ruler which i have added in my windows Application.
Please give any solution by which i can obtain it.
I have use ruler from link "http://www.codeproject.com/Articles/4369/Ruler-Control
Thanks
This might get you started:using System.Drawing; using System.Windows.Forms; namespace Jan14_2014_GuideLine { public enum GuideOrientation { horizontal, vertical } public partial class Guideline : UserControl { public Guideline() { InitializeComponent(); } public GuideOrientation Orientation { get; set; } public int Thickness { get; set; } public Color GColor { get; set; } private bool mDown = false; private int mX, mY; public Guideline(GuideOrientation gOrientation, int gThick, Color gColor): base() { Orientation = gOrientation; Thickness = gThick; GColor = gColor; if(Orientation == GuideOrientation.horizontal) { this.Height = gThick; } else { this.Width = gThick; } this.BackColor = gColor; // note the necessity in VS 2013, FrameWork 4.5 // to wire-up these EventHandlers here ! // ask Microsoft why :) this.MouseDown += Guideline_MouseDown; this.MouseUp += Guideline_MouseUp; this.MouseMove += Guideline_MouseMove; this.Click += Guideline_Click; } void Guideline_Click(object sender, System.EventArgs e) { // for testing only ... MessageBox.Show("GuideLine Clicked"); } private void Guideline_MouseDown(object sender, MouseEventArgs e) { mDown = true; mX = e.X; mY = e.Y; } private void Guideline_MouseMove(object sender, MouseEventArgs e) { if (mDown) { this.Left += e.X - mX; this.Top += e.Y - mY; } } private void Guideline_MouseUp(object sender, MouseEventArgs e) { mDown = false; } } }Example of how you would add a horizontal, and vertical, GuideLine to a Form:
Guideline gLineH; Guideline gLineV; private void Form1_Load(object sender, EventArgs e) { gLineH = new Guideline(GuideOrientation.horizontal, 1, Color.Red); this.Controls.Add(gLineH); gLineH.Location = new Point(0, 100); gLineH.Width = this.Width; gLineH.BringToFront(); gLineV = new Guideline(GuideOrientation.vertical, 1, Color.Red); this.Controls.Add(gLineV); gLineV.Location = new Point(100, 0); gLineV.Height = this.Height; gLineV.BringToFront(); }Discussion:
1. these are movable at run-time via click-drag; if you make the width, or height, 1 pixel, depending on your system (monitor, resolution) you may have some difficult getting a mouse-hit to start them moving. in this example the guides are set to either the width, or height, of the Form.
2. a logical extension of this would be to make the guides re-sizable at run-time which would not be that hard to do.
3. other nice features to add would be:
a. moving by increments (detente), possibly being "docked" to a ruler control
b. when clicked, visually show some sign of being "selected"
c. maintain a collection of the guides which could be edited at run-time
d. put a tool-tip on the guides that would show location; or ... fancier ... put up a label that "tracks" the guide being moved showing the changing co-ordinates.
Other Approaches:
1. Drawing on the surface of the Form via the Paint Event ... I would avoid this.
2. Using Visual Basic's shape-drawing facilities to create the guides: this is not difficult to do in C#.
3. You could, if you wish, use something like a Label instead of a UserControl.
Is it "wasteful" or "expensive" to use a bunch of UserControls as guides: imho, no.
这篇关于如何在Windows应用程序中使用c#添加标尺指南,如MS Paint或Ms Word,具有“click”和“move”两项功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!