您的位置:首页 > 其它

WPF 键盘全局接收消息

2015-10-19 18:51 465 查看
1、==========================================================================

c#中怎样禁用鼠标左键的使用,其实我们可以通过ImessageFilter接口下的PreFilterMessage方法、Application类的AddMessageFilter方法,RemoveMessageFilter方法和Message结构的Msg属性来禁用鼠标左键。Message结构包装Windows发送的消息,可使用该结构包装消息,并将其分配给窗口过程以进行调度,还可以使用该结构获取系统向应用程序或控件发送的关于某个消息的信息。
使用PreFilterMessage方法在调度消息之前将其筛选出来。语法格式如下:

Bool PreFilterMessage(refMessage m)

参数说明:

m:要调度的消息,无法修改此消息。

返回值:如果筛选消息并禁止消息被调度,则为True;如果允许消息继续到达下一个筛选器或控件,则为False。使用AddMessageFilter方法添加消息筛选器以便在向目标传送Windows消息时监视这些消息。使RemoveMessageFilter 从应用程序的消息泵移除一个消息筛选器。

下面给出大家一下主要代码:

public bool? ShowDialog()
{

try
{
this._showingAsDialog = true;
this.Show();
}
catch
{

}
finally
{
this._showingAsDialog = false;
}

}

public void Show()
{
this.VerifyContextAndObjectState();
this.VerifyCanShow();
this.VerifyNotClosing();
this.VerifyConsistencyWithAllowsTransparency();
this.UpdateVisibilityProperty(Visibility.Visible);
this.ShowHelper(BooleanBoxes.TrueBox);
}

private object ShowHelper(object booleanBox)
{
if (!this._disposed)
{

if (this._showingAsDialog && this._isVisible)
{
try
{
ComponentDispatcher.PushModal();
this._dispatcherFrame = new DispatcherFrame();
Dispatcher.PushFrame(this._dispatcherFrame);
}
finally
{
ComponentDispatcher.PopModal();
}
}
}
return null;
}

public class MyDialog : Window
{
private DispatcherFrame _dispatcherFrame;
private FrameworkElement _container = null;
private double _lastLeft = 0.0;
private double _lastTop = 0.0;

public void Open(FrameworkElement container)
{
if (container != null)
{
_container = container;
// 通过禁用来模拟模态的对话框
_container.IsEnabled = false;
// 保持总在最上
this.Owner = GetOwnerWindow(container);
if (this.Owner != null)
{
this.Owner.Closing += new System.ComponentModel.CancelEventHandler(Owner_Closing);
}
// 通过监听容器的Loaded和Unloaded来显示/隐藏窗口
_container.Loaded += new RoutedEventHandler(Container_Loaded);
_container.Unloaded += new RoutedEventHandler(Container_Unloaded);
}
this.Show();
try
{
ComponentDispatcher.PushModal();
_dispatcherFrame = new DispatcherFrame(true);
Dispatcher.PushFrame(_dispatcherFrame);
}
finally
{
ComponentDispatcher.PopModal();
}
}

// 在Owner关闭的时候关闭
private void Owner_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Close();
}

private void Container_Unloaded(object sender, RoutedEventArgs e)
{
// 只能通过这种方式隐藏,而不能通过Visiblity = Visibility.Collapsed,否则会失效
_lastLeft = this.Left;
_lastTop = this.Top;
this.Left = -10000;
this.Top = -10000;
}

private void Container_Loaded(object sender, RoutedEventArgs e)
{
this.Left = _lastLeft;
this.Top = _lastTop;
}

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (_container != null)
{
_container.Loaded -= Container_Loaded;
_container.Unloaded -= Container_Unloaded;
}
if (this.Owner != null)
{
this.Owner.Closing -= Owner_Closing;
}
}

protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// 当关闭终止消息循环
if (_dispatcherFrame != null)
{
_dispatcherFrame.Continue = false;
}
// 这里必须强制调用一下
// 否则出现同时点开多个窗口时,只有一个窗口让代码继续
ComponentDispatcher.PopModal();
if (_container != null)
{
_container.IsEnabled = true;
}
}

private Window GetOwnerWindow(FrameworkElement source)
{
var parent = VisualTreeHelper.GetParent(source) as FrameworkElement;
if (parent == null)
return null;
var win = parent as Window;
return
win != null ?
parent as Window :
GetOwnerWindow(parent);
}
}

var btn = sender as Button;
var dialog = new MyDialog
{
Width = 300,
Height = 200,
Content = btn.Tag,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
var border = this.FindName((string)btn.Tag) as Border;
dialog.Open(border);
// 在窗口关闭之前不会执行
MessageBox.Show(string.Format("{0}上弹出对话框结束!", btn.Tag));

代码下载

/Files/RMay/TryMessageBox.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: