1 Replies - 2487 Views - Last Post: 15 September 2011 - 10:07 AM

#1 SpartanGuy07  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 149
  • Joined: 08-September 11

C++ CLI Form Problems - Label Display

Posted 08 September 2011 - 01:43 PM

My overall goal is to create a program to read the mouse position on a form and control a servo motor attached to the program based on the mouse position. However, I am coming across an issue (or several) in the creation of the form and reading mouse position. I initially wrote the program using C++ console application to read to mouse position (since I am fairly new to Visual C++), which worked fine. Now I am trying to convert the program into something I can use with a form as I previously described. No errors occur and the form renders without an issue, however, no matter how long I have tried I can't get the label to display the coordinate position of the mouse. After thinking it might be an issue with my variables, I tried to just display anything into the label, and nothing happens.


MAIN PROJECT
#include "stdafx.h"
#include "Form1.h"
#include <stdio.h>
#include "windows.h"
#include <conio.h>
#include <iostream>

using namespace std;
using namespace MouseDynamixel;


int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;

	Label ^ lblMouseX = gcnew Label;
	Label ^ lblMouseY;

	//get the position of cursor first
	POINT pos;
	GetCursorPos(&pos);
	int x=pos.x;
	int y=pos.y;

	lblMouseX->Text = "Hi";
	//lblMouseX->Text= Convert::ToString(x);
	//lblMouseY->Text= Convert::ToString(y);	
}


Form1.h
#pragma once


namespace MouseDynamixel {

	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>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </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;
			}
		}
	public: System::Windows::Forms::Label^  lblMouseX;
	protected: 

	protected: 

	protected: 


	private: System::Windows::Forms::PictureBox^  pictureBox1;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	public: System::Windows::Forms::Label^  lblMouseY;
	private: 
	private: System::Windows::Forms::Label^  label3;
	public: 
	public: 

	public: 
	private: 



	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#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)
		{
		System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
		this->lblMouseX = (gcnew System::Windows::Forms::Label());
		this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
		this->label1 = (gcnew System::Windows::Forms::Label());
		this->label2 = (gcnew System::Windows::Forms::Label());
		this->lblMouseY = (gcnew System::Windows::Forms::Label());
		this->label3 = (gcnew System::Windows::Forms::Label());
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
		this->SuspendLayout();
		// 
		// lblMouseX
		// 
		this->lblMouseX->BackColor = System::Drawing::SystemColors::ControlLight;
		this->lblMouseX->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
		this->lblMouseX->Location = System::Drawing::Point(135, 240);
		this->lblMouseX->Name = L"lblMouseX";
		this->lblMouseX->Size = System::Drawing::Size(35, 24);
		this->lblMouseX->TabIndex = 1;
		this->lblMouseX->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
		// 
		// pictureBox1
		// 
		this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureBox1.Image")));
		this->pictureBox1->InitialImage = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureBox1.InitialImage")));
		this->pictureBox1->Location = System::Drawing::Point(36, 12);
		this->pictureBox1->Name = L"pictureBox1";
		this->pictureBox1->Size = System::Drawing::Size(224, 225);
		this->pictureBox1->TabIndex = 0;
		this->pictureBox1->TabStop = false;
		this->pictureBox1->Waitonload = true;
		this->pictureBox1->Click += gcnew System::EventHandler(this, &Form1::pictureBox1_Click);
		// 
		// label1
		// 
		this->label1->AutoSize = true;
		this->label1->Location = System::Drawing::Point(12, 246);
		this->label1->Name = L"label1";
		this->label1->Size = System::Drawing::Size(81, 13);
		this->label1->TabIndex = 2;
		this->label1->Text = L"Current Position";
		// 
		// label2
		// 
		this->label2->AutoSize = true;
		this->label2->Location = System::Drawing::Point(111, 246);
		this->label2->Name = L"label2";
		this->label2->Size = System::Drawing::Size(17, 13);
		this->label2->TabIndex = 3;
		this->label2->Text = L"X:";
		// 
		// lblMouseY
		// 
		this->lblMouseY->BackColor = System::Drawing::SystemColors::ControlLight;
		this->lblMouseY->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
		this->lblMouseY->Location = System::Drawing::Point(234, 240);
		this->lblMouseY->Name = L"lblMouseY";
		this->lblMouseY->Size = System::Drawing::Size(35, 24);
		this->lblMouseY->TabIndex = 4;
		this->lblMouseY->TextAlign = System::Drawing::ContentAlignment::MiddleCenter;
		// 
		// label3
		// 
		this->label3->AutoSize = true;
		this->label3->Location = System::Drawing::Point(199, 246);
		this->label3->Name = L"label3";
		this->label3->Size = System::Drawing::Size(17, 13);
		this->label3->TabIndex = 5;
		this->label3->Text = L"Y:";
		// 
		// Form1
		// 
		this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
		this->ClientSize = System::Drawing::Size(292, 266);
		this->Controls->Add(this->label3);
		this->Controls->Add(this->lblMouseY);
		this->Controls->Add(this->label2);
		this->Controls->Add(this->label1);
		this->Controls->Add(this->lblMouseX);
		this->Controls->Add(this->pictureBox1);
		this->ForeColor = System::Drawing::SystemColors::ControlText;
		this->Name = L"Form1";
		this->Text = L"Form1";
		(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
		this->ResumeLayout(false);
		this->PerformLayout();

			}
#pragma endregion
	private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
				 }
		};
}


As you can see in the main program, I have tried many ways to do things, based on advice I saw on other posts, but nothing has worked. I am sure I am probably just missing some simple error, however, I cannot find it. Any advice to help me out would be greatly appreciate. The labels I am trying to display the text in are lblMouseX (for X coordinate) and lblMouseY (for Y coordinate).

Thanks

Note: Some parts of my code that I thought might be useful are commented out.

This post has been edited by SpartanGuy07: 08 September 2011 - 01:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ CLI Form Problems - Label Display

#2 Mina-kun  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 43
  • Joined: 13-September 11

Re: C++ CLI Form Problems - Label Display

Posted 15 September 2011 - 10:07 AM

Here's your biggest problem:
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;

	Label ^ lblMouseX = gcnew Label;
	Label ^ lblMouseY;

	//get the position of cursor first
	POINT pos;
	GetCursorPos(&pos);
	int x=pos.x;
	int y=pos.y;

	lblMouseX->Text = "Hi";
	//lblMouseX->Text= Convert::ToString(x);
	//lblMouseY->Text= Convert::ToString(y);	
}


Anything after return 0; occurs after the program exits- which is to say, not at all. Beyond that, the label you're creating isn't being applied to any views at all, so even had this code been correctly placed you'd have had no luck with this.

What you need to do here is open the Form editor, make sure you have the Properties window open (View->Properties Window), and select the form (not one of its components). In the properties window, select the Events tab and double-click on the fields for Load and MouseMove to generate function stubs for these events. Remove the NotImplementedExceptions and do the following:

In the callback for the Load event:
Capture the initial position of the mouse and set the initial label text to reflect. (ex: this->lblMouseX->Text = Convert::ToString(x))

In the callback for the MouseMove event:
Capture the new position of the mouse, and update the label text to reflect. (ex: this->lblMouseY->Text = Convert::ToString(y))
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1