Reputation: 453
I am developing a windows app in visual studio 2015 using C++. I need GetForegroundWindow()
and GetWindowText()
to return the app that is currently focusing on. However, it says GetForegroundWindow()
and GetWindowText()
are undefined even I've included "windows.h". I tried go to the definition of GetForegroundWindow()
, it led me to "WinUser.h", so I included "WinUser.h" as well. But it still could not help. Here are my code:
#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;
}
Any ideas? Thanks in advance!
Upvotes: 2
Views: 1022
Reputation: 5920
GetForegroundWindow
and GetWindowText
in the WinUser.h
are declared inside #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
macro block. So you could use them only for windows desktop applications.
Upvotes: 4