Как я могу использовать кнопку WindowsFormApplication для переключения между двумя приложениями Windows (заставить их наверх)?

0

У меня есть следующая цель: создать кнопку окна, в которой: a) на клик переключает свою метку и принудительно запускает приложение для запуска приложений (например, калькулятор) на фронт и б) при последующем щелчке меняет свою метку на исходное состояние и заставляет второе приложение Windows (например, "Блокнот") спереди. Это переключение должно быть возможным для продолжения бесконечного времени (пока пользователь не прервет).

Примечание. Я использую MS Visual Studio.

Что я до сих пор (построенный на основе других других потоков):

1) Успешно запущенная WindowsFormApplication, предоставляющая кнопку, которая при нажатии на нее переключает свой ярлык из "Калькулятор" в "Блокнот" и наоборот.

2) Консольное приложение Win32, которое успешно находит запущенное приложение Calucalor и обеспечивает его на первый план.

Код для 1)

ButtonSwitchApplication.cpp

    // ButtonSwitchApplication.cpp : main project file.

#include "Form1.h"
#include <windows.h>

using namespace ButtonSwitchApplication;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    //HelloWorld();

    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());

    return 0;
}

Form1.h

#pragma once
#include <windows.h>

namespace ButtonSwitchApplication {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(26, 45);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(232, 53);
            this->button1->TabIndex = 0;
            this->button1->Text = L"INFO"; // initializes the button label with "INFO"
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 261);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
        }
#pragma endregion
    private: System::Void Form1::button1_Click(System::Object^  sender,     System::EventArgs^  e) {

        static bool isInfo = true;
        if(isInfo == false)
        {
           button1->Text = "Calculator";

        } // end if (isInfo)
        else
        {
            button1->Text = "Notepad";

        } // end else
        isInfo = !isInfo;

        } // end private button1_Click
    };
}

Код для 2)

Source.cpp

#include <iostream>  // need this header file to support the C++ I/O system
#include <windows.h>

using namespace std; // telling the compiler to use namespace "std",
             // where the entire C++ library is declared.

int main()
{

        // This code is searching for a running application
        // Retrieves a handle to the top-level window whose class name and window name match the specified strings.
        // This function does not search child windows.
        // This function does not perform a case-sensitive search.
        HWND hwnd1 = FindWindow(NULL,TEXT("Calculator"));
        HWND hwnd2 = FindWindow(NULL,TEXT("Notepad"));

        // used temporarily to decide which window to search for 
        int MyTemp = 0;

        if (MyTemp == 0) {
            SetForegroundWindow(hwnd1);
            cout << " Found Calculator!" << endl;
            cout << hwnd1 << endl;
            getchar();
        } // end if
        else if (MyTemp == 1) {
            SetForegroundWindow(hwnd2);
            cout << " Found Notepad!" << endl;
            cout << hwnd2 << endl;
            getchar();
        } // end else if
        else {
            cout << " Did not find the application window!" << endl;
            cout << hwnd1 << endl;
            getchar();
        }

}

Мне не удается выйти замуж 1) и 2) два вместе, чтобы выполнить вышеуказанную задачу.

Я попытался включить

HWND hwnd = FindWindow(NULL,TEXT("Calculator"));
SetForegroundWindow(hwnd);
MessageBox::Show("Found Calculator!");

сразу после

button1->Text = "Calculator";

в Form1.h, но я получаю такие ошибки, как:

1>  ButtonSwitchApplication.cpp
1>ButtonSwitchApplication.obj : error LNK2028: unresolved token (0A000019) "extern "C" int __stdcall SetForegroundWindow(struct HWND__ *)" [...]
1>ButtonSwitchApplication.obj : error LNK2028: unresolved token (0A00001E) "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const [...]
1>ButtonSwitchApplication.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const  [...]
1>ButtonSwitchApplication.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall SetForegroundWindow(struct HWND__ *)"  [...]

Любая помощь по достижению цели очень ценится. Благодарю!

Лучше, Майкл.

Теги:
foreground
togglebutton
hwnd

1 ответ

0
Лучший ответ

Я понял это сам... после долгого времени:

Несколько вещей были ключом к успеху:

  • используя правильное пространство имен для функций SetForegroundWinow
  • включая user32.lib в свойствах> Свойства конфигурации> Компоновщик> Ввод> Дополнительные зависимости

Вот код запуска, который дает мне кнопку переключения, которая выводит на передний план две запущенные приложения (в этом примере калькулятор и блокнот)

ButtonSwitchApplication.cpp:

// ButtonSwitchApplication.cpp : main project file.

#include <iostream>  // need this header file to support the C++ I/O system
#include <windows.h>

//define the functions that do the work
namespace ButtonSwitchApplication
{
    using namespace System::Windows::Forms;

    System::Void CalcToTop(){
    // This code is searching for a running application
    // Retrieves a handle to the top-level window whose class name and window name match the specified strings.
    // This function does not search child windows.
    // This function does not perform a case-sensitive search.
    HWND hwnd = FindWindow(NULL,TEXT("Untitled - Notepad"));
    SetForegroundWindow(hwnd);
    //MessageBox::Show("Found Notepad!");
} //  end CalcToTop

    System::Void NotepadToTop(){
    // This code is searching for a running application
    // Retrieves a handle to the top-level window whose class name and window name match the specified strings.
    // This function does not search child windows.
    // This function does not perform a case-sensitive search.
    HWND hwnd = FindWindow(NULL,TEXT("Calculator"));
    SetForegroundWindow(hwnd);
    //MessageBox::Show("Found Calculator !");
} //  end CalcToTop
}

// includes all of the VS generated forms code
#include "Form1.h"

using namespace ButtonSwitchApplication;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{

    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());

    return 0;
}

Form1.h:

#pragma once
#include <windows.h>

namespace ButtonSwitchApplication {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->AutoSizeMode = System::Windows::Forms::AutoSizeMode::GrowAndShrink;
            this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
            static_cast<System::Byte>(0)));

            this->button1->Dock = System::Windows::Forms::DockStyle::Fill;
            this->button1->Location = System::Drawing::Point(0, 0);
            this->button1->MinimumSize = System::Drawing::Size(200, 60);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(345, 86);
            this->button1->TabIndex = 0;
            this->button1->Text = L"CALCULATOR";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(345, 86);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);

        }
#pragma endregion
    private: System::Void Form1::button1_Click(System::Object^  sender, System::EventArgs^  e) {

        static bool isCalc = true;
        if(isCalc == false)
        {
           button1->Text = "CALCULATOR";
           ButtonSwitchApplication::CalcToTop();
        } // end if (isInfo)
        else
        {
            button1->Text = "NOTEPAD";
            ButtonSwitchApplication::NotepadToTop();

        } // end else
        isCalc = !isCalc;

        } // end private button1_Click
    };
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню