Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用WindowsFormApplication按钮在两个Windows应用程序之间切换(强制将其置于顶部)?_C++_Togglebutton_Foreground_Hwnd - Fatal编程技术网

C++ 如何使用WindowsFormApplication按钮在两个Windows应用程序之间切换(强制将其置于顶部)?

C++ 如何使用WindowsFormApplication按钮在两个Windows应用程序之间切换(强制将其置于顶部)?,c++,togglebutton,foreground,hwnd,C++,Togglebutton,Foreground,Hwnd,我的目标如下: 创建一个windows按钮,a)在单击时切换其标签并将运行的windows应用程序(如计算器)强制置于前端,b)在后续单击时将其标签更改回原始状态,并强制第二个windows应用程序(如记事本)置于前端。这种切换应该可以持续无限次(直到用户中止) 注意:我使用的是MS Visual Studio 到目前为止我拥有的(基于各种其他线程构建): 1) 成功运行的WindowsFormApplication提供了一个按钮,单击该按钮后,可将其标签从“计算器”切换到“记事本”,反之亦然

我的目标如下: 创建一个windows按钮,a)在单击时切换其标签并将运行的windows应用程序(如计算器)强制置于前端,b)在后续单击时将其标签更改回原始状态,并强制第二个windows应用程序(如记事本)置于前端。这种切换应该可以持续无限次(直到用户中止)

注意:我使用的是MS Visual Studio

到目前为止我拥有的(基于各种其他线程构建):

1) 成功运行的WindowsFormApplication提供了一个按钮,单击该按钮后,可将其标签从“计算器”切换到“记事本”,反之亦然

2) 一个Win32控制台应用程序,成功找到正在运行的Calucalor应用程序并将其强制到前端

代码(1)

按钮开关应用程序.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;
}
紧接着

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__ *)"  [...]
非常感谢您对如何实现目标的任何帮助。 谢谢

最好的,
迈克尔。

我自己想出来的。。。久而久之:

以下几点是成功的关键:

  • 为setForeGroundInow函数使用正确的命名空间
  • 在属性>配置属性>链接器>输入>其他依赖项中包括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;
}
//ButtonSwitchApplication.cpp:主项目文件。
包含/ /需要这个头文件来支持C++ I/O系统
#包括
//定义完成工作的函数
命名空间按钮开关应用程序
{
使用命名空间System::Windows::Forms;
系统::Void CalcToTop(){
//此代码正在搜索正在运行的应用程序
//检索其类名和窗口名与指定字符串匹配的顶级窗口的句柄。
//此函数不搜索子窗口。
//此函数不执行区分大小写的搜索。
HWND HWND=FindWindow(空,文本(“无标题的记事本”);
setforegroundindow(hwnd);
//MessageBox::Show(“找到记事本!”);
}//结束CalcToTop
系统::Void NotepadToTop(){
//此代码正在搜索正在运行的应用程序
//检索其类名和窗口名与指定字符串匹配的顶级窗口的句柄。
//此函数不搜索子窗口。
//此函数不执行区分大小写的搜索。
HWND HWND=FindWindow(NULL,文本(“计算器”));
setforegroundindow(hwnd);
//MessageBox::Show(“找到计算器!”);
}//结束CalcToTop
}
//包括VS生成的所有表单代码
#包括“表格1.h”
使用名称空间按钮切换应用程序;
[属性]
int main(数组^args)
{
//在创建任何控件之前启用Windows XP视觉效果
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
//创建主窗口并运行它
应用程序::运行(gcnewform1());
返回0;
}
表格1.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
    };
}
#pragma一次
#包括
命名空间按钮开关应用程序{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
/// 
///表格1摘要
/// 
public ref类Form1:public System::Windows::Forms::Form
{
公众:
表格1(无效)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~Form1()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::按钮^button1;
受保护的:
私人:
/// 
///必需的设计器变量。
/// 
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
void初始化组件(void)
{
此->按钮1=(gcnew System::Windows::Forms::Button());
此->SuspendLayout();
// 
//按钮1
// 
此->按钮1->AutoSizeMode=System::Windows::Forms::AutoSizeMode::GrowthandShrink;
此->按钮1->字体=(gcnew System::Drawing::Font,18,System::Drawing::FontStyle::Bold,System::Drawing::GraphicsUnit::Point,
静态(0);
这个->按钮1->Dock=System::Windows::Forms::DockStyle::Fill;
此->按钮1->位置=系统::图形::点(0,0);
此->按钮1->最小尺寸=系统::图纸::尺寸(200,60);
此->按钮1->名称=L“按钮1”;
该->按钮1->尺寸=系统::图纸::尺寸(345,86);
此->按钮1->选项卡索引=0;
此->按钮1->文本=L“计算器”;
此->按钮1->UseVisualStyleBackColor=true;
此->按钮1->Click+=gcnewsystem::EventHandler(此,&Form1::按钮1\u Click);
// 
//表格1
// 
这->自动缩放尺寸=系统::图纸::尺寸(6,13);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
这->ClientSize=System::Drawing::Size(34586);
此->控制->添加(此->按钮1);
此->名称=L“Form1”;
此->文本=L“Form1”;
此->恢复布局(错误);
}
#布拉格端区
私人:系统::
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__ *)"  [...]
// 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;
}
#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
    };
}