Turning a certain point on form a specified color in MSVC++.NET
Hi, I need to know how I can set a certain point of my form a color of my choosing. (Color found in System::Drawing::Brushes)
Thanks,
The Digital Pioneer
What do you mean by "a certain point" ?
If it is to just color a specified area then perhaps you could just override the OnPaint method of the Form (or Panel or Control), and use the Graphics object to draw a Rectangle (or another shape).
To control if and how the area is painted, you could pre-assign and test against a private boolean field. To paint in a dynamic location, you could simply read the coordinates of an existng control or the mouse.
Finally, to force the paint event, use the Invalidate() method, optionally passing a Region parameter so as not to repaint everything.
I have not included code samples here as I am not 100% certain of C++ syntax, but if you think it would help i could give it a go...
ANOTHER WAY:
The above method is perhaps not very user friendly with the VS.NET GUI designer, so an easier way to do it would be to simply create a Label control with blank text and give it a size, location, and background color of your choosing. Then simply set its Visible property to true or false when you want to show/hide it, thereby emulating "coloring" a certain area of your form.
I thought about your second idea, but it would take too long to get the kind of specifics I would like.
Anyways, I guess I should have mentioned that this would be at the mouse's location, and prefferably with an alterable width, since I only want it to make a colored dot where the mouse is positioned when the event for it is raised. I couldn't figure it out, and, if it isn't too much trouble, I would like to take you up on the offer for the code.
Thanks,
The Digital Pioneer
Alright, I've written a simple form in C++ that draws a small rectangle wherever you click it. Hopefully you can extract from the code below what it is you need to do. I haven't catered for alterable point sizes or colors (it is always 10x10 and red), but I'm sure you'll find that easy enough to do.
Hope this helps.
namespace MyApp
{
using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
public __gc class Form1 : public System::Windows::Forms::Form
{
private: bool drawRect;
private: System::Drawing::Point rectPoint;
private: System::Drawing::Size rectSize;
private: System::Drawing::Brush * rectBrush;
public:
Form1(void)
{
this->ClientSize = System::Drawing::Size(500, 500);
this->Text = S"MyForm";
this->MouseDown += new System::Windows::Forms::MouseEventHandler(this, Form1_MouseDown);
drawRect = false;
rectPoint = NULL;
rectSize = System::Drawing::Size(10, 10);
rectBrush = System::Drawing::Brushes::Red;
} protected:
virtual void OnPaint(System::Windows::Forms::PaintEventArgs* e)
{
Form::OnPaint(e);
if (drawRect)
{
int x = rectPoint.X - (rectSize.Width / 2);
int y = rectPoint.Y - (rectSize.Height / 2);
e->Graphics->FillRectangle(rectBrush, x, y, rectSize.Width, rectSize.Height);
}
}
private:
System::Void Form1_MouseDown(System::Object * sender, MouseEventArgs * e)
{
drawRect = true;
rectPoint = Point(e->X, e->Y);
this->Invalidate();
}
};
}
Ok, thanks. Most of that is Greek to me, but if I sit and meditate on it long enough, I'm sure I'll get it. I am
very new to C++.
What I was really hoping for was code that would create a small black dot on the form (at mouses location) whenever an event was raised. I figured out code for everything except how to make the dot.
I really hate to bother you with this, but this is the only place I can come for answers.
Thanks,
The Digital Pioneer
If it's just a black dot you want, make the Rectangle 1x1 and the Brush black.
If you are new to C++ and want to program in .NET, I would maybe suggest going for VB or C# instead... but that's your choice!
Thanks for the advice, but I already know MSVB.NET!!!
The code provided returned this for 10 errors and 1 warning:
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(10) : error C4980: '__gc' : use of this keyword requires /clr:oldSyntax command line option
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(16) : error C3699: '*' : cannot use this indirection on type 'System::Drawing::Brush'
compiler replacing '*' with '^' to continue parsing
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(22) : error C3921: Use of S-prefixed strings requires /clr:oldSyntax command line option
When compiling with /clr, an implicit conversion exists from string literal type to System::String^. If necessary to avoid ambiguity, cast to System::String^
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(32) : error C3699: '*' : cannot use this indirection on type 'System::Windows::Forms::PaintEventArgs'
compiler replacing '*' with '^' to continue parsing
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(44) : error C3699: '*' : cannot use this indirection on type 'System::Object'
compiler replacing '*' with '^' to continue parsing
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(44) : error C3699: '*' : cannot use this indirection on type 'System::Windows::Forms::MouseEventArgs'
compiler replacing '*' with '^' to continue parsing
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(32) : warning C4485: 'MyApp::Form1::OnPaint' : matches base ref class method 'System::Windows::Forms::Form::OnPaint', but is not marked 'new' or 'override'; 'new' (and 'virtual') is assumed
c:\windows\microsoft.net\framework\v2.0.50215\system.windows.forms.dll : see declaration of 'System::Windows::Forms::Form::OnPaint'
Specify 'override' (and 'virtual') to override the ref class virtual method
Specify 'new' (and 'virtual') to hide the ref class virtual method with a new virtual method
Position for 'new' and 'override' keywords is after method parameter list
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(23) : error C3867: 'MyApp::Form1::Form1_MouseDown': function call missing argument list; use '&MyApp::Form1::Form1_MouseDown' to create a pointer to member
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(23) : error C2750: 'System::Windows::Forms::MouseEventHandler' : cannot use 'new' on the reference type; use 'gcnew' instead
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(23) : error C3350: 'System::Windows::Forms::MouseEventHandler' : a delegate constructor expects 2 argument(s)
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\myapp\myapp\Form1.h(26) : error C2065: 'NULL' : undeclared identifier
I get the feeling that perhaps it is a little bit different than what you thought. Did you use the MSVC++.NET 2005 Express IDE to make that code?
Thanks,
The Digital Pioneer
Well, as I said, I am certainly no C++ expert - I had a bit of a battle with errors myself in producing my own bit of code above.
But one thing I do notice in your error listing is that it often says "this requires /clr:oldSyntax command line option" (e.g. for things like '__gc' and S"..." string notation). I also notice from your project directory that you are using visual studio 2005...
So, perhaps there has been a managed C++ syntax or compiler change in the .NET Framework 2.0... I would suggest looking for articles or code samples on this matter which may explain more, unless of course anybody else in these forums can shed some experienced light on it...?
Yeah, I use 2005 beta 2. You can get that for free. The others are aprx. $500-$1000. I can't afford that. I am 13 years old!!!
Anyways, the languages change quite a bit between compiler versions. I've noticed. It's so
hard to find a good tutorial for C++.NET. But, as you said, is there
anyone who knows all the little changes who can give me something to work with.
And Phizz, thanks. Maybe the code doesn't work, but I gleaned a few ideas from it, that might be enough to get me through.
One more question, how can I get a list of events and have the IDE set me up a handler? The MSVB.NET 2005 Betas 1 & 2 had that feature.
Thanks,
The Digital Pioneer
I've been working on this, and I have the following code:
bool doDraw;
System::Drawing::Point rectpoint;
System::Drawing::Size rectsize;
System::Drawing::Pen^ rectbrush;
private: System::Void Form1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
doDraw=true;
rectpoint=Point(e->X, e->Y);
this->Invalidate();
}
private: System::Void Form1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
doDraw=false;
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
rectsize=System::Drawing::Size(10, 10);
rectbrush=System::Drawing::Pens::Black;
doDraw=false;
}
private: System::Void Form1_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
if(doDraw){
int x=rectpoint.X-(rectsize.Width/2);
int y=rectpoint.Y-(rectsize.Height/2);
System::Drawing::Graphics::DrawRectangle(rectbrush, x, y, rectsize.Width, rectsize.Height) }
}
Unfortunately, it has two errors. Can somebody tell me how to fix them? Here they are:
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\desktop doodle\desktop doodle\Form1.h(101) : error C2352: 'System::Drawing::Graphics::DrawRectangle' : illegal call of non-static member function
c:\windows\microsoft.net\framework\v2.0.50215\system.drawing.dll : see declaration of 'System::Drawing::Graphics::DrawRectangle'
Desktop Doodle.cpp
c:\documents and settings\compaq_owner\my documents\visual studio 2005\projects\desktop doodle\desktop doodle\Form1.h(101) : error C2352: 'System::Drawing::Graphics::DrawRectangle' : illegal call of non-static member function
c:\windows\microsoft.net\framework\v2.0.50215\system.drawing.dll : see declaration of 'System::Drawing::Graphics::DrawRectangle'
They are identical, and refer to the same line of code. Can someone tell me what's wrong? By the way, the red code above is where the error is located.
Thanks,
The Digital Pioneer
I added a VB version for everyone.
Public
Class Form1
Private drawRect As Boolean = False
Private rectPoint As System.Drawing.Point = Nothing Private rectSize As New System.Drawing.Size(10, 10) Private rectBrush As System.Drawing.Brush = Brushes.Red Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown drawRect =
True rectPoint =
New Point(e.X, e.Y) Me.Invalidate() End Sub Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint Dim x, y As Integer If drawRect Then x = rectPoint.X - (rectSize.Width / 2)
y = rectPoint.Y - (rectSize.Height / 2)
e.Graphics.FillRectangle(Brushes.Red, x, y, rectSize.Width, rectSize.Height)
End If
End Sub
End Class OK, I'm a VB.NET Programmer too. Unfortunately, that isn't working. By sprinkling messageboxes through it, I found out that the
e.Graphics.FillRectangle(Brushes.Red, x, y, rectSize.Width, rectSize.Height)
command doesn't work.
Thanks,
The Digital Pioneer