题
我试图让一些C++2008年的节目,绘制一些数据在一个窗口。我已经阅读,从 各个 的地方 正确的方式做到这一点是复盖WndProc.所以我做了一个窗形式应用程序在视觉C++2008表达版,和我加入这个代码Form1.h,但它不会编制:
public:
[System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
virtual void WndProc(Message %m) override
{
switch(m.Msg)
{
case WM_PAINT:
{
HDC hDC;
PAINTSTRUCT ps;
hDC = BeginPaint(m.HWnd, &ps);
// i'd like to insert GDI code here
EndPaint(m.Wnd, &ps);
return;
}
}
Form::WndProc(m);
}
当我试图编纂这些C++2008表达版,这一错误发生:错误C2664:'BeginPaint':无法转换参数1从'系统::因此"到"HWND'
当我尝试使用这个->处理,而不是m。HWnd同样的错误发生。
当我试着铸造的m。HWnd至(HWND),这一错误发生:错误C2440:'类型的铸':不能转换系统::因此'到'HWND'
也许我需要投的m。HWnd到pin_ptr或东西。
解决方案
如果你是一个原Win32应用程序,然后你可以用这些功能。
如果,另一方面,你是一个它应用程序,那么你需要复盖OnPaint事件。
- 切换到设计图(看那显示了你的形式。)
- 点击标题栏中的形式
- 在"属性"窗口(默认情况下可能在你的右下方画面)选择的闪电靠近顶部。这将显示一系列的事件。
- 滚下来的油漆的事件和双击。
你最终会有一个例行的油漆的外壳从中可以利用绘图功能的图形象。
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{
e->Graphics->DrawRectangle(...)
}
如果你真的要写原Win32代码,让我知道,我可以帮你写个外壳。在暂时如果你有兴趣在Win32我建议鲁Petzold的编程Windows第5版。
如果你想学习C++它...好了,我建议交换C#或VB.NET 只是因为他们可能更为直观的。
希望这会有所帮助。欢呼声。
其他提示
该文章引讨论如何在一个地C++应用程序,不在它的应用程序。你应该复盖OnPaint方法,而不是处理消息在WndProc.
我认为你混了之间Win32编程(必须复盖WM_PAINT)和窗式/.净,你只需要复盖所绘制的方法。
绘制上的一种形式是超级-简单。净!你只需复盖的OnPaint方法,然后执行所有的绘图。
你可以结合到油漆的处理程序使用的"工具箱"中的Visual Studio或通过使用下列中的代码类;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyForm_Paint);
你那么实现MyForm_Paint样的方法;
private void MyForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//create a graphics object from the form
Graphics g = this.CreateGraphics();
// create a pen object with which to draw
Pen p = new Pen(Color.Red, 7); // draw the line
// call a member of the graphics class
g.DrawLine(p, 1, 1, 100, 100);
}