winform中获得鼠标点击点的光标位置
在C操作中如何查询光标位置
GetCursorPos(&myPoint);
printf("%d/%d\\", myPoint.x, myPoint.y);
SetTagWord("mouse_x",myPoint.x);
SetTagWord("mouse_y",myPoint.y);
在WinCC应用程序窗口中用语句‘printf("%d/%d\\", myPoint.x, myPoint.y)’触发鼠标指针光标的打印输出。而且,两个无符号的16-位值类型的内部变量(‘mouse_x’和‘mouse_yd’)已经被声明。
该函数预期得到point类型的一ห้องสมุดไป่ตู้结构的地址在该地址中保存有光标坐标
在C操作中如何查询光标位置
在C操作中如何查询光标位置?
说明:
用函数GetCursorPos(LPPOINT lpPoint)可以确定光标的当前位置。该函数预期得到POINT类型的一个结构的地址,在该地址中保存有光标坐标。下面例子显示了应用GetCursorPos函数的一种可能的方法。例如单击鼠标后可以定位该操作。
C#winform自动触发鼠标、键盘事件
C#winform⾃动触发⿏标、键盘事件要在C#程序中触发⿏标、键盘事件必须要调⽤windows函数。
⼀、⿏标事件的触发1.引⽤windows函数mouse_event/// <summary>/// ⿏标事件/// </summary>/// <param name="flags">事件类型</param>/// <param name="dx">x坐标值(0~65535)</param>/// <param name="dy">y坐标值(0~65535)</param>/// <param name="data">滚动值(120⼀个单位)</param>/// <param name="extraInfo">不⽀持</param>[DllImport("user32.dll")]static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);MouseEventFlag表⽰⿏标事件的类型,可取多个枚举值组合。
注意dx,dy参数,必须是绝对坐标(0,0)~(65535,65535)中的⼀点。
/// <summary>/// ⿏标操作标志位集合/// </summary>[Flags]enum MouseEventFlag : uint{/// <summary>/// ⿏标移动事件/// </summary>Move = 0x0001,/// <summary>/// ⿏标左键按下事件/// </summary>LeftDown = 0x0002,LeftUp = 0x0004,RightDown = 0x0008,RightUp = 0x0010,MiddleDown = 0x0020,MiddleUp = 0x0040,XDown = 0x0080,XUp = 0x0100,Wheel = 0x0800,VirtualDesk = 0x4000,/// <summary>/// 设置⿏标坐标为绝对位置(dx,dy),否则为距离最后⼀次事件触发的相对位置/// </summary>Absolute = 0x8000}2.调⽤mouse_event函数,触发⿏标事件/// <summary>/// 触发⿏标事件/// </summary>/// <param name="x"></param>/// <param name="y"></param>private static void DoMouseClick(int x, int y){int dx = (int)((double)x / Screen.PrimaryScreen.Bounds.Width * 65535); //屏幕分辨率映射到0~65535(0xffff,即16位)之间int dy = (int)((double)y / Screen.PrimaryScreen.Bounds.Height * 0xffff); //转换为double类型运算,否则值为0、1mouse_event(MouseEventFlag.Move | MouseEventFlag.LeftDown | MouseEventFlag.LeftUp | MouseEventFlag.Absolute, dx, dy, 0, new UIntPtr(0)); //点击 }⼆、键盘事件的触发1.引⽤windows函数keybd_event/// <summary>/// 键盘事件/// </summary>/// <param name="bVk"> virtual-key code</param>/// <param name="bScan">hardware scan code</param>/// <param name="dwFlags"> flags specifying various function options</param>/// <param name="dwExtraInfo"> additional data associated with keystroke</param>[DllImport("user32.dll")]public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);bvk为键值,例如回车13,bScan设置为0,dwFlags设置0表⽰按下,2表⽰抬起;dwExtraInfo也设置为0即可。
C#WinForm控件的拖动和缩放的实现
C#WinForm控件的拖动和缩放的实现C# WinForm控件的拖动和缩放是个很有⽤的功能。
实现起来其实很简单的,主要是设计控件的MouseDown、MouseLeave、MouseMove 事件下⾯的⼏个步骤将逐步实现C# WinForm控件的拖动和缩放的功能。
1、定义⼀个枚举类型,描述光标状态1. private enum EnumMousePointPosition2. {3. MouseSizeNone = 0, //'⽆4. MouseSizeRight = 1, //'拉伸右边框5. MouseSizeLeft = 2, //'拉伸左边框6. MouseSizeBottom = 3, //'拉伸下边框7. MouseSizeTop = 4, //'拉伸上边框8. MouseSizeTopLeft = 5, //'拉伸左上⾓9. MouseSizeTopRight = 6, //'拉伸右上⾓10. MouseSizeBottomLeft = 7, //'拉伸左下⾓11. MouseSizeBottomRight= 8, //'拉伸右下⾓12. MouseDrag = 9 // '⿏标拖动13. }2、定义⼏个变量1. const int Band = 5;2. const int MinWidth=10;3. const int MinHeight=10;4. private EnumMousePointPosition m_MousePointPosition;5. private Point p,p1;3、定义⾃⼰的MyMouseDown事件1. private void MyMouseDown(object sender,System.Windows.Forms.MouseEventArgs e)2. {3. p.X=e.X;4. p.Y=e.Y;5. p1.X=e.X;6. p1.Y=e.Y;7. }4、定义⾃⼰的MyMouseLeave事件1. private void MyMouseLeave(object sender, System.EventArgs e)2. {3. m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;4. this.Cursor=Cursors.Arrow;5. }5、设计⼀个函数,确定光标在控件不同位置的样式1. private EnumMousePointPosition MousePointPosition(Size size,System.Windows.Forms.MouseEventArgs e)2. {3.4. if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))5. {6. if (e.X < Band)7. {8. if (e.Y < Band) {return EnumMousePointPosition.MouseSizeTopLeft;}9. else10. {11. if (e.Y > -1 * Band + size.Height)12. {return EnumMousePointPosition.MouseSizeBottomLeft;}13. else14. {return EnumMousePointPosition.MouseSizeLeft;}15. }16. }17. else18. {19. if (e.X > -1 * Band + size.Width)20. {21. if (e.Y < Band)22. {return EnumMousePointPosition.MouseSizeTopRight;}23. else24. {25. if (e.Y > -1 * Band + size.Height)26. {return EnumMousePointPosition.MouseSizeBottomRight;}27. else28. {return EnumMousePointPosition.MouseSizeRight;}33. if (e.Y < Band)34. {return EnumMousePointPosition.MouseSizeTop;}35. else36. {37. if (e.Y > -1 * Band + size.Height)38. {return EnumMousePointPosition.MouseSizeBottom;}39. else40. {return EnumMousePointPosition.MouseDrag;}41. }42. }43. }44. }45. else46. {return EnumMousePointPosition.MouseSizeNone;}47. }6、定义⾃⼰的MyMouseMove事件,在这个事件⾥,会使⽤上⾯设计的函数1. private void MyMouseMove(object sender,System.Windows.Forms.MouseEventArgs e)2. {3. Control lCtrl =(sender as Control);4.5. if (e.Button==MouseButtons.Left)6. {7. switch (m_MousePointPosition)8. {9. case EnumMousePointPosition.MouseDrag:10. lCtrl.Left =lCtrl.Left+ e.X - p.X;11. lCtrl.Top =lCtrl.Top+ e.Y - p.Y;12. break;13. case EnumMousePointPosition.MouseSizeBottom:14. lCtrl.Height = lCtrl.Height + e.Y - p1.Y;15. p1.X=e.X;16. p1.Y=e.Y; //'记录光标拖动的当前点17. break;18. case EnumMousePointPosition.MouseSizeBottomRight:19. lCtrl.Width = lCtrl.Width + e.X - p1.X;20. lCtrl.Height = lCtrl.Height + e.Y - p1.Y;21. p1.X=e.X;22. p1.Y=e.Y; //'记录光标拖动的当前点23. break;24. case EnumMousePointPosition.MouseSizeRight:25. lCtrl.Width = lCtrl.Width + e.X - p1.X;26. // lCtrl.Height = lCtrl.Height + e.Y - p1.Y;27. p1.X=e.X;28. p1.Y=e.Y; //'记录光标拖动的当前点29. break;30. case EnumMousePointPosition.MouseSizeTop:31. lCtrl.Top = lCtrl.Top + (e.Y - p.Y);32. lCtrl.Height = lCtrl.Height - (e.Y - p.Y);33. break;34. case EnumMousePointPosition.MouseSizeLeft:35. lCtrl.Left = lCtrl.Left + e.X - p.X;36. lCtrl.Width = lCtrl.Width - (e.X - p.X);37. break;38. case EnumMousePointPosition.MouseSizeBottomLeft:39. lCtrl.Left = lCtrl.Left + e.X - p.X;40. lCtrl.Width = lCtrl.Width - (e.X - p.X);41. lCtrl.Height = lCtrl.Height+ e.Y - p1.Y;42. p1.X=e.X;43. p1.Y=e.Y; //'记录光标拖动的当前点44. break;45. case EnumMousePointPosition.MouseSizeTopRight:46. lCtrl.Top = lCtrl.Top + (e.Y - p.Y);47. lCtrl.Width = lCtrl.Width + (e.X - p1.X);48. lCtrl.Height = lCtrl.Height - (e.Y - p.Y);49. p1.X=e.X;50. p1.Y=e.Y; //'记录光标拖动的当前点51. break;52. case EnumMousePointPosition.MouseSizeTopLeft:53. lCtrl.Left = lCtrl.Left + e.X - p.X;54. lCtrl.Top = lCtrl.Top + (e.Y - p.Y);55. lCtrl.Width = lCtrl.Width - (e.X - p.X);56. lCtrl.Height = lCtrl.Height - (e.Y - p.Y);57. break;58. default:59. break;60. }61. if (lCtrl.Width<MinWidth) lCtrl.Width=MinWidth;62. if (lCtrl.Height<MinHeight) lCtrl.Height=MinHeight;67. m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态68. switch (m_MousePointPosition) //'改变光标69. {70. case EnumMousePointPosition.MouseSizeNone:71. this.Cursor = Cursors.Arrow; //'箭头72. break;73. case EnumMousePointPosition.MouseDrag:74. this.Cursor = Cursors.SizeAll; //'四⽅向75. break;76. case EnumMousePointPosition.MouseSizeBottom:77. this.Cursor = Cursors.SizeNS; //'南北78. break;79. case EnumMousePointPosition.MouseSizeTop:80. this.Cursor = Cursors.SizeNS; //'南北81. break;82. case EnumMousePointPosition.MouseSizeLeft:83. this.Cursor = Cursors.SizeWE; //'东西84. break;85. case EnumMousePointPosition.MouseSizeRight:86. this.Cursor = Cursors.SizeWE; //'东西87. break;88. case EnumMousePointPosition.MouseSizeBottomLeft:89. this.Cursor = Cursors.SizeNESW; //'东北到南西90. break;91. case EnumMousePointPosition.MouseSizeBottomRight:92. this.Cursor = Cursors.SizeNWSE; //'东南到西北93. break;94. case EnumMousePointPosition.MouseSizeTopLeft:95. this.Cursor = Cursors.SizeNWSE; //'东南到西北96. break;97. case EnumMousePointPosition.MouseSizeTopRight:98. this.Cursor = Cursors.SizeNESW; //'东北到南西99. break;100. default:101. break;102. }103. }104.105. }7、制作⼀个初始化过程,将界⾯panel1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件,记得在Form初始化或者Form_Load时先执⾏它。
C# 获取和设置鼠标坐标
C# 获取和设置鼠标坐标运行界面:代码:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;//namespace mouseTest{public partial class Form1 : Form{public Form1(){InitializeComponent();}///<summary>///设置鼠标的坐标///</summary>///<param name="x">横坐标</param>///<param name="y">纵坐标</param>[DllImport("User32")]public extern static void SetCursorPos(int x, int y);///<summary>///获取鼠标的坐标///</summary>///<param name="lpPoint">传址参数,坐标point类型</param>///<returns>获取成功返回真</returns>[DllImport("User32")]public extern static bool GetCursorPos(ref Point lpPoint);Point p = new Point(1, 1);//定义存放获取坐标的point变量private void button_go_Click(object sender, EventArgs e){timer1.Start();SetCursorPos(int.Parse(textBox_x.Text), (int.Parse(textBox_y.Text)));}private void timer1_Tick_1(object sender, EventArgs e){GetCursorPos(ref p);label_p.Text = "X:" + p.X + "\r\nY:" + p.Y;if (Convert.ToInt16(p.X) > 560 && Convert.ToInt16(p.X) < 680 && Convert.ToInt16(p.Y) > 350 && Convert.ToInt16(p.Y) < 460)pictureBox1.Visible = true;elsepictureBox1.Visible = false;}}}源代码下载地址:/s/1jG9mqY6。
前端获取鼠标位置的方法
前端获取鼠标位置的方法在前端中,获取鼠标位置有多种方法。
下面将介绍几种常见的实现方式。
1. 使用原生JavaScript的`mousemove`事件:可以通过绑定`mousemove`事件来获取鼠标的当前位置。
通过`event`参数的`clientX`和`clientY`属性可以获取鼠标相对于浏览器窗口的水平和垂直坐标。
```javascriptdocument.addEventListener('mousemove', function(event)var mouseX = event.clientX;var mouseY = event.clientY;console.log("鼠标位置:X: " + mouseX + ", Y: " + mouseY);});```2. 使用原生JavaScript的`mouseenter`和`mouseleave`事件:这两个事件分别在鼠标进入和离开指定元素时触发。
通过`event`参数的`clientX`和`clientY`属性可以获取鼠标相对于浏览器窗口的水平和垂直坐标。
```javascriptdocument.addEventListener('mouseenter', function(event)var mouseX = event.clientX;var mouseY = event.clientY;console.log("鼠标进入位置:X: " + mouseX + ", Y: " + mouseY);});document.addEventListener('mouseleave', function(event)var mouseX = event.clientX;var mouseY = event.clientY;console.log("鼠标离开位置:X: " + mouseX + ", Y: " + mouseY);});```3. 使用jQuery:如果你在项目中使用了jQuery库,可以使用`mousemove`事件来获取鼠标的当前位置。
C#WinForm设置窗口无边框、窗口可移动、窗口显示在屏幕中央、控件去边框
C#WinForm设置窗⼝⽆边框、窗⼝可移动、窗⼝显⽰在屏幕中央、控件去边框1)窗⼝去除边框在组件属性中FormBorderStyle设为None2)窗⼝随着⿏标移动⽽动添加引⽤using System.Runtime.InteropServices;在初始化控件{InitializeComponent();}代码后添加1 [DllImport("user32.dll")]2public static extern bool ReleaseCapture();3 [DllImport("user32.dll")]4public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);5bool beginMove = false;//初始化⿏标位置6int currentXPosition;7int currentYPosition;8 //获取⿏标按下时的位置9 private void QRCode_MouseDown(object sender, MouseEventArgs e)10 {11if (e.Button == MouseButtons.Left)12 {13 beginMove = true;14 currentXPosition = MousePosition.X;//⿏标的x坐标为当前窗体左上⾓x坐标15 currentYPosition = MousePosition.Y;//⿏标的y坐标为当前窗体左上⾓y坐标16 }17 }18 //获取⿏标移动到的位置19private void QRCode_MouseMove(object sender, MouseEventArgs e)20 {21if (beginMove)22 {23this.Left += MousePosition.X - currentXPosition;//根据⿏标x坐标确定窗体的左边坐标x24this.Top += MousePosition.Y - currentYPosition;//根据⿏标的y坐标窗体的顶部,即Y坐标25 currentXPosition = MousePosition.X;26 currentYPosition = MousePosition.Y;27 }28 }29 //释放⿏标时的位置30private void QRCode_MouseUp(object sender, MouseEventArgs e)31 {32if (e.Button == MouseButtons.Left)33 {34 currentXPosition = 0; //设置初始状态35 currentYPosition = 0;36 beginMove = false;37 }38 }3)窗⼝居中显⽰利⽤C# Form中的StartPosition属性CenterScreen将界⾯显⽰在屏幕中央若是⽤代码实现,显⽰窗体前,应设置此属性,可在调⽤Show()或是ShowDialog()⽅法之前或在窗体构造函数中设置此属性,不要在load()事件中改变此属性,不起作⽤。
MFC获取picture控件的鼠标点击坐标位置的方法
MFC获取picture控件的鼠标点击坐标位置的方法MFC 获取picture控件的鼠标点击坐标位置的方法在一个自定义的Dialog中加入了picture控件,想要获取鼠标在该控件上的点击位置,遇到一些困难,最终解决了。
方法如下:其实挺简单的,首先用自定义的Dialog类重载CDialog的PreTranslateMessage函数,并在其中用到了Dialog的OnLButtonDown函数(其实不用也行,我只是想把操作封在这个函数里)这样就可以通过此函数传递点击位置。
BOOL PrintDialog::PreTranslateMessage(MSG* pMsg){// TODO: Add your specialized code here and/or call the base classif(pMsg->message == WM_LBUTTONDOWN &&GetDlgItem(IDC_PACKPIC)->GetSafeHwnd() == pMsg->hwnd)OnLButtonDown(MK_LBUTTON, pMsg->pt); //在此传递点击部位在对话框中的坐标return CDialog::PreTranslateMessage(pMsg);}接下来在Dialog中的OnLButtonDown函数中判段是否点在picture控件内(lRect是控件的区域)if((point.x>=lRect.left && point.x<=lRect.right) && (point.y>=lRect.top &&point.y<=lRect.bottom)){// 通过对point的处理,获得实际在picture控件中的点击位置(坐标),完成。
point.x-=lRect.left;point.y-=lRect.top;}。
QT实现鼠标操作事件(获得鼠标的坐标和间值)
QT实现⿏标操作事件(获得⿏标的坐标和间值)1、⾸先建⽴⼀个新的Widget⼯程在新建⼯程的头⽂件中定义申明⿏标按下、释放、移动、双击事件;然后到主程序中对定义的⼏个⿏标事件进⾏简单的处理注意在主程序最前⾯添加⿏标的头⽂件和要⽤到的其他头⽂件找到帮助⽂档:() const : QPoint() const : int() const : int() const : Qt::MouseButton在其中这⼏个代表的是⿏标事件触发后的坐标值和按下的⿏标值(是⿏标左键还是右键还是中键)打印各个事件触发后的⿏标坐标值和⿏标值添加以下代码void Widget::mousePressEvent(QMouseEvent *event){qDebug()<<"⿏标按下"<<endl;if(event->button()==Qt::LeftButton)qDebug()<<"左键按下"<<endl;else if(event->button()==Qt::RightButton)qDebug()<<"右键按下"<<endl;else if(event->button()==Qt::MidButton)qDebug()<<"中键按下"<<endl;qDebug()<<event->globalPos()<<endl;qDebug()<<event->windowPos()<<endl;}void Widget::mouseReleaseEvent(QMouseEvent *event){qDebug()<<"⿏标释放"<<endl;qDebug()<<event->globalPos()<<endl;qDebug()<<event->windowPos()<<endl;}void Widget::mouseMoveEvent(QMouseEvent *event){qDebug()<<"⿏标移动"<<endl;qDebug()<<event->globalPos()<<endl;qDebug()<<event->windowPos()<<endl;}void Widget::mouseDoubleClickEvent(QMouseEvent *event){qDebug()<<"⿏标双击"<<endl;qDebug()<<event->globalPos()<<endl;qDebug()<<event->windowPos()<<endl;}。
C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果
C#winform⾃定义控件模拟设计时界⾯⿏标移动和调节⼤⼩、选中效果要想玩转Winform⾃定义控件需要对GDI+⾮常熟悉,对常⽤的控件有⼀些了解,好选择合适的基类控件来简化。
要点说明及代码1)定义接⼝:using System;using System.Windows.Forms;namespace GDIPrinterDriver{///<summary>///模板元素接⼝///</summary>public interface ILabelDesignElement{///<summary>/// PrintData/codeContext⾥的字段,{} []///</summary>string动态内容 { get; set; }///<summary>///是否被选中///</summary>bool DesignSelected { get; set; }///<summary>///选择状态发⽣改变///</summary>event Action<object, bool> SelectedStatusChange;///<summary>///本控件被选中时键盘⽅向键被按下///</summary>///<param name="keyData"></param>void KeysChangedLocation(Keys keyData);}}2)控件基类实现:using GDIPrinterDriver;using System;using ponentModel;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.Windows.Forms;namespace GDILabelDesigner{///<summary>///设计时控件基类///</summary>public abstract class DesignCellControl : PictureBox, ILabelDesignElement{#region⿏标移动和缩放private enum EnumMousePointPosition{MouseSizeNone = 0, //'⽆MouseSizeRight = 1, //'拉伸右边框MouseSizeLeft = 2, //'拉伸左边框MouseSizeBottom = 3, //'拉伸下边框MouseSizeTop = 4, //'拉伸上边框MouseSizeTopLeft = 5, //'拉伸左上⾓MouseSizeTopRight = 6, //'拉伸右上⾓MouseSizeBottomLeft = 7, //'拉伸左下⾓MouseSizeBottomRight = 8, //'拉伸右下⾓MouseDrag = 9// '⿏标拖动}const int Band = 5;const int MinWidth = 10;const int MinHeight = 10;private EnumMousePointPosition m_MousePointPosition;private Point p, p1;private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e) {if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height)){if (e.X < Band){if (e.Y < Band) { return EnumMousePointPosition.MouseSizeTopLeft; }else{if (e.Y > -1 * Band + size.Height){ return EnumMousePointPosition.MouseSizeBottomLeft; }else{ return EnumMousePointPosition.MouseSizeLeft; }}}else{if (e.X > -1 * Band + size.Width){if (e.Y < Band){ return EnumMousePointPosition.MouseSizeTopRight; }else{if (e.Y > -1 * Band + size.Height){ return EnumMousePointPosition.MouseSizeBottomRight; }else{ return EnumMousePointPosition.MouseSizeRight; }}}else{if (e.Y < Band){ return EnumMousePointPosition.MouseSizeTop; }else{if (e.Y > -1 * Band + size.Height){ return EnumMousePointPosition.MouseSizeBottom; }else{ return EnumMousePointPosition.MouseDrag; }}}}}else{ return EnumMousePointPosition.MouseSizeNone; }}#endregionpublic bool DesignSelected{get{return designSelected;}set{designSelected = value;Invalidate();}}private bool designSelected = false;public DynamicMapProperty DynamicMapProperty { get; set; }public StaticMapProperty StaticMapProperty { get; set; }public string动态内容 { get; set; }public RoteDescription RoteDescription { get; set; }///<summary>///被选中,获取到焦点的事件///</summary>public event Action<object, bool> SelectedStatusChange;protected override void OnClick(EventArgs e){DesignSelected = true;SelectedStatusChange?.Invoke(this, DesignSelected);}protected override void OnMouseWheel(MouseEventArgs e){base.OnMouseWheel(e);double d = 1.068D;if (e.Delta > 0 && DesignSelected){this.Size = new Size((int)(this.Size.Width * d), (int)(this.Size.Height * d)); }else{this.Size = new Size((int)(this.Size.Width / d), (int)(this.Size.Height / d)); }}protected override void OnMouseDown(MouseEventArgs e){p.X = e.X;p.Y = e.Y;p1.X = e.X;p1.Y = e.Y;}protected override void OnMouseUp(MouseEventArgs e){m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;this.Cursor = Cursors.Arrow;}protected override void OnMouseMove(MouseEventArgs e){if (e.Button == MouseButtons.Left){switch (m_MousePointPosition){#region位置计算case EnumMousePointPosition.MouseDrag:Left = Left + e.X - p.X;Top = Top + e.Y - p.Y;break;case EnumMousePointPosition.MouseSizeBottom:Height = Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点break;case EnumMousePointPosition.MouseSizeBottomRight:Width = Width + e.X - p1.X;Height = Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点break;case EnumMousePointPosition.MouseSizeRight:Width = Width + e.X - p1.X;Height = Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点break;case EnumMousePointPosition.MouseSizeTop:Top = Top + (e.Y - p.Y);Height = Height - (e.Y - p.Y);Left = Left + e.X - p.X;Width = Width - (e.X - p.X);break;case EnumMousePointPosition.MouseSizeBottomLeft: Left = Left + e.X - p.X;Width = Width - (e.X - p.X);Height = Height + e.Y - p1.Y;p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点break;case EnumMousePointPosition.MouseSizeTopRight:Top = Top + (e.Y - p.Y);Width = Width + (e.X - p1.X);Height = Height - (e.Y - p.Y);p1.X = e.X;p1.Y = e.Y; //'记录光标拖动的当前点break;case EnumMousePointPosition.MouseSizeTopLeft:Left = Left + e.X - p.X;Top = Top + (e.Y - p.Y);Width = Width - (e.X - p.X);Height = Height - (e.Y - p.Y);break;default:break;#endregion}if (Width < MinWidth) Width = MinWidth;if (Height < MinHeight) Height = MinHeight;if (Tag != null){if (Tag is ImageElementNode){var tag = Tag as ImageElementNode;tag.Location = Location;}else if (Tag is BarcodeElementNode){var tag = Tag as BarcodeElementNode;tag.Location = Location;}else if (Tag is TextBoxElementNode){var tag = Tag as TextBoxElementNode;tag.Location = Location;}}}else{m_MousePointPosition = MousePointPosition(Size, e);switch (m_MousePointPosition){#region改变光标case EnumMousePointPosition.MouseSizeNone:this.Cursor = Cursors.Arrow; //'箭头break;case EnumMousePointPosition.MouseDrag:this.Cursor = Cursors.SizeAll; //'四⽅向break;case EnumMousePointPosition.MouseSizeBottom:this.Cursor = Cursors.SizeNS; //'南北break;case EnumMousePointPosition.MouseSizeTop:this.Cursor = Cursors.SizeNS; //'南北break;case EnumMousePointPosition.MouseSizeLeft:this.Cursor = Cursors.SizeWE; //'东西break;case EnumMousePointPosition.MouseSizeRight:this.Cursor = Cursors.SizeWE; //'东西break;case EnumMousePointPosition.MouseSizeBottomLeft:this.Cursor = Cursors.SizeNESW; //'东北到南西break;case EnumMousePointPosition.MouseSizeBottomRight:this.Cursor = Cursors.SizeNWSE; //'东南到西北this.Cursor = Cursors.SizeNWSE; //'东南到西北break;case EnumMousePointPosition.MouseSizeTopRight:this.Cursor = Cursors.SizeNESW; //'东北到南西break;default:break;#endregion}}}///<summary>///绘制⽅框///</summary>///<param name="g"></param>protected void DrawSelectedStatus(Graphics g){Rectangle rect = ClientRectangle;rect.Inflate(-6, -6);using (Pen p = new Pen(Brushes.Black, 1)){p.DashStyle = DashStyle.Dot;p.DashStyle = DashStyle.Solid;//8个⽅块g.FillRectangle(Brushes.White, new Rectangle(rect.Left - 4, rect.Top - 4, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top - 4, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width, rect.Top - 4, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left - 4, rect.Top + rect.Height / 2 - 3, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left - 4, rect.Top + rect.Height, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height / 2 - 3, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top + rect.Height, 4, 4));g.FillRectangle(Brushes.White, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left - 4, rect.Top - 4, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top - 4, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width, rect.Top - 4, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left - 4, rect.Top + rect.Height / 2 - 3, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left - 4, rect.Top + rect.Height, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height / 2 - 3, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width / 2 - 3, rect.Top + rect.Height, 4, 4));g.DrawRectangle(p, new Rectangle(rect.Left + rect.Width, rect.Top + rect.Height, 4, 4));}}///<summary>///旋转图⽚///</summary>///<param name="bmp">源图</param>///<param name="angle">⾓度</param>///<param name="bkColor">背景⾊</param>///<returns></returns>protected Bitmap ImageRotate(Bitmap bmp, float angle, Color bkColor){int w = bmp.Width + 2;int h = bmp.Height + 2;PixelFormat pf;if (bkColor == Color.Transparent){pf = PixelFormat.Format32bppArgb;}else{pf = bmp.PixelFormat;}Bitmap tmp = new Bitmap(w, h, pf);Graphics g = Graphics.FromImage(tmp);g.Clear(bkColor);g.DrawImageUnscaled(bmp, 1, 1);g.Dispose();GraphicsPath path = new GraphicsPath();path.AddRectangle(new RectangleF(0f, 0f, w, h));Matrix mtrx = new Matrix();mtrx.Rotate(angle);RectangleF rct = path.GetBounds(mtrx);Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);g = Graphics.FromImage(dst);g.Clear(bkColor);g.TranslateTransform(-rct.X, -rct.Y);g.RotateTransform(angle);g.InterpolationMode = InterpolationMode.HighQualityBilinear;g.DrawImageUnscaled(tmp, 0, 0);g.Dispose();tmp.Dispose();return dst;}///<summary>///响应键盘光标键///</summary>///<param name="keyData"></param>public virtual void KeysChangedLocation(Keys keyData){if (keyData == Keys.Up){Top -= 1;}if (keyData == Keys.Down){Top += 1;}if (keyData == Keys.Left){Left -= 1;}if (keyData == Keys.Right){Left += 1;}if (Tag != null){if (Tag is ImageElementNode){var tag = Tag as ImageElementNode;tag.Location = Location;}else if (Tag is BarcodeElementNode){var tag = Tag as BarcodeElementNode;tag.Location = Location;}else if (Tag is TextBoxElementNode){var tag = Tag as TextBoxElementNode;tag.Location = Location;}}}}}。
(3条消息)winformChart控件获取鼠标处坐标值方法
(3条消息)winformChart控件获取鼠标处坐标值方法Chart控件本身功能强大,应用广泛,因此其属性、方法也很多。
此处介绍在很多应用中需要查看鼠标位置处坐标值的一些方法1,调用Chart事件 GetToolTip利用ToolTipEventArgs ,将ToolTipEventArgs的Text属性赋值即可实现,优点是非常简单,无需其他步骤,但其因为用到HitTest方法,只能获取序列点上的坐标值,不能获得“空白”位置的值private void chart_Wave1_GetT oolTipText(object sender, ToolTipEventArgs e){if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint){this.Cursor = Cursors.Cross;int i = e.HitTestResult.PointIndex;DataPoint dp = e.HitTestResult.Series.Points[i];e.Text = string.Format("数值:{1:F3}" + e.HitTestResult.Series.ChartArea + " \n日期:{0}", DateTime.FromOADate(dp.XValue), dp.YValues[0]);}else{this.Cursor = Cursors.Default;}}2.利用Chart的MouseMove事件和Chart方法 HitTest特点是可以获取鼠标值,同样因为用到HitTest方法,只能获取序列上序列点处的坐标值,且需要自我实现值的显示private void chart_Wave1_MouseMove(object sender, MouseEventArgs e){HitTestResult myTestResult= chart_Wave1.HitTest(e.X,e.Y);if (myTestResult.ChartElementType == ChartElementType.DataPoint){this.Cursor = Cursors.Cross;int i = myTestResult.PointIndex;DataPoint dp = myTestResult.Series.Points[i];double doubleXValue= (dp.XValue);double doubleYValue = dp.YValues[0];//自我实现值的显示}else{this.Cursor = Cursors.Default;}}3.利用Chart的MouseMove事件和获取鼠标处坐标实现根据鼠标位置自动转化为Chart控件ChartArea上的坐标值,再根据序列上(可以看成数组),可以找到每个序列点的坐标值,从而可以确定鼠标所在点在哪两个序列点之间,再利用线性插值或其他插值算法,计算出该点的值。
winfrom text imemode 取消光标定位
winfrom text imemode 取消光标定位取消光标定位是一项非常有用的功能,它允许在WinForm应用程序中通过编程方式控制光标的位置。
通过这种功能,开发人员可以实现许多有趣和实用的功能,如自动滚动窗体、输入框中的提示文本和键盘导航。
在本文中,我将逐步介绍如何在WinForm应用程序中取消光标定位,并提供一些实际应用的例子和代码示例。
首先,我们需要了解一下在WinForm中如何设置光标的位置。
通常情况下,我们可以通过将焦点设置到特定的控件上来设置光标的位置。
例如,可以使用`textBox1.Focus()`语句将光标设置到名为`textBox1`的文本框控件上。
然而,在某些情况下,我们可能希望取消光标定位,即不将焦点设置到任何控件上。
这在某些特定的用户界面设计中非常有用,例如当我们希望在窗体加载时避免自动聚焦到某个特定的控件上。
要实现这一点,我们可以使用`Control.TabStop`属性。
这个属性控制了一个控件是否允许获得焦点。
默认情况下,大多数控件的`TabStop`属性都是`true`,即它们可以通过使用Tab键在控件之间进行导航。
但是,通过将`TabStop`属性设置为`false`,我们可以取消光标的定位。
以下是一个简单的示例,演示了如何取消光标定位:csharppublic partial class Form1 : Form{public Form1(){InitializeComponent();textBox1.TabStop = false; 取消光标定位到textBox1 }}在这个例子中,我在窗体的构造函数中将`textBox1.TabStop`属性设置为`false`,这样窗体加载时就不会将焦点定位到`textBox1`控件上。
除了取消光标定位外,我们还可以使用一些其他的技巧和技巧来增强用户界面的体验。
例如,可以使用`Control.SelectNextControl`方法在控件之间进行键盘导航。
(原)python中matplot中获得鼠标点击的位置及显示灰度图像
(原)python中matplot中获得⿏标点击的位置及显⽰灰度图像转载请注明出处:
参考⽹址:
1. 获得⿏标点击位置——使⽤ginput函数:
import matplotlib.pyplot as plt
import numpy as np
x=range(1,10)
y=[2*v for v in x]
print(x, y)
plt.plot(x, y)
pos=plt.ginput(3)
print(pos)
那三个红⾊的⼗字为⿏标点击的位置,显⽰如下:
2. 显⽰灰度图像:
默认imshow显⽰的是彩⾊图像:
需要显⽰灰度图像时,可以使⽤plt.get_cmap:
import matplotlib.pyplot as plt
import Image
im = Image.open("th.jpg")
plt.imshow(im, cmap = plt.get_cmap("gray"))
pos=plt.ginput(3)
print(pos)
可以使⽤vmin和vmax来达到和matlab的imshow中[]⼀样的效果,将⼩于vmin的量化成⿊⾊,⼤于vmax的量化成⽩⾊:
plt.imshow(im, cmap = plt.get_cmap("gray"), vmin = 100, vmax = 150)
那三个红⾊的⼗字为⿏标点击的位置,显⽰如下:
由上⾯可见,直接plot的话,左下⾓是坐标原点;使⽤imshow时,是左上⾓为坐标原点。
对于图像来说,和图像的坐标⼀样,不⽤转换,可以直接使⽤。
C#鼠标移动Winform窗体内或者panel容器内的控件显示虚线实现虚线框来确定位置
C#⿏标移动Winform窗体内或者panel容器内的控件显⽰虚线实现虚线框来确定位置C# ⿏标移动WinForm窗体或者panel容器内的控件移动虚线/实现虚线框来确定位置1.⽤到的⽅法介绍今天,根据领导指⽰指导移动容器内的控件,⽣成虚线框,使⽤ControlPaint.DrawReversibleFrame1//2// 摘要:3// 在屏幕上的指定边界内,按指定背景⾊绘制处于指定状态的可逆框架。
4//5// 参数:6// rectangle:7// 代表要绘制矩形的尺⼨的 System.Drawing.Rectangle(采⽤屏幕坐标)。
8//9// backColor:10// 框架的背景的 System.Drawing.Color。
11//12// style:13// System.Windows.Forms.FrameStyle 值之⼀,它指定框架的样式。
14public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, FrameStyle style);2.程序运⾏效果3.代码实现1public Form1()2 {3 InitializeComponent();4 }56private Point downPoint;7private Rectangle downRectangle;8private Rectangle lastRectangle;910private void pictureBox1_MouseDown(object sender, MouseEventArgs e)11 {12if (e.Button != MouseButtons.Left) return;1314 downPoint = e.Location;15 downRectangle =new Rectangle(0, 0, ((Control)sender).Width, pictureBox1.Height);16 downRectangle.Offset(((Control)sender).PointToScreen(new Point(0, 0)));17 ControlPaint.DrawReversibleFrame(downRectangle, Color.White, FrameStyle.Thick);1819 lastRectangle = downRectangle;20 }21private void pictureBox1_MouseMove(object sender, MouseEventArgs e)22 {23if (e.Button != MouseButtons.Left) return;2425 ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick);2627 Rectangle rectangle = downRectangle;28 rectangle.Offset(e.X - downPoint.X, e.Y - downPoint.Y);29 ControlPaint.DrawReversibleFrame(rectangle, Color.White, FrameStyle.Thick);3031 lastRectangle = rectangle;32 }33private void pictureBox1_MouseUp(object sender, MouseEventArgs e)34 {35if (e.Button != MouseButtons.Left) return;3637 ControlPaint.DrawReversibleFrame(lastRectangle, Color.White, FrameStyle.Thick);3839 pictureBox1.Location = new Point(40 ((Control)sender).Location.X + e.X - downPoint.X,41 ((Control)sender).Location.Y + e.Y - downPoint.Y);42 }4.程序源代码⼯程⽂件下载。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
this.Text = e.Location.ToString();
}
Keesés afómokban: 如题,C#2008获取点击PicteBox控件时在PicteBox中的坐标值,需要把这个坐标值传递给其他函数使用,谢谢!我需要的是在PicteBox控件里的坐标值。我需要先给出个提示消息,确定之后再点击图像,但是这个过程怎么才可以使程序等待我点击PicteBox控件呢?我尝试用了MoseCick()事件,但是不行。Váaszokpivate void picteBox1_MoseMove(object sende,MoseEventAgs e){int oiginaWidth=this.picteBox1.Image.Width;int oiginaHeight=this.picteBox1.Image.Height;PopetyInfo ectangePopety=this.picteBox1.GetType().GetPopety(&qot;ImageRectange&qot;,BindingFags.Instance|BindingFags.NonPbic);Rectange ectange=(Rectange)ectangePopety.GetVae(this.picteBox1,n);int centWidth=ectange.Width;int centHeight=ectange.Height;dobe ate=(dobe)centHeight(dobe)oiginaHeight;int back_eft_width=(centWidth==this.picteBox1.Width)?0:(this.picteBox1.Width-centWidth)2;int back_top_height=(centHeight==this.picteBox1.Height)?0:(this.picteBox1.Height-centHeight)2;int zoom_x=e.X-back_eft_width;int zoom_y=e.Y-back_top_height;dobe oigina_x=(dobe)zoom_x*ate;dobe oigina_y=(dobe)zoom_y*ate;StingBide sb=new StingBide();sb.AppendFomat(&qot;原始尺寸{0}{1}(宽高)\\n&qot;,oiginaWidth,oiginaHeight);sb.AppendFomat(&qot;缩放状态图片尺寸{0}{1}(宽高)\\n&qot;,centWidth,centHeight);sb.AppendFomat(&qot;缩放比率{0}\\n&qot;,ate);sb.AppendFomat(&qot;左留白宽度{0}\\n&qot;,back_eft_width);sb.AppendFomat(&qot;上留白高度{0}\\n&qot;,back_top_height);sb.AppendFomat(&qot;当前鼠标坐标{0}{1}(XY)\\n&qot;,e.X,e.Y);sb.AppendFomat(&qot;缩放图中鼠标坐标{0}{1}(XY)\\n&qot;,zoom_x,zoom_y);sb.AppendFomat(&qot;原始图中鼠标坐标{0}{1}(XY)\\n&qot;,oigina_x,oigina_y);this.abe1.Text=sb.ToSting();}Megjete váaszként:pcx2010.agszts 31.13:21Az sszes váaszMoseCick事件中事件参数MoseEventAgs中可以获取X,Y坐标。pivate void picteBox1_MoseCick(object sende,MoseEventAgs e){点击时记录坐标int x=e.X;int y=e.Y;if(MessageBox.Show(&qot;你确定吗?&qot;,&qot;提示&qot;,MessageBoxBttons.YesNo,MessageBoxIcon.Qestion)==DiaogRest.Yes){执行你的方法,无须模拟鼠标动作YoMethod(x,y);}}但是我需要的是先提示我点击鼠标,确定之后我再点击PicteBox控件。不是点击之后在确定啊,因为是做在一个菜单下,我点击菜单之后需要有提示我才点击了。2010.agszts 20.2:24 feiyn0112MVP,Modeátomsemove事件判断坐标,提示tootip但是这个仍然不是我所希望和需要的啊?麻烦各位帮我解答下,谢谢!你再把你的问题说清楚点吧!这样才好帮助你。知识改变命运,奋斗成就人生!我的想法是这样的,我在窗体中有一个PicteBox控件,我需要获取我鼠标点击这个控件时的坐标值,但是这个坐标值的话我又想换为这个控件中图像的图像坐标值,也就是图像的x,y值。我这个PicteBox控件的sizemode属性设置为zoom方式,请问该怎么样才可以实现呢?有没有好的解决办法呢?如果你是想将点击缩放后图片的坐标值换为图片正常尺寸的坐标值,那么你需要将图片的缩放比例算出来就行了。正常值=图片绽放状态的坐标值*缩放比例知识改变命运,奋斗成就人生!但是如果我这个图片不是恰好的正常缩放情况下填充满整个PicteBox控件的话,那怎么知道这个坐标换到图片上去的坐标值呢?还有就是这个图片的缩放比例改怎么样获得呢?谢谢当前显示的尺寸图片原始尺寸=绽放比例。如果图片正好用原始尺寸显示在pictebox中,那么缩放比例为1。知识改变命运,奋斗成就人生!但是这个当前显示的尺寸怎么获取呢?如果我获取图片的width和height那是原始大小了?但如果获取PicteBox的大小的话,如果图片缩放后不是完全填充PicteBox控件空间的话那怎么获得图片大小呢?谢谢!2010Micosoft Copoation.Minden jog fenntatva.
//其他需要执行的代码
}
}
mouseclick事件
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right || e.Button == MouseButtons.Left)
mouseclick事件
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right || e.Button == MouseButtons.Left)
{
Point p = MousePosition;//获取位置
{
Point p = MousePosition;//获取位置
//ic Form1()
{
this.MouseClick += new MouseEventHandler(Form1_MouseClick);
}
void Form1_MouseClick(object sender, MouseEventArgs e)