I am trying to make a Visual C++ 2008 program that plots some data in a Window. I have read from various places the correct way to do this is to override WndProc. So I made a Windows Forms Application in Visual C++ 2008 Express Edition, and I added this code to Form1.h, but it won’t compile:
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); }
When I try to compile this in Visual C++ 2008 Express Edition, this error occurs: error C2664: ‘BeginPaint’ : cannot convert parameter 1 from ‘System::IntPtr’ to ‘HWND’
When I try using this->Handle instead of m.HWnd the same error occurs.
When I try casting m.HWnd to (HWND), this error occurs: error C2440: ‘type cast’ : cannot convert from ‘System::IntPtr’ to ‘HWND’
Maybe I need to cast the m.HWnd to a pin_ptr or something.
If you were making a raw Win32 application then you could use those functions.
If, on the other hand, you are making a WinForms application then you need to override the OnPaint event.
You will end up with a Paint routine shell from which you can use the drawing functions of the graphics object.
If you really want to write raw Win32 code, let me know, and I can help you write a shell. In the time being if you are interested in Win32 I recommend Charles Petzold’s Programming Windows 5th edition.
If you want to learn C++ WinForms… well, I recommend switching to C# or VB.NET simply because they might be more intuitive.
Hope this helps. Cheers.