Currently I am working on a program on Visual Studio with the Windows Form Application, but every time I try to draw a shape or line with the paint event I get this error:
C2228: left of '.DrawString' must have class/struct/union
Below is my code that involves the pain event from the header file:
using namespace System::Drawing;
void InitializeComponent(void)
{
this->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::Form1_Paint);
}
{
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
e.Graphics.DrawRectangle(blackPen, x, y, width, height);
}
};
Any help would be greatly appreciated
Change
e.Graphics.DrawRectangle( ...
to
e->Graphics->DrawRectangle( ...
Related
I'm using visual studio 2010 and i'm writing a code to capture a screen on a button click. i've written the code as
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
try
{
SaveFileDialog^ save = gcnew SaveFileDialog();
save->Title = "Save Screenshot";
save->Filter = "JPEG | *.jpg | Bitmap | *.bmp | Portable Network Graphics|*.png|Graphical Interchange File Format|*.gif";
save->ShowDialog();
pictureBox1->Image->Save(save->FileName);
}
catch(Exception^ ex)
{
MessageBox::Show(ex->Message);
}
}
private: System::Void Form1_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if(e->KeyCode == System::Windows::Forms::Keys::Enter)
{
Rectangle^ bounds;
System::Drawing::Bitmap^ screenshot;
Graphics^ graph;
bounds = Screen::PrimaryScreen->Bounds;
screenshot = gcnew System::Drawing::Bitmap(bounds->Width,bounds->Height, System::Drawing::Imaging::PixelFormat::Format32bppArgb);
graph = Graphics::FromImage(screenshot);
graph = CopyFromScreen (bounds->X, bounds->Y, 0, 0, bounds-> Size, CopyPixelOperation::SourceCopy);
pictureBox1->Image = screenshot;
}
}
But with this i'm getting an error as CopyFromScreen:Identifier not found. i tried to search abt this and everywhere it shows syntax is correct.
Assuming CopyFromScreen is not a custom free function that isn't included in your example, it is member of the Graphics class.
You should invoke it as such (as you do with Graphics::FromImage() in the line above:
graph = Graphics::CopyFromScreen(bounds->X, bounds->Y, 0, 0, bounds-> Size, CopyPixelOperation::SourceCopy);
EDIT
To address your comment, and my oversight:
CopyFromScreen is not static, so it must be invoked on a specific object that you need to create first:
graph.CopyFromScreen(bounds->X, bounds->Y, 0, 0, bounds-> Size, CopyPixelOperation::SourceCopy);
I have read some answers and some tutorials but they either didn't work or were too complex for me to understand.
I am trying to get a different class to change A labels text but It did not work. Here is my code:
public static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Mainprog mp = new Mainprog();
Mainprog.count += 25;
mp.Counter.Text = Convert.ToString(Mainprog.count);
}
I am currently making a small application for a BMI calculation. The program compiles fine, but I get a run time error of
Format Exception Unhandled
in this line:
height = float.Parse(textBox1.Text);
The line is a part of the function:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
float height;
height = float.Parse(textBox1.Text);
height = height*height;
}
You haven't said what's in the text box when you parse it. Perhaps it's empty, or the user's typed something like "fred". You should always assume that the input could be invalid:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
float height;
if (!float.TryParse(textBox1.Text, out height))
{
// Indicate to the user that the input is invalid, and stop processing
// at this point. For example, you may want to highlight the textbox with
// a red box. Return at the end of the block.
}
// It parsed correctly: continue...
height = height*height;
...
}
(This would probably be structured slightly differently in an MVVM approach, but you'd still want to use float.TryParse to test the user input before accepting it.)
I am developing a smart tag for one of my WPF controls. The smart tag is added through an AdornerProvider in the design dlls of the control. What I want to do is to synchronize the zoom level of my smart tag and the Visual Studio designer, because if I zoom in/out the Visual Studio designer, the smart tag remains unchanged. Anyone got an idea?
private DesignerView Designer
{
get
{
return DesignerView.FromContext(this.Context);
}
}
protected override void Activate(ModelItem item)
{
this.adornedControlModel = item;
Designer.ZoomLevelChanged += new EventHandler(Designer_ZoomLevelChanged);
//
// YOUR CODE
//
base.Activate(item);
}
void Designer_ZoomLevelChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
Is there a way to limit mouse pointer movement to a specific area in wxWidgets? I know there is an API function ClipCursor() in Windows, but is there a method in wxWidgets for all platforms?
No. There is no such function in wx by all i know. Start up a timer (say 50ms) checking the global mouse position. If the mouse is outside the region, then set it into again.
If you want to restrict the mouse for some certain reason, for example to make some sort of game, then you can capture the mouse (see wxWindow::CaptureMouse). You will get mouse events even if the pointer is outside your window. Then you could react to mouse-motion events and do the check for the position there, without a timer. Downside of this is that the mouse won't be able to be used somewhere else for other programs since they won't receive events.
wxWidgets manual states that OSX guidelines forbid the programs to set the mouse pointer to a certain position programmatically. That might contribute to the reason there is not much support for such stuff in wx, especially since wx tries really hard to be compatible to everything possible.
Small sample. Click on the button to restrict the mouse to area 0,0,100,100. Click somewhere to release it.
#include <wx/wx.h>
namespace sample {
class MyWin : public wxFrame {
public:
MyWin()
:wxFrame(0, wxID_ANY, wxT("haha title")) {
mRestricted = wxRect(0, 0, 100, 100);
mLast = mRestricted.GetTopLeft();
wxButton * button = new wxButton(this, wxID_ANY, wxT("click this"));
}
private:
void OnClicked(wxCommandEvent& event) {
if(!HasCapture()) {
CaptureMouse();
CheckPosition();
}
}
void OnMotion(wxMouseEvent& event) {
CheckPosition();
}
void OnLeft(wxMouseEvent& event) {
if(HasCapture())
ReleaseMouse();
}
void CheckPosition() {
wxPoint pos = wxGetMousePosition();
if(!mRestricted.Contains(pos)) {
pos = ScreenToClient(mLast);
WarpPointer(pos.x, pos.y);
} else {
mLast = pos;
}
}
wxRect mRestricted;
wxPoint mLast;
DECLARE_EVENT_TABLE();
};
BEGIN_EVENT_TABLE(MyWin, wxFrame)
EVT_BUTTON(wxID_ANY, MyWin::OnClicked)
EVT_MOTION(MyWin::OnMotion)
EVT_LEFT_DOWN(MyWin::OnLeft)
END_EVENT_TABLE()
class MyApp : public wxApp {
virtual bool OnInit() {
MyWin * win = new MyWin;
win -> Show();
SetTopWindow(win);
return true;
}
};
} /* sample:: */
IMPLEMENT_APP(sample::MyApp)