Where main code should be located ?(C++/CLI Visual Studio 2012) - c++

I have began making a project in Visual Studio 2012 C++/CLI .
But I have some questions about correct formatting and placing.
Firstly, where should be main code located ? Creating GUI means that it will be a lot of events(like button clicks, text inputs and so on ) . But is it correct to place code of events in header file ?
My .cpp file looks like that
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void Main(array<String^>^args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
MultiLevelList::MyForm form;
Application::Run(%form);
}
All events are in MyForm.h .
Also events are private , so I haven't possibility to use them in separate .cpp file.
What is correct style of writting ?
Thx in advance .

Related

How to reopen the form designer in Visual Studio 2019 while the `MyForm.cs` is missing?

I Created a windows form by using an empty C++ CLR project and got an error in a new tab of form designer and I closed that tab and fixed the error in MyForm.cpp by copying the following code:
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
//MFP10 is your project name
MFP10::MyForm form;
Application::Run(%form);
}
and now I am trying to access that designer form tab by double clicking the MyForm.h , suddenly it is showing me the code instead of opening the form in the designer.
I solved the Issue. If you have this Issue you can follow the steps to solve it.
According to the version I am using is VS Enterprise 2019 Registered
Steps:
Options>>Environment>>Preview Features >> and
Enable the Preview of .net Core SDK
Restart the app and open the project and double click on the MyForm.h under header directory.

Getting 2 errors, the project only has a standard windows form

I'm new to programming and I just made a windows form in my new project, I've changed the project properties and I still get these 2 errors:
The SDK 'Microsoft.NET.Sdk' specified could not be found.
command-line error: cannot open metadata file "System.Runtime.dll"
I don't really know what to do and I'd like some help.
It seems that you are creating a C++ winform project. After modifying project properties, do you add below codes into your form.cpp:
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
//WinformCDemo is your project name
WinformCDemo::MyForm form;
Application::Run(%form);
}
And then, you need to re-open visual studio. The workround: https://social.msdn.microsoft.com/Forums/en-US/a9529502-6304-4aa6-90ee-0757ab258d87/create-c-windows-forms-application-in-visual-studio-2017?forum=winforms

Problems using C++ REST with Windows Form and mixed managed code in VS2015

I'm attempting to use the C++ REST SDK. I'm using this example as my starting point.
What has worked so far is creating a new Windows CLR application in VS2015, adding a new cpp file, installing cpprestsdk thru NuGet, pasting the tutorial code into the cpp, turning off management for that cpp file, and compiling. Works as expected.
What also works is creating a new Windows CLR application and making a blank form by adding a cpp file with:
#include "MainForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
MyProject::MainForm form;
Application::Run(%form);
}
And adding a new UI Windows Form element "Main Form" and keeping the code managed. This compiles fine and displays a blank window which I can close.
The issue lies when I add a new .cpp file into the above and paste the example into it as I've done before. I set this new .cpp file as unmanaged and rename the function something arbitrary. I'm not calling this function in any way. It compiles fine, however when I try to launch the program, I have AccessViolationExceptions.
I've done plenty of looking into running mixed managed code and I don't see what error I'm making.
EDIT: For now, I've circumvented the problem by avoiding Windows Forms and using Qt GUI instead.

How do I set up Visual Studio with default headers and comments?

I am fairly new to using Visual Studio. Currently I am using Visual Studio 2015 version. Currently when I set up a new C++ console application project (with Precompiled header off or on) it displays the following:
// Name of project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main()
{
return 0;
}
Every time I create a new project I have to waste my time entering in my header information and comments. I would like it to look like this every time I create a new C++ console application project.
// *********************************************************************
// Name:
// Description:
// Date:
// Class Section:
// Title:
// *********************************************************************
//Heading information
#include <iostream>
#include "stdlib.h"
#include "stdafx.h"
using namespace std;
//Main function
int main()
{
//Pause system and end main function
system("pause");
return 0;
}
Is it possible to set up Visual Studio 2015 to display the code above every time I create a new project? Thanks
Create a new console application.
Edit the file to be as you want it in the new one.
Select "Export Template..." in the "File" menu.
Walk through the wizard (choose a name for your new template, etc.)
Be sure the "Automatically import the template into Visual Studio" is checked.
Restart VS
Select "New Project"
Select "Visual C++" in the tree control on the left
Select your new template in the list in the middle
The usual new-project stuff (name and location for the project, etc.)
[Note: As I'm writing this, I'm looking at it in VS 2013. I don't recall it's changing in VS 2015, but it's possible.]

How to create a VTK project in Windows Form Project (VisualC++ 2012 )

I'm trying to embed scene created with Visualization ToolKit (VTK)library into VisualC++ 2012 created windows form so I can design my Windows native GUI interface.
I'd like to underline that, all examples with console app are configured with (Cmake), compiled with VC++2012 and works flawlessly, as instructed by the official VTK wiki page.
The issue is, if I try to call those VTK functions and class initializations inside of Win Form application I get the Error LNK1107: invalid or corrupt file: cannot read at 0x2E0 D:\.....\VTK_61_BUILD_VS2012\bin\Debug\vtkViewsCore-6.1.dll even if I add everything normally as expected, include headers and external library dependencies.
This makes me think that I'm originating from wrong Visual C++ 2012 project template or something obvious that I'm completely missing, otherwise compiler would arise many not found files or syntax error.
This is the first lines where I'm trying to invoke the VTK library, even the intellisense suggest the vtk..... named proc,functions and structures, but application fails to compile.
#pragma once
#include <vtkSmartPointer.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
namespace CLR_Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
...
...
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
What is the problem here ?
The following link (in particular the answer of Mr. bnsteel) helped me very much, when I had the same problem ==>
http://vtk.1045678.n5.nabble.com/VTK-and-Winforms-integration-td5721086.html
You can use Swig as it is suggested under the link. But it is not necessary. Using the similar way (passing the windows form panel handle) you can create a C++/CLI wrapper as jalal sadeghi proposed. You will pass the panel handle through the wrapper to your C++ library that does all the "VTK work". This way you don't have to create individual wrappers for all VTK classes, all the "VTK work" and dependencies on VTK stay hidden in your C++ layer.
Something like this ==>
C++ side
setImageRenderWindowParentID(void *theID){
... (init your vtk render window)
renWin->SetParentId(theID);
}
C++/CLI side
void setRenderWindowParentID(IntPtr parentID, .. also pass the panel size .. ){
void* p = parentID.ToPointer();
myCPPVTK->setRenderWindowParentID(p, .. also pass the panel size ..);
}
C# side
VTKWrapper.setRenderWindowParentID(m_panel.Handle, .. also pass the panel size ..);