Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

[C++] 全ウインドウ(UWP/DESKTOP)を最小化する/元に戻す

Posted at

処理の流れ

画面に出てるウインドウを最小化⇔元に戻すときの流れ

EnumWindowsでウインドウをとってくる
以下はそのcallbackl内でやる

GetWindowTextLength()で、ウインドウのタイトルをとってくる
タイトルの長さが0未満のときはなにもしない

IsWindowVisible()でウィンドウが可視かどうか調べて、表示してないのものを除外

GetWindowText()でウィンドウのタイトルを取得する

GetClassName()でウィンドウのクラス名を取得する

クラス名がWindows.UI.Core.CoreWindowでもなくApplicationFrameWindowでもなければ
→デスクトップアプリのウインドウ。

クラス名がApplicationFrameWindowであれば、
→UWPのウインドウ。

それらのハンドルをとってきて、ShowWindowとかをしてやれば、ウインドウを最小化とか元に戻すとかができる。

サンプルコード

#include <vector>
#include "framework.h"
#include "WindowsProject1.h"
#include "resource.h"
#include <string>

HINSTANCE hInst;
std::vector<HWND> _desktopWindowList;
std::vector<HWND> _uwpWindowList;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK MyDlgProc(HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lp);
void OutputLog(std::wstring logtxt);
void MinimizeDesktopWindows();
void RestoreDesktopWindows();

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    hInst = hInstance;

    DialogBox(hInst, L"MyTestDlgBase_Main", NULL, (DLGPROC)MyDlgProc);

    return (int)0;
}

// ダイアログプロシージャ
BOOL CALLBACK MyDlgProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg) {
    case WM_COMMAND:
        switch (LOWORD(wp)) {
        case IDOK:
            EndDialog(hDlg, IDOK);
            return TRUE;
        case IDCANCEL:
            EndDialog(hDlg, IDCANCEL);
            return TRUE;
        case IDC_MINIMIZE_WINDOWS:
            MinimizeDesktopWindows();
            break;
        case IDC_RESTORE_WINDOWS:
            RestoreDesktopWindows();
            break;
        }
        return FALSE;
    }
    return FALSE;
}

void MinimizeDesktopWindows()
{
    _desktopWindowList.clear();
    _uwpWindowList.clear();

    EnumWindows(EnumWindowsProc, NULL);

    for (auto i = 0 ; i < _desktopWindowList.size(); i++)
    {
        ShowWindow(_desktopWindowList[i], SW_MINIMIZE);
    }
    for (auto i = 0; i < _uwpWindowList.size(); i++)
    {
        ShowWindow(_uwpWindowList[i], SW_MINIMIZE);
    }
}

void RestoreDesktopWindows()
{
    _desktopWindowList.clear();
    _uwpWindowList.clear();

    EnumWindows(EnumWindowsProc, NULL);

    for (auto i = 0; i < _desktopWindowList.size(); i++)
    {
        ShowWindow(_desktopWindowList[i], SW_RESTORE);
    }
    for (auto i = 0; i < _uwpWindowList.size(); i++)
    {
        ShowWindow(_uwpWindowList[i], SW_RESTORE);
    }
}

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lp)
{
    auto textLen = GetWindowTextLength(hwnd);

    if (textLen > 0)
    {
        if (!IsWindowVisible(hwnd)) return TRUE;
        
        WCHAR buf[256] = { 0 };
        GetWindowText(hwnd, buf, sizeof(buf) / sizeof(buf[0]));
        auto title = std::wstring(buf);

        //WCHAR className[256] = { 0 };
        GetClassName(hwnd, buf, sizeof(buf) / sizeof(buf[0]));
        auto titlclassName = std::wstring(buf);

        if (titlclassName != L"Windows.UI.Core.CoreWindow"
            && titlclassName != L"ApplicationFrameWindow")
        {
            // デスクトップのウインドウ
            _desktopWindowList.push_back(hwnd);
            OutputLog(std::wstring(L"desktop : ") + title);
        }
        else if (titlclassName == L"ApplicationFrameWindow")
        {
            // UWPアプリのウインドウ
            _uwpWindowList.push_back(hwnd);
            OutputLog(std::wstring(L"uwp : ") + title);
        }
    }
    return TRUE;
}

void OutputLog(std::wstring logtxt)
{
    OutputDebugString(logtxt.c_str());
    OutputDebugString(L"\r\n");
}

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?