我正在尝试编写一个简单的CLR-Windows表单应用程序。它将显示一个带按钮的窗口。单击该按钮后,它将隐藏其中一个打开的Windows。为了达到这个目的,我调用ShowWindow(00050214,false);,其中00050214是我隐藏的Window的句柄。
但它给出了错误:
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
答案 0 :(得分:0)
您的标头文件不包含windows.h
,因此不知道符号ShowWindow
。
您拨打ShowWindow
的方式也是错误的。第二个参数是int
,您想要传递SW_HIDE
。我打赌你的窗口句柄实际上是一个十六进制数字。
ShowWindow((HWND)0x00050214, SW_HIDE);
顺便说一句,我不知道为什么你在头文件中放了这么多代码。通常,您将在头文件中声明该类,然后在cpp文件中定义实现。