在自定义类中自动响应鼠标(移动或单击)

jjjy 2003-12-18 09:09:34
如题:
类似VC的相应鼠标消息???
怎么做
...全文
87 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jjjy 2003-12-18
  • 打赏
  • 举报
回复
现在就是不知道基类中
有关鼠标移动的处理函数
应该怎么写,
比如 处理移动图形对象的操作
有人能举个例子吗

看 wangj2001(乡村酒吧)
知道
GetCursorPos 可以获取鼠标点的位置
SetCursorPos 可以设置鼠标点的位置
很有用
jjjy 2003-12-18
  • 打赏
  • 举报
回复
我的基类是自己写的
定义的都是图形元素
继承类都是 像:线,多边形,园,曲线等 矢量图形元素
应该不能像 Edelweissobject(青山绿水) 说的那样做吧...
angxain 2003-12-18
  • 打赏
  • 举报
回复
UP
jjjy 2003-12-18
  • 打赏
  • 举报
回复
学习中...
Edelweissobject 2003-12-18
  • 打赏
  • 举报
回复
楼主我觉得你可以把自定义的类从一个类派生出来,由于基类已经具有响应鼠标(在窗口或类对象本身 上移动,单击等)消息,如果你有特殊要求就重写基类事件就可了!象下面的例子:
public class Show:System.Windows.System.Windows.Forms.Panel//基类
{
//string strContext;
public Show()
{

}
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
// TODO: 添加 Show.OnMouseMove 实现
base.OnMouseMove (e);
}


protected override void OnClick(EventArgs e)
{
// TODO: 添加 Show.OnClick 实现
base.OnClick (e);
}
}
DangerousWang 2003-12-18
  • 打赏
  • 举报
回复
学习
jjjy 2003-12-18
  • 打赏
  • 举报
回复
类似与VC中的
响应鼠标(在窗口或类对象本身 上移动,单击等)消息?
在自己定义的类库中该怎么做?
可以实现吗




wangj2001 2003-12-18
  • 打赏
  • 举报
回复
namespace ClassLibrary.Hardware
{
// Using C# and Win32API

public class Mouse
{
internal const byte SM_MOUSEPRESENT = 19;
internal const byte SM_CMOUSEBUTTONS = 43;
internal const byte SM_MOUSEWHEELPRESENT = 75;

internal struct POINTAPI
{
internal int x;
internal int y;
}

internal struct RECT
{
internal int left ;
internal int top ;
internal int right ;
internal int bottom ;
}

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SwapMouseButton")]
internal extern static int SwapMouseButton ( int bSwap );

[System.Runtime.InteropServices.DllImport("user32" , EntryPoint="ClipCursor")]
internal extern static int ClipCursor(ref RECT lpRect);

[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );

[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="ShowCursor")]
internal extern static bool ShowCursor ( bool bShow ) ;

[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint = "EnableWindow" )]
internal extern static int EnableWindow( int hwnd , int fEnable );

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetWindowRect")]
internal extern static int GetWindowRect( int hwnd , ref RECT lpRect ) ;

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetCursorPos")]
internal extern static int SetCursorPos ( int x , int y ) ;

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetSystemMetrics")]
internal extern static int GetSystemMetrics( int nIndex );

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="SetDoubleClickTime")]
internal extern static int SetDoubleClickTime ( int wCount );

[System.Runtime.InteropServices.DllImport("user32.dll" , EntryPoint="GetDoubleClickTime")]
internal extern static int GetDoubleClickTime() ;

[System.Runtime.InteropServices.DllImport("kernel32.DLL", EntryPoint="Sleep")]
internal extern static void Sleep ( int dwMilliseconds ) ;

//得到鼠标相对与全屏的坐标,不是相对与你的Form的,且与你的分辨率有关系

public static int FullScreenPosition_X
{
get
{
POINTAPI _POINTAPI = new POINTAPI();

GetCursorPos ( ref _POINTAPI );

return _POINTAPI.x;
}
}

public static int FullScreenPosition_Y
{
get
{
POINTAPI _POINTAPI = new POINTAPI();

GetCursorPos ( ref _POINTAPI );

return _POINTAPI.y;
}
}

// 隐藏 显示 鼠标

public static void Hide()
{
ShowCursor( false ) ;
}

public static void Show()
{
ShowCursor( true ) ;
}

// 将鼠标锁定在你的Form里 不过你得将你的Form先锁了,Form Resize 就失效了

public static void Lock( System.Windows.Forms.Form ObjectForm )
{
RECT _FormRect = new RECT ();

GetWindowRect( ObjectForm.Handle.ToInt32() , ref _FormRect );

ClipCursor( ref _FormRect );
}

public static void UnLock()
{
RECT _ScreenRect = new RECT ();

_ScreenRect.top = 0;
_ScreenRect.left = 0;
_ScreenRect.bottom = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Bottom;
_ScreenRect.right = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right;

ClipCursor( ref _ScreenRect );
}

// 鼠标失效,不过失效的好像不只是鼠标,小心哦

public static void Disable( System.Windows.Forms.Form ObjectForm )
{
EnableWindow( ObjectForm.Handle.ToInt32() , 0 ) ;
}

public static void Enable( System.Windows.Forms.Form ObjectForm )
{
EnableWindow( ObjectForm.Handle.ToInt32() , 1 ) ;
}

// 鼠标自己移动 很想动画哦 参数是2个控件的handle
// 看这个方法前,先用凉水擦把脸。。。 反正我写的时候 头晕

public static void Move ( int From_Handle_ToInt32 , int To_Handle_ToInt32 )
{
RECT rectFrom = new RECT () ;
RECT rectTo = new RECT () ;

int i ;

GetWindowRect( From_Handle_ToInt32 , ref rectFrom ) ;
GetWindowRect( To_Handle_ToInt32 , ref rectTo ) ;

if ( ( rectFrom.left + rectFrom.right ) / 2 - ( rectTo.left + rectTo.right ) / 2 > 0 )
{
for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i >= ( rectTo.left + rectTo.right ) / 2 ; i-- )
{
SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;
Sleep ( 1 ) ;
}
}
else
{
for ( i = ( rectFrom.left + rectFrom.right ) / 2 ; i <= ( rectTo.left + rectTo.right ) / 2 ; i++ )
{
SetCursorPos ( i , ( rectFrom.top + rectFrom.bottom ) / 2) ;
Sleep ( 1 ) ;
}
}

if ( ( rectFrom.top + rectFrom.bottom ) / 2 - ( rectTo.top + rectTo.bottom ) / 2 > 0 )
{
for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i >= ( rectTo.top + rectTo.bottom ) / 2 ; i-- )
{
SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;
Sleep ( 1 ) ;
}
}
else
{
for ( i = ( rectFrom.top + rectFrom.bottom ) / 2 ; i <= ( rectTo.top + rectTo.bottom ) / 2 ; i++ )
{
SetCursorPos ( ( rectTo.left + rectTo.right ) / 2 , i ) ;
Sleep ( 1 ) ;
}
}
}

// 得到你的鼠标类型

public static string Type
{
get
{
if ( GetSystemMetrics( SM_MOUSEPRESENT ) == 0 )
{
return "本计算机尚未安装鼠标" ;
}
else
{
if ( GetSystemMetrics( SM_MOUSEWHEELPRESENT ) != 0 )
{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键滚轮鼠标" ;
}
else
{
return GetSystemMetrics( SM_CMOUSEBUTTONS ) + "键鼠标" ;
}
}
}
}

// 设置鼠标双击时间

public static void DoubleClickTime_Set( int MouseDoubleClickTime )
{
SetDoubleClickTime( MouseDoubleClickTime );
}

public static string DoubleClickTime_Get()
{
return GetDoubleClickTime().ToString() ;
}

// 设置鼠标默认主键 我是没有见过谁左手用鼠标

public static void DefaultRightButton()
{
SwapMouseButton ( 1 ) ;
}

public static void DefaultLeftButton()
{
SwapMouseButton ( 0 ) ;
}
}
}
polarlm 2003-12-18
  • 打赏
  • 举报
回复
还是不太懂楼主的意思

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧