none
C# 如何让某个类中的函数执行前执行自定义的函数? RRS feed

  • 问题

  • 要被拦截的类声明:

    internal class TransparentBehavior: System.Windows.Forms.Design.Behavior.Behavior
    {
        public override void OnDragOver(Glyph g, DragEventArgs e);
    }

    如何让这个类在OnDragOver执行前执行自定义的函数?

    尽量不使用第三方库

    (或者有什么办法能直接继承这个

    internal class TransparentBehavior

    因为系统的Serveice会判断

    is TransparentBehavior

    • 已编辑 Troy_Zhang 2019年4月11日 7:34 排版
    2019年4月11日 7:23

答案


  • Hi  Troy_Zhang,

    根据你进一步的信息,我并没有发现比较合适的方法。也许你可以尝试找找一些三方库来实现你的需求。

    Best Regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Troy_Zhang 2019年4月23日 4:13
    2019年4月15日 7:02
    版主

全部回复

  • Hi    Troy_Zhang,

    我建议你将你尝试的代码或者要实现的代码一并贴出来,这样方便大家分析。

    Best Regards

    Yong Lu

      

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年4月12日 7:28
    版主
  • 这是BehaviorService类中的一段代码,请注意return (hitTestedGlyph.Behavior is ControlDesigner.TransparentBehavior);如果你不是ControlDesigner.TransparentBehavior,就不会获得BehaviorService提供的功能,但是ControlDesigner.TransparentBehavior是 Internal的,你无法继承它

    private bool PropagateHitTest(Point pt)
    		{
    			for (int i = adorners.Count - 1; i >= 0; i--)
    			{
    
    				if (!adorners[i].Enabled)
    				{
    					continue;
    				}
    
    				for (int j = 0; j < adorners[i].Glyphs.Count; j++)
    				{
    					Cursor hitTestCursor = adorners[i].Glyphs[j].GetHitTest(pt);
    					if (hitTestCursor != null)
    					{
    						// InvokeMouseEnterGlyph will cause the selection to change, which 
    						// might change the number of glyphs, so we need to remember the new glyph 
    						// before calling InvokeMouseEnterLeave. VSWhidbey #396611
    						Glyph newGlyph = adorners[i].Glyphs[j];
    
    						//with a valid hit test, fire enter/leave events
    						//
    						InvokeMouseEnterLeave(hitTestedGlyph, newGlyph);
    						if (validDragArgs == null)
    						{
    							//if we're not dragging, set the appropriate cursor 
    							// 
    							SetAppropriateCursor(hitTestCursor);
    						}
    
    						hitTestedGlyph = newGlyph;
    
    						//return true if we hit on a transparentBehavior, otherwise false 
    						return (hitTestedGlyph.Behavior is ControlDesigner.TransparentBehavior);
    					}
    				}
    
    			}
    
    			InvokeMouseEnterLeave(hitTestedGlyph, null);
    			if (validDragArgs == null)
    			{
    				Cursor cursor = Cursors.Default;
    				if ((behaviorStack != null) && (behaviorStack.Count > 0))
    				{
    					Behavior behavior = behaviorStack[0] as Behavior;
    					if (behavior != null)
    					{
    						cursor = behavior.Cursor;
    					}
    				}
    				SetAppropriateCursor(cursor);
    			}
    			hitTestedGlyph = null;
    			return true;//Returning false will cause the transparent window to return HTCLIENT when handling WM_NCHITTEST, thus blocking underline window to receive mouse events.
    
    		}

    2019年4月12日 9:47
  • 如果不能有其他方法继承,那么如何拦截这个类的函数,执行自己的方法呢?如果不拦截的话

    designer.Control.RectangleToScreen(designer.Control.ClientRectangle);它返回的是客户区的大小,可我想返回的是窗口的大小

    internal class TransparentBehavior : System.Windows.Forms.Design.Behavior.Behavior
    {
    	public override void OnDragEnter(Glyph g, DragEventArgs e)
    	{
    		if (designer != null && designer.Control != null)
    		{
    			controlRect = designer.Control.RectangleToScreen(designer.Control.ClientRectangle);
    		}
    
    		designer.OnDragEnter(e);
    	}
    }

    2019年4月12日 9:51

  • Hi  Troy_Zhang,

    根据你进一步的信息,我并没有发现比较合适的方法。也许你可以尝试找找一些三方库来实现你的需求。

    Best Regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Troy_Zhang 2019年4月23日 4:13
    2019年4月15日 7:02
    版主