WPF 设置windows窗体在最前端 top window / Foreground Window

需要实现的功能是当鼠标滑过WPF窗体的textbox时,获取textbox的输入焦点。

步骤:

1.需要把此WPF的Topmost属性设置成true。

2.引用 win32 API 函数:

ExpandedBlockStart.gif View Code
        [DllImport( " user32.dll " )]
        
private   static   extern   bool  SetForegroundWindow(IntPtr hWnd);

        [DllImport(
" user32.dll " )]
        
private   static   extern  IntPtr SetFocus(IntPtr hWnd);

2.添加Textbox的MouseEnter事件

ExpandedBlockStart.gif View Code
private   void  tbWord_MouseEnter( object  sender, MouseEventArgs e)
        {
            HwndSource source 
=  (HwndSource)PresentationSource.FromVisual( this );
            IntPtr handle 
=  source.Handle;
           
            
bool  b1  =  BringWindowToTop(handle);
            System.Threading.Thread.Sleep(
300 );

            
bool  b  =  SetForegroundWindow(handle);
            System.Threading.Thread.Sleep(
100 );
  
            tbWord.Focus();
            tbWord.SelectAll();
        }

 程序演示: http://www.cnblogs.com/JimmyXiao/archive/2011/03/17/1987561.html

 

转载于:https://www.cnblogs.com/JimmyXiao/archive/2011/04/29/2032960.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要创建C# WPFWindows应用程序,可以按照以下步骤进行操作。首先,在Visual Studio中选择“文件”->“新建”->“项目”命令,弹出项目对话框。在该对话框中选择“WPF应用程序”,并设置项目名称、位置等信息,然后点击“确定”按钮。接下来,在项目文件夹中会有一个默认的程序MainWindow.xaml和MainWindow.xaml.cs。在MainWindow.xaml中,可以使用XAML语言来设计的布局和界面元素。例如,可以使用StackPanel来放置按钮和文本块等控件。在MainWindow.xaml.cs文件中,可以编写C#代码来处理的事件和逻辑。例如,可以在加载时设置控件的属性,或者在按钮点击事件中执行相应的操作。最后,可以通过运行应用程序来查看的效果。 #### 引用[.reference_title] - *1* *2* [C#创建Windows应用程序](https://blog.csdn.net/weixin_50610118/article/details/113002325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C# WPF通过WindowChrome自定义](https://blog.csdn.net/sD7O95O/article/details/127218714)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值