- Forum
- Windows Programming
- creating custom control
creating custom control
I am trying to create a custom control that can be placed on forms.
I created a new component and got it to show in the tool box. I think I did something wrong because when I try to drag it into form1, the icon drops to the bottom of the design page. When I try to change the parent property I get a compiler error saying that parent is not a member of my component.
Below is the source code of my control:
|
|
1. You created a component, not a control. Inherit from Control and not from Component.
2. Almost nobody knows C++/CLI around here. If you have more questions/issues, best if you post @ the MSDN forums.
Thank you for the information
I solved the problem by making 2 changes:
1) adding the following line at the top of the code:
using namespace System::Windows::Forms;
2) changing the class declaration as follows:
public ref class grid : public System::Windows::Forms::Panel
—————————-.————————————————————————————
I’m having another problem now. My custom control grid has a button as a child control. When I drag grid onto form1, I see the button on the design pane of the compiler. However when I compile and run the program the button does not appear!
grid code:
|
|
form1 code:
|
|
Refer to #2 above.
Topic archived. No new replies allowed.
Не понимаю, в чем смысл ошибки, прошу пожалуйста помочь.
Вот весь код.
Весь мой код
#pragma once
namespace Project3 {
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>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::TextBox^ textBox1;
protected:
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::ComboBox^ comboBox1;
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->label4 = (gcnew System::Windows::Forms::Label());
this->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(3, 66);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(251, 20);
this->textBox1->TabIndex = 0;
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(3, 139);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(251, 20);
this->textBox2->TabIndex = 1;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(3, 213);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(251, 20);
this->textBox3->TabIndex = 2;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15));
this->label1->Location = System::Drawing::Point(-2, 38);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(140, 25);
this->label1->TabIndex = 3;
this->label1->Text = L"Первое число";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15));
this->label2->Location = System::Drawing::Point(-2, 111);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(140, 25);
this->label2->TabIndex = 4;
this->label2->Text = L"Второе число";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15));
this->label3->Location = System::Drawing::Point(-2, 185);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(111, 25);
this->label3->TabIndex = 5;
this->label3->Text = L"Результат";
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 239);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(126, 49);
this->button1->TabIndex = 6;
this->button1->Text = L"Расчитать";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(144, 239);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(126, 49);
this->button2->TabIndex = 7;
this->button2->Text = L"Сбросить";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
//
// label4
//
this->label4->AutoSize = true;
this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15));
this->label4->Location = System::Drawing::Point(145, 40);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(109, 25);
this->label4->TabIndex = 8;
this->label4->Text = L"Сложение";
//
// comboBox1
//
this->comboBox1->BackColor = System::Drawing::SystemColors::WindowText;
this->comboBox1->ForeColor = System::Drawing::SystemColors::Window;
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^ >(4) { L"Сложение", L"Вычитание", L"Умножение", L"Деление" });
this->comboBox1->Location = System::Drawing::Point(250, 44);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(121, 21);
this->comboBox1->TabIndex = 9;
this->comboBox1->Text = L"Сложение";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(372, 300);
this->Controls->Add(this->comboBox1);
this->Controls->Add(this->label4);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
bool Protect() {
if (textBox1->Text->Length == 0) || ((textBox1->Text->Legth == 0) {
return false;
}
for (int i = 0; i < textBox1->Text->Length; i++) {
if ((textBox1->Text[i] >= '0') && (textBox1->Text[i] <= '9') || (textBox1->Text[i] == ',')) {}
else { return false; }
}
bool Protect() {
for (int i = 0; i < textBox1->Text->Length; i++) {
if ((textBox2->Text[i] >= '0') && (textBox2->Text[i] <= '9') || (textBox2->Text[i] == ',')) {}
else { return false; }
}
return true;
};
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (Protect()) {
double Num1, Num2, Result;
Num1 = System::Convert::ToDouble(textBox1->Text);
Num2 = System::Convert::ToDouble(textBox2->Text);
if (comboBox1->SelectedItem == "Сложение") {
Result = Num1 + Num2;
label4->Text = L"Сложение";
}
else
if (comboBox1->SelectedItem == "Сложение") {
Result = Num1 + Num2;
label4->Text = L"Сложение";
}
else
if (comboBox1->SelectedItem == "Вычитание") {
Result = Num1 - Num2;
label4->Text = L"Вычитание";
}else
if (comboBox1->SelectedItem == "Умножение") {
Result = Num1 * Num2;
label4->Text = L"Умножение";
}else
if (comboBox1->SelectedItem == "Деление") {
if (Num2 != 0) {
Result = Num1 * Num2;
label4->Text = L"Умножение";
}
else {
MessageBox::Show("Попытка деления на 0", "Ошибка");
}
textBox3->Text =System::Convert::ToString(Result);
else {
MessageBox::Show("Ошибка ввода", "Ошибка");
}
}
}
Вот часть, из-за которой не происходят вычисления, хотя ошибка может и не в ней.
Предположительное место ошибки
bool Protect() {
if (textBox1->Text->Length == 0) || ((textBox1->Text->Legth == 0) {
return false;
}
for (int i = 0; i < textBox1->Text->Length; i++) {
if ((textBox1->Text[i] >= '0') && (textBox1->Text[i] <= '9') || (textBox1->Text[i] == ',')) {}
else { return false; }
}
bool Protect() {
for (int i = 0; i < textBox1->Text->Length; i++) {
if ((textBox2->Text[i] >= '0') && (textBox2->Text[i] <= '9') || (textBox2->Text[i] == ',')) {}
else { return false; }
}
return true;
};
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (Protect()) {
double Num1, Num2, Result;
Num1 = System::Convert::ToDouble(textBox1->Text);
Num2 = System::Convert::ToDouble(textBox2->Text);
if (comboBox1->SelectedItem == "Сложение") {
Result = Num1 + Num2;
label4->Text = L"Сложение";
}
else
if (comboBox1->SelectedItem == "Сложение") {
Result = Num1 + Num2;
label4->Text = L"Сложение";
}
else
if (comboBox1->SelectedItem == "Вычитание") {
Result = Num1 - Num2;
label4->Text = L"Вычитание";
}else
if (comboBox1->SelectedItem == "Умножение") {
Result = Num1 * Num2;
label4->Text = L"Умножение";
}else
if (comboBox1->SelectedItem == "Деление") {
if (Num2 != 0) {
Result = Num1 * Num2;
label4->Text = L"Умножение";
}
else {
MessageBox::Show("Попытка деления на 0", "Ошибка");
}
textBox3->Text =System::Convert::ToString(Result);
else {
MessageBox::Show("Ошибка ввода", "Ошибка");
}
}
}
Topic: Add SFML to Windows Form — Visual Studio Design Mode — C++/CLI .NET Managed Code (Read 11708 times)
0 Members and 1 Guest are viewing this topic.
SFML-2.0 for C++
Win 7 Pro x64
Visual Studio 2010 and 11 Beta (CLI IntelliSense!)
Like the subject says, I’d like to add SFML views to a Windows form. I’ve played with the Win32 examples, but the designer in Visual Studio is very nice and I’d like to use it.
The most obvious way to use the designer is to make a Windows Forms program, which puts it under CLI managed code. But if there’s a way to use the designer with the example Win32 code, that would be fine as well.
I’ve looked around the forums, but I’ve found very few threads talking about this, and those I’ve found were incomplete and 2-3 years old.
http://en.sfml-dev.org/forums/index.php?topic=1666.0 <— This is what I’ve tried to use, though I decided to make a new thread instead of resurrecting one that is three years old.
My test Windows Forms project has the same settings added as the working Win32 example (include path, lib path, dependencies: sfml-system-d.lib;sfml-window-d.lib;sfml-graphics-d.lib;).
I attempted modifying my form’s constructor:
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void) : renderView(NULL) // <— init pointer
{
InitializeComponent();
HWND ptr = (HWND)renderWindow—>Handle.ToPointer(); // Get the Label’s handle (renderWindow is a Label)
renderView = new sf::RenderWindow(ptr); // Pass it to the sf::RenderWindow
renderWindow—>Paint += gcnew PaintEventHandler(this, &MyForm::MyForm_Paint);
}
[more code…]
The line «renderView = new sf::RenderWindow(ptr);» generates linker errors:
1>WindowsForms.obj : error LNK2028: unresolved token (0A00000E) «private: virtual bool __clrcall sf::RenderWindow::activate(bool)» (?activate@RenderWindow@sf@@$$FEAM_N_N@Z) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::RenderTarget’}»(void)» (???__E??_SRenderWindow@sf@@6BRenderTarget@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2028: unresolved token (0A00000F) «public: virtual class sf::Vector2<unsigned int> __clrcall sf::RenderWindow::getSize(void)const « (?getSize@RenderWindow@sf@@$$FUBM?AV?$Vector2@I@2@XZ) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::RenderTarget’}»(void)» (???__E??_SRenderWindow@sf@@6BRenderTarget@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2028: unresolved token (0A000010) «protected: virtual void __clrcall sf::RenderWindow::onResize(void)» (?onResize@RenderWindow@sf@@$$FMAMXXZ) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::Window’}»(void)» (???__E??_SRenderWindow@sf@@6BWindow@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2028: unresolved token (0A000011) «protected: virtual void __clrcall sf::RenderWindow::onCreate(void)» (?onCreate@RenderWindow@sf@@$$FMAMXXZ) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::Window’}»(void)» (???__E??_SRenderWindow@sf@@6BWindow@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2028: unresolved token (0A000017) «public: virtual __clrcall sf::RenderWindow::~RenderWindow(void)» (??1RenderWindow@sf@@$$FUAM@XZ) referenced in function «public: virtual void * __clrcall sf::RenderWindow::`vector deleting destructor'(unsigned int)» (??_ERenderWindow@sf@@$$FUAMPAXI@Z)
1>WindowsForms.obj : error LNK2028: unresolved token (0A000042) «public: __clrcall sf::RenderWindow::RenderWindow(struct HWND__ *,struct sf::ContextSettings const &)» (??0RenderWindow@sf@@$$FQAM@PAUHWND__@@ABUContextSettings@1@@Z) referenced in function «public: __clrcall WindowsForms::MyForm::MyForm(void)» (??0MyForm@WindowsForms@@$$FQ$AAM@XZ)
1>WindowsForms.obj : error LNK2019: unresolved external symbol «public: virtual __clrcall sf::RenderWindow::~RenderWindow(void)» (??1RenderWindow@sf@@$$FUAM@XZ) referenced in function «public: virtual void * __clrcall sf::RenderWindow::`vector deleting destructor'(unsigned int)» (??_ERenderWindow@sf@@$$FUAMPAXI@Z)
1>WindowsForms.obj : error LNK2019: unresolved external symbol «public: __clrcall sf::RenderWindow::RenderWindow(struct HWND__ *,struct sf::ContextSettings const &)» (??0RenderWindow@sf@@$$FQAM@PAUHWND__@@ABUContextSettings@1@@Z) referenced in function «public: __clrcall WindowsForms::MyForm::MyForm(void)» (??0MyForm@WindowsForms@@$$FQ$AAM@XZ)
1>WindowsForms.obj : error LNK2019: unresolved external symbol «protected: virtual void __clrcall sf::RenderWindow::onResize(void)» (?onResize@RenderWindow@sf@@$$FMAMXXZ) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::Window’}»(void)» (???__E??_SRenderWindow@sf@@6BWindow@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2019: unresolved external symbol «protected: virtual void __clrcall sf::RenderWindow::onCreate(void)» (?onCreate@RenderWindow@sf@@$$FMAMXXZ) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::Window’}»(void)» (???__E??_SRenderWindow@sf@@6BWindow@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2019: unresolved external symbol «private: virtual bool __clrcall sf::RenderWindow::activate(bool)» (?activate@RenderWindow@sf@@$$FEAM_N_N@Z) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::RenderTarget’}»(void)» (???__E??_SRenderWindow@sf@@6BRenderTarget@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>WindowsForms.obj : error LNK2019: unresolved external symbol «public: virtual class sf::Vector2<unsigned int> __clrcall sf::RenderWindow::getSize(void)const « (?getSize@RenderWindow@sf@@$$FUBM?AV?$Vector2@I@2@XZ) referenced in function «void __clrcall `dynamic initializer for ‘const sf::RenderWindow::`local vftable'{for `sf::RenderTarget’}»(void)» (???__E??_SRenderWindow@sf@@6BRenderTarget@1@@@@YMXXZ@?A0x679daca2@@$$FYMXXZ)
1>D:\Zytrex—Development\VisualStudio\WindowsForms\Debug\WindowsForms.exe : fatal error LNK1120: 12 unresolved externals
In the thread I’ve tried to work from (the link I posted), it sounded like the guy got his to work. Unfortunately, he didn’t mention anything about his project settings, so I don’t know what I’m missing that would get rid of the linker errors.
« Last Edit: July 06, 2012, 11:47:22 am by Senzin »
Logged
Andrei15193
Hello,
First of all C++/CLI is considered to be a separate programming language from C++/STD. Altho VS uses the same compiler for both (which allows you to combine C with C++/STD with C++/CLI) it’s better not to mix those up. SFML is written for C++/STD and all of it’s classes are «unmanaged». You can’t have an «unmanaged» type inside a «managed» one, unless you do some weird stuff. In the end you will have an unportable application.
If you want Windows Forms-like apps and mix it up with SFML, it’s better to look into Qt since both are made for C++/STD. For .NET game development look into XNA Framework.
Logged
Thanks Andrei15193,
When you said, «You can’t have an «unmanaged» type inside a «managed» one», I assume you were being literal, referring to having an unmanaged class as a member of a managed class? Can you not even have pointers to unmanaged classes? And I assume this has to do with garbage collection, etc, because I know you can mix them, as long as you know which are handled by what. That’s part of the purpose of using the ^ operator instead of *, as I understand it.
Anyway, I just noticed that in the thread I linked in my first post, the author said he was using .NET and there is a .NET version of SFML-2.0. I downloaded it to take a look, but it only included the dll files sfmlnet-audio-2.dll, sfmlnet-graphics-2.dll and sfmlnet-windows-2.dll. Don’t I need different .lib and .pdb files too, as well as debug versions for compilation?
Logged
Don’t I need different .lib and .pdb files too, as well as debug versions for compilation?
SFML.Net is made of regular assemblies, that work with any .Net language. It’s not C++, you don’t need import libraries or debug files.
Logged
Laurent Gomila — SFML developer
Andrei15193
In a «managed» class you can’t have a pointer. If you do not use dynamic allocation, as far as I know, you can have an «unmanaged» class instance inside a «managed» one, however I can’t promise it will work (it probably interprets the class as a struct, therefore it will allocate memory on Stack), you can try it out.
The difference between ^ and * is not just who gets a piece of memory where. * (from standard C) defines a pointer type, same in C++/STD since it inherits C. In C++/CLI there are no pointers, there are handles. If you put a ^ next to a type declaration, that is a handle type (or reference), it works similar to a pointer, but it is not a pointer!
Keep in mind that using the .NET Framework will limit you to machines that have the .NET Framework installed. This means something like Windows only, if you limit yourself to .NET Compact Framework 2.0 you may get your app to run on a Xbox. Can’t say anything about Unix/Linux since I haven’t looked into GTK yet.
Logged
Thanks Andrei15193, it looks like I have some reading to do. As for portability, well the whole point of this thread is that I’m trying to use Visual Studio to make a Windows Forms program with Design Mode, so I’m definitely targeting Windows.
Laurent, looks like I have it working. I removed the C++ paths and libs and added references to the .NET libs. I’ll post my code at the bottom (I’m just going to post the entire file so there are no assumptions).
It works great! I didn’t have to do any funky stuff with the label’s style or with the on paint events like in that other thread. I just gave the label’s handle to the RenderWindow constructor and called Clear, Draw and Display from a timer and everything looks nice, with smooth (though very simple so far) animation.
I also have a couple lines of code commented out that will create a separate SFML window instead of using the label. It works just fine as well.
You’ll notice all the SFML setup is in a button’s click event. I originally had it in the Form’s constructor. However the program was starting and then crashing before I ever saw anything. I moved the code to the button just so I could see the form actually appear before I make it crash. But when I hit the button, the exception window popped up and told me what the problem was. It seems before the program was crashing before the popup exception handler was loaded. So I’m just keeping the SFML setup in the button click for now. (Edit: Moving the SFML setup to the form load event solves this problem. SFML starts immediately and automatically and any exceptions are handled by the popup exception handler so you know about them.)
Anyway, the exception was «Unable to load DLL ‘csfml-graphics-2’: The specified module could not be found.» This surprised me, but I went ahead and downloaded csfml, put the dll in the debug folder and it worked! I did the same for audio and window. Is this how it’s supposed to be done?
Oh, and one other thing. I noticed that the examples that come with the 2.0 .NET release are just executables. There isn’t actually any example code. And all of them crash immediately when I try to run them. I checked the 1.6 .NET release and they do have the source code. But the binaries that come with them also crash immediately. Since the 1.6 version has the code, I figured you probably intended to put the code in the 2.0 version, so I wanted to let you know. I don’t know about the crashing though. I tried putting all the dlls in with the binaries, but it didn’t work.
#pragma once
namespace SFMLWinForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace SFML::Audio;
using namespace SFML::Window;
using namespace SFML::Graphics;
/// <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:
RenderWindow^ window;
Music^ music;
Texture^ texture;
Sprite^ sprite;
SFML::Graphics::Font^ font;
SFML::Graphics::Text^ text;
private: System::Windows::Forms::Label^ lblWindow;
private: System::Windows::Forms::Button^ btnQuit;
private: System::Windows::Forms::Button^ btnStart;
private: System::Windows::Forms::Timer^ tmrSFML;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
#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—>components = (gcnew System::ComponentModel::Container());
this—>lblWindow = (gcnew System::Windows::Forms::Label());
this—>btnQuit = (gcnew System::Windows::Forms::Button());
this—>btnStart = (gcnew System::Windows::Forms::Button());
this—>tmrSFML = (gcnew System::Windows::Forms::Timer(this—>components));
this—>SuspendLayout();
//
// lblWindow
//
this—>lblWindow—>Location = System::Drawing::Point(12, 9);
this—>lblWindow—>Name = L«lblWindow»;
this—>lblWindow—>Size = System::Drawing::Size(760, 515);
this—>lblWindow—>TabIndex = 2;
this—>lblWindow—>Text = L«Label Window»;
//
// btnQuit
//
this—>btnQuit—>DialogResult = System::Windows::Forms::DialogResult::Cancel;
this—>btnQuit—>Location = System::Drawing::Point(697, 527);
this—>btnQuit—>Name = L«btnQuit»;
this—>btnQuit—>Size = System::Drawing::Size(75, 23);
this—>btnQuit—>TabIndex = 1;
this—>btnQuit—>Text = L«Quit»;
this—>btnQuit—>UseVisualStyleBackColor = true;
this—>btnQuit—>Click += gcnew System::EventHandler(this, &Form1::btnQuit_Click);
//
// btnStart
//
this—>btnStart—>Location = System::Drawing::Point(15, 527);
this—>btnStart—>Name = L«btnStart»;
this—>btnStart—>Size = System::Drawing::Size(75, 23);
this—>btnStart—>TabIndex = 0;
this—>btnStart—>Text = L«Start»;
this—>btnStart—>UseVisualStyleBackColor = true;
this—>btnStart—>Click += gcnew System::EventHandler(this, &Form1::btnStart_Click);
//
// tmrSFML
//
this—>tmrSFML—>Interval = 1;
this—>tmrSFML—>Tick += gcnew System::EventHandler(this, &Form1::tmrSFML_Tick);
//
// Form1
//
this—>AcceptButton = this—>btnStart;
this—>AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this—>AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this—>CancelButton = this—>btnQuit;
this—>ClientSize = System::Drawing::Size(784, 562);
this—>Controls—>Add(this—>btnStart);
this—>Controls—>Add(this—>btnQuit);
this—>Controls—>Add(this—>lblWindow);
this—>Name = L«Form1»;
this—>Text = L«SFML»;
this—>ResumeLayout(false);
}
#pragma endregion
private:System::Void btnQuit_Click(System::Object^ sender, System::EventArgs^ e)
{
window—>Close(); // For separate SFML window
this—>Close();
}
private:System::Void OnClose(System::Object^ sender, System::EventArgs^ e)
{
// For separate SFML window close
((RenderWindow^)sender)—>Close();
}
private:System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e)
{
static bool runOnce = false;
if (runOnce) return;
runOnce = true;
// Creates a separate window (in addition to Form1) for rendering SFML
//window = gcnew RenderWindow(VideoMode(800, 600), «SFML Window»);
//window->Closed += gcnew EventHandler(this, &Form1::OnClose); // For separate SFML window close
// Uses lblWindow (a label) as render window instead of separate window
window = gcnew RenderWindow(lblWindow—>Handle);
music = gcnew Music(«../media/music.ogg»);
music—>Play();
music—>Loop = true;
texture = gcnew Texture(«../media/cute_image.jpg»);
sprite = gcnew Sprite(texture);
font = gcnew SFML::Graphics::Font(«../media/arial.ttf»);
text = gcnew SFML::Graphics::Text(«WHY DO I EXIST ?», font, 50);
text—>Position = Vector2f(8, 335);
text—>Color = SFML::Graphics::Color::Black;
tmrSFML—>Enabled = true;
}
private:System::Void tmrSFML_Tick(System::Object^ sender, System::EventArgs^ e)
{
// Process events
window—>DispatchEvents();
// Animate sprite and text
float speed = 1;
static float i = speed;
static float j = speed;
if (sprite—>Position.X + texture—>Size.X + 1 > window—>Size.X) i = —speed;
if (sprite—>Position.X — 1 < 0) i = speed;
if (sprite—>Position.Y + texture—>Size.Y + 1 > window—>Size.Y) j = —speed;
if (sprite—>Position.Y — 1 < 0) j = speed;
sprite—>Position += Vector2f(i, j);
text—>Position += Vector2f(i, j);
// Clear screen
window—>Clear();
// Draw the sprite
window—>Draw(sprite);
// Draw the string
window—>Draw(text);
// Update the window
window—>Display();
}
};
}
[attachment deleted by admin]
« Last Edit: July 07, 2012, 08:57:57 am by Senzin »
Logged
Keep in mind that using the .NET Framework will limit you to machines that have the .NET Framework installed. This means something like Windows only, if you limit yourself to .NET Compact Framework 2.0 you may get your app to run on a Xbox.
While I agree with the sentiment, it’s not true that .NET is limited to Windows. Mono is a cross-platform .NET implementation which works on Mac and Linux (although I think it doesn’t implement a few older, deprecated interfaces).
Logged
Andrei15193
Keep in mind that using the .NET Framework will limit you to machines that have the .NET Framework installed. This means something like Windows only, if you limit yourself to .NET Compact Framework 2.0 you may get your app to run on a Xbox.
While I agree with the sentiment, it’s not true that .NET is limited to Windows. Mono is a cross-platform .NET implementation which works on Mac and Linux (although I think it doesn’t implement a few older, deprecated interfaces).
.NET Framework has releases only for Windows Platforms. Mono doesn’t fully support .NET. When saying that .NET can run on other platforms via Mono I expect that it can run any feature from that Framework, it isn’t true (http://mono-project.com/Compatibility). Therefore .NET runs only on Windows Platforms because those platforms receive .NET releases (which fully implement all features of that Framework). Saying that Mono gives portability to a limited .NET application is true.
Logged
So since I’m using a Label as the handle that the SFML Render Window (just «SFML» for short) draws on. So I’m no longer using any of SFML’s own events. Instead, I’m using the C++/CLI Winforms interface. However there are times when I want key presses and mouse clicks to be directed to SFML (or rather, certain key presses and mouse clicks should activate code that does something to SFML).
Since I’m using a Label, which cannot gain focus, I need to handle key presses globally (within the form at least). In other words, I need hot keys. Here are some simple examples of what kind of hot keys I’d like (please don’t worry about how I make things happen in the SFML, the point is catching the hot keys).
1) When I press ‘R’, the graphics should reset.
2) When I press ‘L’, new graphics should be loaded.
3) If I hold Ctrl and click on the SFML, something will happen at the location I clicked.
4) If I hold the Spacebar and mouse drag on the SFML, things will be moved around.
5) If some TextBox control has focus, then key presses should go to the text box and not activate the hot keys.
6) If I MouseClick on some Button control and then start clicking on the SFML and at some point press the Spacebar, the Button should not be clicked again. (Remember, this SFML cannot have focus, so the button I pressed before still has focus.)
So that’s what I’d like to do. Here is what I’ve tried so far:
A) To catch all hot keys, I set the form’s KeyPreview property to true. This makes all key presses go to the form before any other controls. Then I simply handle KeyDown, KeyUp, or KeyPress events for the form.
B) To make sure typing in text boxes doesn’t activate hot keys, in the KeyDown, KeyUp, or KeyPress events for the form, I check if any text boxes have focus before doing anything with the event, e.g.:
if (ActiveControl—>Name != «textBox1» && ActiveControl—>Name != «textBox2»)
{ handleHotKey(e); }
TextBoxes actually have a PreviewKeyDown event that fires before the Form’s KeyPreview, but I’m not sure how I would use it. It fires even before the text enters the TextBox, so setting the event.handled to true actually prevents the text from appearing.
C) To prevent buttons from being activated by the Spacebar, I used the MouseClick event for the buttons instead of the Click event, so only an actual MouseClick on the button will make anything happen. While this works, the Spacebar still activates the button’s clicking animation.
Instead of a Label, I’ve also considered using a TextBox as the handle given to the SFML Render Window. But there are a few problems with this as well. When the TextBox gains or loses focus, it redraws itself, causing a flicker in SFML. When it has focus, even with no text and read only, it still shows the typing/selection caret blinking (disabling the TextBox fixes that, but then it can no longer gain focus). If there was a way to prevent the TextBox from repainting, that could fix the problem. However, even if those problems were fixed, the idea that the TextBox could lose focus by clicking on Buttons and other things seems bad. If I click on some Button (giving it focus), and then hit some hot key, it should still fire off the event.
Basically, it should be assumed that all keyboard events could be doing something to the SFML Render Window, where the exceptions are things like text boxes having focus. Another way to think of it is that the SFML Render Window should always have focus (again, except for TextBoxes, etc).
So far, my methods with the Label all seems to work, but they just feel a bit clumsy. Any suggestions on how to improve this would be appreciated.
Edit: Someone on Stack Overflow had this to say: «Well, don’t pick a Label. And definitely not a TextBox. Just make your own Control derived class.» Not so sure about that.
Logged
Форум программистов Vingrad
Модераторы: Partizan, gambit |
Поиск: |
При создании объекта Graphics ошибка |
Опции темы |
grey_f1 |
|
||
Новичок Профиль Репутация: нет
|
При попытке создания объекта Graphics выводится ошибка «c:\documents and settings\user\мои документы\visual studio 2005\projects\asd\asd\Form1.h(27) : error C3699: ‘*’ : cannot use this indirection on type ‘System::Drawing::Graphics’ Вот код формы:
Подскажите, в чем проблема. |
||
|
|||
Lazin |
|
||
Эксперт Профиль
Репутация: 1
|
Вопрос нужно задавать сдесь .NET ——————— C++ is the steampunk of programming languages: modern ideas realized with victorian technology. |
||
|
|||
grey_f1 |
|
||
Новичок Профиль Репутация: нет
|
Модераторы, пожалуйста перенесите тему. |
||
|
|||
stab |
|
||
Эксперт Профиль Репутация: 7
|
grey_f1, вместо звёздочки крыжик (^) поставь. ——————— 6, 6, 6 — the number of the beast. |
||
|
|||
grey_f1 |
|
||
Новичок Профиль Репутация: нет
|
stab, спасибо! Заработало! Но у меня еще одна проблемка, при попытке создать экземпляр класса Pen в обработчике клика по кнопке, такая ошибка «error C2750: ‘System::Drawing::Pen’ : cannot use ‘new’ on the reference type; use ‘gcnew’ instead» и такая «error C2440: ‘initializing’ : cannot convert from ‘System::Drawing::Pen *’ to ‘System::Drawing::Pen ^'» Вот код, который добавляю в обработчик клика по кнопке:
Подскажите, в чем тут проблема |
||
|
|||
stab |
|
||
Эксперт Профиль Репутация: 7
|
вместо new надо gcnew. написали же всё в тексте ошибки ——————— 6, 6, 6 — the number of the beast. |
||
|
|||
grey_f1 |
|
||
Новичок Профиль Репутация: нет
|
Да, и правда написано, туплю чего то |
||
|
|||
Прежде чем создать тему, посмотрите сюда: | |
Используйте теги [code=csharp][/code] для подсветки кода. Используйтe чекбокс «транслит» если у Вас нет русских шрифтов. Если Вам понравилась атмосфера форума, заходите к нам чаще! С уважением, mr.DUDA, Partizan, PashaPash. |
0 Пользователей читают эту тему (0 Гостей и 0 Скрытых Пользователей) |
0 Пользователей: |
« Предыдущая тема | .NET для новичков | Следующая тема » |