"GetForegroundWindow: identifier not found" 在 visual studio 2015

"GetForegroundWindow: identifier not found" in visual studio 2015

我正在 visual studio 2015 年使用 C++ 开发一个 windows 应用程序。我需要 GetForegroundWindow()GetWindowText() 到 return 当前关注的应用程序。然而,它说 GetForegroundWindow()GetWindowText() 是未定义的,即使我已经包括了 "windows.h"。我尝试转到 GetForegroundWindow() 的定义,它引导我找到 "WinUser.h",所以我也包含了 "WinUser.h"。但还是没办法。这是我的代码:

#include "MainPage.xaml.h"
#include <windows.h>
#include <WinUser.h>
#include <winapifamily.h>
#include <string>
#include <stdio.h>
#include <iostream>

using namespace App1;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using std::cout;
using std::endl;

MainPage::MainPage()
{
InitializeComponent();
}

HWND currenthwnd, hwnd = NULL;
char wnd_title[1024];

void App1::MainPage::Focus_app_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
currenthwnd = GetForegroundWindow();
GetWindowText(hwnd, wnd_title, sizeof(wnd_title));
cout << wnd_title << endl;
}

有什么想法吗?提前致谢!

GetForegroundWindowWinUser.h中的GetWindowText是在#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)宏块中声明的。因此,您只能将它们用于 windows 桌面应用程序。