此处将为大家介绍关于C#–如何获得鼠标点击命令行EXE不使用WINFORM的详细内容,并且为您解答有关c#获取鼠标事件的相关问题,此外,我们还将为您介绍关于.net–为什么winform面板仅在鼠标悬
此处将为大家介绍关于C# – 如何获得鼠标点击命令行EXE不使用WINFORM的详细内容,并且为您解答有关c# 获取鼠标事件的相关问题,此外,我们还将为您介绍关于.net – 为什么winform面板仅在鼠标悬停或鼠标点击时更新?、c# winform 使用webapi、C# winform 获取鼠标点击位置、c# – WinForms禁用双击并接受所有鼠标点击?的有用信息。
本文目录一览:- C# – 如何获得鼠标点击命令行EXE不使用WINFORM(c# 获取鼠标事件)
- .net – 为什么winform面板仅在鼠标悬停或鼠标点击时更新?
- c# winform 使用webapi
- C# winform 获取鼠标点击位置
- c# – WinForms禁用双击并接受所有鼠标点击?
C# – 如何获得鼠标点击命令行EXE不使用WINFORM(c# 获取鼠标事件)
我看到一些“相似”的问题。 但答案总是要求提问者使用winform。 我需要100%的控制台应用程序,可以挂钩到Windows消息队列,并给鼠标点击点。 鼠标点击可以发生在窗口的任何地方。
我所做的:我使用winforms完美地完成了这个任务。 其实我复制了一个博客的大部分代码。 这是工作。 但是我现在的项目是“自动化testing”。 在这里,我们必须将大部分应用程序作为控制台应用程序启动。 否则操作将变得一团糟。 我试着用IMessageFilter,然后我才知道它需要表单。
任何人都可以指导我正确的方向吗?
注意:我正在使用Windows7,.Net4.5,Visual Studio Express – 2012
.net开源和运行平台select
不允许调整窗口大小
切换RIDEV_CAPTUREMOUSE |时出现奇怪的行为 RIDEV_NOLEGACY
从窗口应用程序开始,用于检索安装在远程PC上的软件和硬件列表
Windows句柄如果控件设置为可见= false(.NET)
编辑:
我根本不在乎控制台。 我的目标是获得鼠标点击坐标(在屏幕上的任何地方)。 这意味着首先我会从控制台发出程序,然后我会在屏幕上点击一下。 控制台应该立即打印出鼠标点击的坐标。
GetwindowRect在一个应用程序上返回错误的值,所有其他应用程序都是正确的
通过C#确定本地组的成员
如何在.NET中确定我的Monitor的真实像素大小?
Windows服务应用程序在C#
添加自定义/新的属性到任何文件,无论types和扩展名,例如在.txt文件上设置“作者”
这是我所要做的事情,尽管我对于我是否理解这个问题还是有些朦胧。
创建一个正常的控制台应用
安装一个鼠标钩子WH_MOUSE_LL 。
如您所愿处理来自钩子的鼠标消息,例如通过在控制台上输出信息。
在WinForm中编写你的程序,但做一个无形的应用程序。
然后,将这个应用程序附加到父控制台,并写下你想要的东西:
NativeMethods.AttachConsole(NativeMethods.ATTACH_PARENT_PROCESS); Console.WriteLine("Coordinate : " + mouse.X);
使用这个类来做到这一点:
internal static class NativeMethods { internal const int ATTACH_PARENT_PROCESS = -1; /// <summary> /// Allocates a new console for the calling process. /// </summary> /// <returns>nonzero if the function succeeds; otherwise,zero.</returns> /// <remarks> /// A process can be associated with only one console,/// so the function fails if the calling process already has a console. /// http://msdn.microsoft.com/en-us/library/ms681944(VS.85).aspx /// </remarks> [DllImport("kernel32.dll",SetLastError = true)] internal static extern int AllocConsole(); [DllImport("kernel32.dll")] internal static extern bool AttachConsole(int dwProcessId); /// <summary> /// Detaches the calling process from its console. /// </summary> /// <returns>nonzero if the function succeeds; otherwise,zero.</returns> /// <remarks> /// If the calling process is not already attached to a console,/// the error code returned is ERROR_INVALID_ParaMETER (87). /// http://msdn.microsoft.com/en-us/library/ms683150(VS.85).aspx /// </remarks> [DllImport("kernel32.dll",SetLastError = true)] internal static extern int FreeConsole(); }
.net – 为什么winform面板仅在鼠标悬停或鼠标点击时更新?
以下是2个面板,一个按钮和一个计时器的winform外观:
这是它背后的代码:
@H_301_13@namespace TestIndicator; interface uses System.Drawing,System.Collections,System.Collections.Generic,System.Windows.Forms,System.ComponentModel; type /// <summary> /// Summary description for MainForm. /// </summary> MainForm = partial class(System.Windows.Forms.Form) private method d_Click(sender: System.Object; e: System.EventArgs); method timer1_Tick(sender: System.Object; e: System.EventArgs); protected method dispose(disposing: Boolean); override; public constructor; end; var TurnOnRx,TurnOnTx:Boolean; implementation {$REGION Construction and disposition} constructor MainForm; begin // // required for Windows Form Designer support // InitializeComponent(); // // Todo: Add any constructor code after InitializeComponent call // TurnOnRx := true; TurnOnTx := true; end; method MainForm.dispose(disposing: Boolean); begin if disposing then begin if assigned(components) then components.dispose(); // // Todo: Add custom disposition code here // end; inherited dispose(disposing); end; {$ENDREGION} method MainForm.d_Click(sender: System.Object; e: System.EventArgs); begin timer1.Enabled := not timer1.Enabled; end; method MainForm.timer1_Tick(sender: System.Object; e: System.EventArgs); begin if TurnOnTx then begin TurnOnTx:=false; TxLight.BackColor := Color.Red; end else begin TurnOnTx:=true; TxLight.BackColor := Color.black; end; if TurnOnRx then begin TurnOnRx := false; RxLight.BackColor := Color.Lime; end else begin TurnOnRx := true; RxLight.BackColor := Color.Black; end; end; end.解决方法
c# winform 使用webapi
public static string HttpPostNoFile(string url, Dictionary<string,string> para)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.Method = "POST";//get或者post
StringBuilder buffer = new StringBuilder();//这是要提交的数据
int i = 0;
//通过泛型集合转成要提交的参数和数据
foreach (string key in para.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, para[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, para[key]);
}
i++;
}
byte[] bs = Encoding.UTF8.GetBytes(buffer.ToString());//UTF-8
request.ContentLength = bs.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
return reader.ReadToEnd().ToString();
}
}
}
public static string HttpGet(string url, Dictionary<string, string> para)
{
Encoding encoding = Encoding.UTF8;
WebClient webClient = new WebClient();
StringBuilder buffer = new StringBuilder("?");//这是要提交的数据
int i = 0;
foreach (string key in para.Keys)
{
string pk = para[key].Replace(''_'', ''-'');
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, pk);
}
else
{
buffer.AppendFormat("{0}={1}", key, pk);
}
i++;
}
url = url + buffer.ToString();
Stream stream = webClient.OpenRead(url);
StreamReader sr = new StreamReader(stream);
string str = sr.ReadToEnd();
return str;
}
/// <summary>
/// 把对象转换为JSON字符串
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string ToJSON(this object o)
{
if(o == null)
{
return null;
}
return JsonConvert.SerializeObject(o);
}
/// <summary>
/// 把Json文本转为实体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input"></param>
/// <returns></returns>
public static T FromJSON<T>(this string input)
{
try
{
return JsonConvert.DeserializeObject<T>(input);
}
catch (Exception ex)
{
return default(T);
}
}
C# winform 获取鼠标点击位置
说明:该篇随笔的代码内容并非出自本人,是在其他网站搜寻的,出处已经不记得了,本次随笔只为记录,目的帮助自己,帮助他人。
实现的原理也不做多的赘述,直接上代码。
第一个类是需要用到的Windows API


public class Win32Api
{
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//安装钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸载钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//调用下一个钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
}
第二个类是用于监控鼠标移动,鼠标点击的


public class MouseHook
{
private Point point;
private Point Point
{
get { return point; }
set
{
if (point != value)
{
point = value;
if (MouseMoveEvent != null)
{
var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
MouseMoveEvent(this, e);
}
}
}
}
private int hHook;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_RBUTTONDOWN = 0x204;
public const int WH_MOUSE_LL = 14;
public Win32Api.HookProc hProc;
public MouseHook()
{
this.Point = new Point();
}
public int SetHook()
{
hProc = new Win32Api.HookProc(MouseHookProc);
hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
return hHook;
}
public void UnHook()
{
Win32Api.UnhookWindowsHookEx(hHook);
}
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct));
if (nCode < 0)
{
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
if (MouseClickEvent != null)
{
MouseButtons button = MouseButtons.None;
int clickCount = 0;
switch ((Int32)wParam)
{
case WM_LBUTTONDOWN:
button = MouseButtons.Left;
clickCount = 1;
break;
case WM_RBUTTONDOWN:
button = MouseButtons.Right;
clickCount = 1;
break;
}
var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
MouseClickEvent(this, e);
}
this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y);
return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
public event MouseMoveHandler MouseMoveEvent;
public delegate void MouseClickHandler(object sender, MouseEventArgs e);
public event MouseClickHandler MouseClickEvent;
}
最后直接在窗体中调用重写的事件即可


public getLocationForm()
{
InitializeComponent();
}
MouseHook mh;
private void getLocationForm_Load(object sender, EventArgs e)
{
mh = new MouseHook();
mh.SetHook();
mh.MouseMoveEvent += mh_MouseMoveEvent;
mh.MouseClickEvent += mh_MouseClickEvent;
}
private void mh_MouseClickEvent(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
//MessageBox.Show(e.X + "-" + e.Y);
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
}
private void mh_MouseMoveEvent(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
textBox1.Text = x + "";
textBox2.Text = y + "";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
mh.UnHook();
}
c# – WinForms禁用双击并接受所有鼠标点击?
解决方法
然而,对于内置的Winforms控件,如PictureBox,可以使用Control.SetStyle()方法进行配置.在项目中添加一个新类并粘贴下面显示的代码.编译.从工具箱顶部删除新控件:
using System; using System.Windows.Forms; class MyPictureBox : PictureBox { public MyPictureBox() { this.SetStyle(ControlStyles.StandardDoubleClick,false); } }
但请注意,这不适用于包装现有本机Windows控件的.NET类.像TextBox,ListBox,TreeView等.它的基础配置是WNDCLASSEX.style member,CS_DBLCLKS样式标志.设置该样式标志的代码将烘焙到Windows中,无法更改.您需要进行不同类型的手术才能将双击返回到单击.您可以通过重写WndProc()方法来实现,我将为TextBox提供一个示例:
using System; using System.Windows.Forms; class MyTextBox : TextBox { protected override void WndProc(ref Message m) { // Change WM_LBUTTONDBLCLK to WM_LBUTTONCLICK if (m.Msg == 0x203) m.Msg = 0x201; base.WndProc(ref m); } }
如果要对其他控件执行此操作,只需更改类名. Commandeering Winforms让它以你想要的方式工作从不需要太多的代码,只是Petzold
总结
以上是小编为你收集整理的c# – WinForms禁用双击并接受所有鼠标点击?全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
关于C# – 如何获得鼠标点击命令行EXE不使用WINFORM和c# 获取鼠标事件的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于.net – 为什么winform面板仅在鼠标悬停或鼠标点击时更新?、c# winform 使用webapi、C# winform 获取鼠标点击位置、c# – WinForms禁用双击并接受所有鼠标点击?的相关知识,请在本站寻找。
本文标签: