windows判断窗口是否置顶设置取消置顶设置前景

Posted ningto.com

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows判断窗口是否置顶设置取消置顶设置前景相关的知识,希望对你有一定的参考价值。

判断窗口是否置顶

    bool isWndTopMost(HWND hwnd) 
        return GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST;
    

设置和取消置顶

    void showWndTopMost(HWND hwnd)
        RECT rect;
        GetWindowRect(hwnd, &rect);
        SetWindowPos(hwnd, HWND_TOPMOST, rect.left, rect.top, abs(rect.right - rect.left), abs(rect.bottom - rect.top), SWP_SHOWWINDOW);
    
    void cancelTopMost(HWND hwnd)
        RECT rect;
        GetWindowRect(hwnd, &rect);
        SetWindowPos(hwnd, HWND_NOTOPMOST, rect.left, rect.top, abs(rect.right - rect.left), abs(rect.bottom - rect.top), SWP_SHOWWINDOW);
    

设置窗口前景

    void showWndTop(HWND hWnd)
        if (!::IsWindow(hWnd))
            return;
        if (!::SetForegroundWindow(hWnd)) 
            WinParameter winParameter;
            ::SetForegroundWindow(hWnd)
        
    

WinParameter是为了解决某些特殊情况下设置失败的问题

#ifndef WINPARAMETER_H
#define WINPARAMETER_H

class WinParameter 

public:
    WinParameter();
    ~WinParameter();

private:
    unsigned long  m_lockTimeOut;
;

#endif // WINPARAMETER_H

///////////////////////////////////////////////////////
#include "winparameter.h"
#include <windows.h>
WinParameter::WinParameter()

    m_lockTimeOut = 0;
    HWND  hCurrWnd = ::GetForegroundWindow();
    unsigned long dwThisTID = ::GetCurrentThreadId();
    unsigned long dwCurrTID = ::GetWindowThreadProcessId(hCurrWnd,0);

    //we need to bypass some limitations from Microsoft :)
    if(dwThisTID != dwCurrTID)
    
        ::AttachThreadInput(dwThisTID, dwCurrTID, TRUE);

        ::SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT,0,&m_lockTimeOut,0);

        ::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,0,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
        ::AllowSetForegroundWindow(ASFW_ANY);
    



WinParameter::~WinParameter()

    HWND  hCurrWnd = ::GetForegroundWindow();
    unsigned long dwThisTID = ::GetCurrentThreadId();
    unsigned long dwCurrTID = ::GetWindowThreadProcessId(hCurrWnd,0);
    if(dwThisTID != dwCurrTID)
    

        ::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,(PVOID)m_lockTimeOut,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);

        ::AttachThreadInput(dwThisTID, dwCurrTID, FALSE);
    

以上是关于windows判断窗口是否置顶设置取消置顶设置前景的主要内容,如果未能解决你的问题,请参考以下文章

怎么用c语言在windows下弹出一个置顶的窗口?

Windows Terminal 取消窗口置顶

易语言如何使某软件窗口置顶?

VB怎么使外调程序置顶

google浏览器总在最前面,怎么取消总是置顶

MFC 如何使窗口 置顶