原文:C#或者WPF中让某个窗体置顶

前记:在工作中有个需求,要求不管到那个界面,我必须让一个浮动条(其实是个窗体)置顶。

我用wpf,因为有之前有好几个界面已经设置成topmost了,所以在这几个界面,我的浮动条会被遮挡。为了始终让浮动条在最顶端,我写了个简单的工具类。在前面已经设置成topmost的窗体的Window_Loaded中调用这个工具类里的方法实现了始终让浮动条置顶。

工具类代码如下:

public class TopMostTool
{
public static int SW_SHOW = 5;
public static int SW_NORMAL = 1;
public static int SW_MAX = 3;
public static int SW_HIDE = 0;
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //窗体置顶
public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); //取消窗体置顶
public const uint SWP_NOMOVE = 0x0002; //不调整窗体位置
public const uint SWP_NOSIZE = 0x0001; //不调整窗体大小
public bool isFirst = true; [DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); [DllImport("user32.dll", EntryPoint = "ShowWindow")]
public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")]
FindWindow(string lpClassName,string lpWindowName); /// <summary>
/// 在外面的方法中掉用这个方法就可以让浮动条(CustomBar)始终置顶
/// CustomBar是我的程序中需要置顶的窗体的名字,你们可以根据需要传入不同的值
/// </summary>
public static void setTopCustomBar(){
IntPtr CustomBar = FindWindow(null,"CustomBar"); //CustomBar是我的程序中需要置顶的窗体的名字
if(CustomBar!=null){
SetWindowPos(CustomBar, MainWindow.HWND_TOPMOST, 0, 0, 0, 0, MainWindow.SWP_NOMOVE | MainWindow.SWP_NOSIZE);
}
}
}

这个类里的几个方法详解

SetWindowPos方法详解请戳这里

ShowWindow方法详解请戳这里

FindWindow方法详解请戳这里

写的比较粗糙,就当给自己做笔记!

C#或者WPF中让某个窗体置顶的更多相关文章

  1. Creating Dialogbased Windows Application (3) / 创建基于对话框的Windows应用程序(三)Checkbox的应用、窗体置顶、设置图标 / VC++, Windows

    创建基于对话框的Windows应用程序(三) —— Checkbox的应用.窗体置顶.设置图标 上一节创建的窗体应用程序中,我们用到了Button和StaticText这两个控件.这一节中我们将学习使 ...

  2. winform窗体置顶

    winform窗体置顶 金刚 winform 置顶 今天做了一个winform小工具.需要设置置顶功能. 网上找了下,发现百度真的很垃圾... 还是必应靠谱些. 找到一个可以链接. https://s ...

  3. C#控件、窗体置顶

    //控件置于顶层和底层 panel.BringToFront();//置于顶层 panel.SendToBack();//置于底层 //窗体置顶 TopMost = true;

  4. Delphi窗体置顶及失去焦点后取得焦点

    unit u_FrmTopMostActive; interface uses Winapi.Windows; implementation // 窗体置顶 procedure SetXwForegr ...

  5. 019 [工具软件]窗体置顶 DeskPins

    DeskPins:Windows下将任何窗体置顶的工具 官方主页:https://efotinis.neocities.org/deskpins/index.html 官方下载的是一个exe安装包,用 ...

  6. WPF:窗体置顶

    1.设置窗体TopMost属性 private DispatcherTimer timer; public Window1() { InitializeComponent(); Loaded += n ...

  7. WPF主窗体调用 User32的SetWindowPos 设置窗体置顶会导致与其他窗体抢夺焦点的问题

    最近发现:自己开发的窗体应用,在二级弹窗或者提示框弹出的时候,交替点击窗体和窗体外(相当于窗体交替的获取焦点和失去焦点),都会导致其他的应用一闪一闪的. 经过排查,是由于该窗体由于部分因素考虑,用了  ...

  8. 让WPF的Popup不总置顶的解决方案

    使用WPF的Popup的时候会发现有一个问题,它总是会置顶,只要Popup的StayOpen不设置为False,它就一直呆在最顶端,挡住其他的窗口. 解决方案是继承Popup重新定义控件PopupEx ...

  9. Python tkinter 副窗体置顶和取消置顶

    root = Tk() #置顶root.wm_attributes('-topmost',1) #取消置顶 root.wm_attributes('-topmost',0)

随机推荐

  1. 【BZOJ 4516】生成魔咒

    [链接]h在这里写链接 [题意]     [Description]         给你n(n<=10^9)个数字,把它们依次,一个一个地添加在空串S的后面.         要求每添加一次之 ...

  2. 字符的输入和输出即:getchar和putchar

    #include <stdio.h> int main(int argc, const char * argv[]) { putchar(getchar());//这种方式就是输入一个字符 ...

  3. linux 查看 *.a *.so 符号表(zt)

    objdump -tT libName.so | grep symbel symbolName nm -D libName.so | grep symbel symbolName 很多非常熟悉wind ...

  4. Eclipse 使用技巧之 ---- 查看本类调用和被调用列表

    当工程复杂的情况下,用眼睛去人工查看调用情况是很费力也没必要的.我们需要用 Eclipse 来做这点. (1)    我们查看本类调用他类情况可以直接看 import . (2)    如果要查看本类 ...

  5. PatentTips - Enhanced I/O Performance in a Multi-Processor System Via Interrupt Affinity Schemes

    BACKGROUND OF THE INVENTION This relates to Input/Output (I/O) performance in a host system having m ...

  6. 使用 Google Guava 美化你的 Java 代码:1~4 【转】

    文章转载自:http://my.oschina.net/leejun2005/blog/172328 1.使用Google Collections,Guava,static imports编写漂亮代码 ...

  7. [ES7] Handle Errors in Asynchronous Functions

    This lesson shows how regular control flow statements such as try/catch blocks can be used to proper ...

  8. maven打包到本地库

    mvn install:install-file -DgroupId=com.alipay -DartifactId=com.alipay.core -Dversion=20180104135026 ...

  9. [Angular Directive] 3. Handle Events with Angular 2 Directives

    A @Directive can also listen to events on their host element using @HostListener. This allows you to ...

  10. Qt on Android: http下载与Json解析

    百度提供有查询 ip 归属地的开放接口,当你在搜索框中输入一个 ip 地址进行搜索,就会打开由 ip138 提供的百度框应用,你能够在框内直接输入 ip 地址查询.我查看了页面请求,提取出查询 ip ...