0

我正在尝试编写一个简单的 CLR-Windows 窗体应用程序。它将显示一个带有按钮的窗口。一旦您单击该按钮,它将隐藏其中一个打开的 Windows。为此,我调用 ShowWindow(00050214, false);,其中 00050214 是我隐藏的窗口的句柄。

但它给出了错误:

Error   1   error C3861: 'ShowWindow': identifier not found c:\users\afnan
\documents     \visual studio 2010\projects\winformtest\winformtest\Form1.h 80

请查看下面给出的 .h 文件的最后几行,以了解我如何使用上述功能。

WinformTest.cpp:主项目文件。

#include "stdafx.h"
#include "Form1.h"
#include"windows.h"

using namespace WinformTest;

[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;
}

这是.h文件

#pragma once

namespace WinformTest {

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(91, 51);
        this->button1->Name = L"button1";
        this->button1->Size = System::Drawing::Size(75, 23);
        this->button1->TabIndex = 0;
        this->button1->Text = L"button1";
        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, 262);
        this->Controls->Add(this->button1);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

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

             ShowWindow(00050214, false );

         }
};
}

更新

我现在将 windows.h 放在 form.h 文件中,并使用 ShowWindow((HWND)0x00050214, SW_HIDE); 但现在我得到:

Error   1   error LNK2028: unresolved token (0A000011) "extern "C" int __stdcall 
ShowWindow(struct HWND__ *,int)" (?ShowWindow@@$$J18YGHPAUHWND__@@H@Z) referenced in function 
"private: void __clrcall WinformTest::Form1::button1_Click(class System::Object ^,class 
System::EventArgs ^)" 
(?button1_Click@Form1@WinformTest@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)    
C:\Users\Afnan\Documents\Visual Studio 2010\Projects\WinformTest\WinformTest\WinformTest.obj

Error   2   error LNK2019: unresolved external symbol "extern "C" int __stdcall 
ShowWindow(struct HWND__ *,int)" (?ShowWindow@@$$J18YGHPAUHWND__@@H@Z) referenced in function 
"private: void __clrcall WinformTest::Form1::button1_Click(class System::Object ^,class 
System::EventArgs ^)" 
(?button1_Click@Form1@WinformTest@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)    
C:\Users\Afnan\Documents\Visual Studio 2010\Projects\WinformTest\WinformTest\WinformTest.obj

Error   3   error LNK1120: 2 unresolved externals   C:\Users\Afnan\Documents\Visual  
Studio 2010\Projects\WinformTest\Debug\WinformTest.exe  1

怎么放句柄?我使用 spy++ 工具得到了这个句柄。它还显示与以数字表示的句柄相对应的标题。在我的例子中,对应的句柄是: WinformTest - Microsoft Visual Studio

4

1 回答 1

0

您的头文件不包含windows.h,因此它不知道该符号ShowWindow

你打电话的方式ShowWindow也是错误的。第二个参数是 anint并且您要传递SW_HIDE。我敢打赌,您的窗口句柄实际上是一个十六进制数字。

ShowWindow((HWND)0x00050214, SW_HIDE);

顺便说一句,我不确定你为什么在头文件中放了这么多代码。通常你会在头文件中声明类,然后在 cpp 文件中定义实现。

于 2012-06-23T09:34:35.720 回答