C# SetWindowPos无法设置窗口";“永远在最上面”;

C# SetWindowPos无法设置窗口";“永远在最上面”;,c#,.net,windows,visual-studio-2013,C#,.net,Windows,Visual Studio 2013,我正在尝试制作一个小程序,这样我就可以让我的vlc播放器始终处于顶部,这样我就可以在做其他事情的同时在屏幕上看一些电影 我是在这里被另一个人发现的。我的代码如下所示 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System; using System.Diagnostics; using Syste

我正在尝试制作一个小程序,这样我就可以让我的vlc播放器始终处于顶部,这样我就可以在做其他事情的同时在屏幕上看一些电影

我是在这里被另一个人发现的。我的代码如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Diagnostics;
using System.Windows.Shapes;
using System.Runtime.InteropServices;

namespace WpfApplication1
{
    public class ProcessManager
    {
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, uint windowStyle);

        [DllImport("user32.dll")]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        public ProcessManager()
        {
            string processName = "vlc";
            //SearchProcessAndModifyState(processName);
        }

        public void SearchProcessAndModifyState(string targetProcessName)
        {
            Process specifiedProcess = null;
            Process[] processes = Process.GetProcesses();
            for (int i = 0; i < processes.Length; i++)
            {
                Process process = processes[i];
                if (process.ProcessName == targetProcessName)
                {
                    specifiedProcess = process;
                    break;
                }
            }
            if (specifiedProcess != null)
            {
                ProcessManager.ShowWindow(specifiedProcess.MainWindowHandle, 1u);
                ProcessManager.SetWindowPos(specifiedProcess.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3u);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用制度;
使用系统诊断;
使用System.Windows.Shapes;
使用System.Runtime.InteropServices;
命名空间WpfApplication1
{
公共类进程管理器
{
[DllImport(“user32.dll”)]
私有静态外部布尔显示窗口(IntPtr hWnd、uint WindowsStyle);
[DllImport(“user32.dll”)]
私有静态外部bool SetWindowPos(IntPtr hWnd、IntPtr hwninsertafter、intx、inty、intcx、intcy、uint uFlags);
公共进程管理器()
{
字符串processName=“vlc”;
//SearchProcessAndModifyState(processName);
}
public void SearchProcessAndModifyState(字符串targetProcessName)
{
进程指定的进程=null;
Process[]processs=Process.getprocesss();
for(int i=0;i

现在当我运行这个程序时,vlc窗口会出现,但它不会停留在顶部。所以我想ShowWindow可以工作,但是SetWindowPos不能。我通过文件->新建->项目…->Visual Studio 2013中的Visual C#->Windows->WPF应用程序,我使用的是Windows 8.1。有人知道吗?

我也有同样的问题。在调用SetWindowPos之前,我插入了以下内容来解决此问题:

const int GWL_EXSTYLE = -20;
const int WS_EX_TOPMOST = 8;

var extStyle = GetWindowLongPtr(hWnd, GWL_EXSTYLE).ToInt64();
extStyle |= WS_EX_TOPMOST;
SetWindowLongPtr(hWnd, GWL_EXSTYLE, new IntPtr(extStyle));

(GetWindowLongPtr和SetWindowLongPtr都可以在pinvoke.net上查找-从我手机的内存中发布,或者会给出一个更完整的答案!)

您没有检查错误。api调用返回什么?同时,运行此代码的程序在执行此操作时是否是前台窗口?您确实意识到VLC已经有了这样的选项,对吗?如何使其工作。。我有并且我可以看到如何为
IntPtr hWnd
获取一个实际参数(参数),但是我看不到如何为
HandRef hWnd
获取一个实际参数(参数),这正是SetWindowLongPtr使用的。我看到可以使用
HandleRef hr=new HandleRef(null,iptr)