[英]Using PaintEventHandler - Visual C++ (Studio 2010) Windows Forms Application
So, I am creating a Windows Forms Application in Visual C++ 2010, and I want to add an event to a text box.所以,我正在 Visual C++ 2010 中创建一个 Windows Forms 应用程序,并且我想在文本框中添加一个事件。 When the program loads, a letter A is printed onto the screen.当程序加载时,一个字母 A 会打印到屏幕上。 When you enter the text box, the letter is supposed to turn red.当您输入文本框时,该字母应该变为红色。
The name of the textbox is AngleA, and this is the code I have so far:文本框的名称是 AngleA,这是我到目前为止的代码:
this->AngleA->Enter += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::AngleA_Enter);
//many lines later
this->Controls->Add(this->AngleA);
//many lines later
public: System::Void Form1::AngleA_Enter(System::Object^ sender, PaintEventArgs^ e)
{
System::Drawing::Font^ textFontA = gcnew System::Drawing::Font("Arial", 16);
System::Drawing::SolidBrush^ textBrushA = gcnew System::Drawing::SolidBrush(Color::Red);
e->Graphics->DrawString("A", textFontA, textBrushA, 300, 120);
}
The original drawing of the letter happens in a separate function, here:这封信的原图发生在一个单独的 function 中,这里:
public: virtual Void Form1::OnPaint(PaintEventArgs^ pe ) override
{
Graphics^ g = pe->Graphics;
System::Drawing::Font^ textFont = gcnew System::Drawing::Font("Times New Roman", 16);
SolidBrush^ textBrushA = gcnew SolidBrush(Color::Black);
g->DrawString("A", textFont, textBrushA, 300, 120);
}
So, the drawing of the original letter works great, but every time I try to build the program with the Enter event, I get the following error:因此,原始字母的绘制效果很好,但是每次我尝试使用 Enter 事件构建程序时,都会出现以下错误:
error C2664: 'System::Windows::Forms::Control::Enter::add' : cannot convert parameter 1 from 'System::Windows::Forms::PaintEventHandler ^' to 'System::EventHandler ^'
1> No user-defined-conversion operator available, or
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
It seems to me that the form1 object (default name for class in windows forms apps) will only accept an EventHandler parameter for the "this->AngleA->Enter += gcnew " and not PaintEventHandler, but I dont understand why. It seems to me that the form1 object (default name for class in windows forms apps) will only accept an EventHandler parameter for the "this->AngleA->Enter += gcnew " and not PaintEventHandler, but I dont understand why. Is there any way to create an Enter event function that will allow me to paint after the program has already loaded, based on an event?有什么方法可以创建一个 Enter 事件 function 允许我在程序加载后根据事件进行绘画?
Thanks for the help, I hope I was clear in my question:)感谢您的帮助,我希望我的问题很清楚:)
You can only add a PaintEventHandler
to the Paint
event;您只能将PaintEventHandler
添加到Paint
事件; not to the Enter
event.不是Enter
事件。
You probably want to add a normal EventHandler
to the Enter
event and call Invalidate()
in the handler.您可能希望向Enter
事件添加一个普通的EventHandler
并在处理程序中调用Invalidate()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.