/* * AddWmHandler */ /// <summary> /// </summary> /// <param name="wmId"></param> /// <param name="wmHandler"></param> public void AddWmHandler(int wmId, NuGenWmHandler wmHandler) { if (this.MessageMap.ContainsKey(wmId)) { NuGenWmHandler @delegate = this.MessageMap[wmId]; this.MessageMap[wmId] = (NuGenWmHandler)NuGenWmHandler.Combine(@delegate, wmHandler); } else { this.MessageMap.Add(wmId, wmHandler); } }
/* * RemoveWmHandler */ /// <summary> /// </summary> /// <param name="wmId"></param> /// <param name="wmHandler"></param> public void RemoveWmHandler(int wmId, NuGenWmHandler wmHandler) { if (this.MessageMap.ContainsKey(wmId)) { NuGenWmHandler @delegate = this.MessageMap[wmId]; if (@delegate != null) { this.MessageMap[wmId] = (NuGenWmHandler)NuGenWmHandler.Remove(@delegate, wmHandler); } } }
public void AddWmHandlerTest() { _wmHandlerList.AddWmHandler(WM_LBUTTONDOWN, this.StubWmHandler); Assert.AreEqual(1, _wmHandlerList.Count); NuGenWmHandler wmHandler = _wmHandlerList[WM_LBUTTONDOWN]; Assert.IsNotNull(wmHandler); _wmHandlerList.AddWmHandler(WM_LBUTTONDOWN, this.StubWmHandler2); Assert.AreEqual(1, _wmHandlerList.Count); }
protected override void WndProc(ref Message m) { NuGenWmHandler wmHandler = this.MessageMap[m.Msg]; if (wmHandler != null) { wmHandler(ref m, base.WndProc); } else { base.WndProc(ref m); } }
public void InvokeHandlerAfterRemoveTest() { WmSinkRemove wmSink = new WmSinkRemove(_wmHandlerList); wmSink.ExpectedWmHandlerInvokeCount = 0; wmSink.ExpectedWmHandler2InvokeCount = 1; NuGenWmHandler wmHandler = _wmHandlerList[WM_LBUTTONDOWN]; wmHandler(ref _m, this.BaseWndProc); wmSink.Verify(); }
/// <summary> /// Looks for methods marked with <see cref="NuGenWmHandlerAttribute"/> and initializes a message map /// of type <see cref="NuGenWmHandlerList"/>. /// </summary> /// /// <param name="messageProcessor"> /// Specifies the methods marked with <see cref="NuGenWmHandlerAttribute"/> attribute(s) /// to handle Windows messages. /// </param> /// /// <exception cref="ArgumentNullException"> /// <paramref name="messageProcessor"/> is <see langword="null"/>. /// </exception> /// /// <exception cref="NuGenWmHandlerSignatureException"> /// <paramref name="messageProcessor"/> provides a method marked with <see cref="NuGenWmHandlerAttribute"/> /// but not compatible with the <see cref="NuGenWmHandler"/> delegate. /// </exception> public NuGenWmHandlerList BuildMessageMap(INuGenMessageProcessor messageProcessor) { if (messageProcessor == null) { throw new ArgumentNullException("messageProcessor"); } NuGenWmHandlerList handlerList = new NuGenWmHandlerList(); MethodInfo[] methods = messageProcessor.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int i = 0; i < methods.Length; i++) { MethodInfo method = methods[i]; object[] attributes = method.GetCustomAttributes(typeof(NuGenWmHandlerAttribute), false); if (attributes != null) { foreach (NuGenWmHandlerAttribute attribute in attributes) { string methodName = method.Name; NuGenWmHandler wmHandler = null; try { wmHandler = (NuGenWmHandler)NuGenWmHandler.CreateDelegate( typeof(NuGenWmHandler), messageProcessor, methodName, false, true ); } catch (ArgumentException) { throw new NuGenWmHandlerSignatureException(methodName); } handlerList.AddWmHandler( attribute.WmId, wmHandler ); } } } return(handlerList); }