Introduction CSC 211 Intermediate Programming Graphical User Interface Concepts: Part 1 Graphical user interface Allow interaction with program visually Give program distinct look and feel Built from window gadgets Is an object, accessed via keyboard or mouse 1 2 Fig. 1 Introduction Button Menu Label Menu Bar Box Scrollbar Sample Internet Explorer window with GUI components. Introduction Control Description Label An area in which icons or uneditable text can be displayed. Box An area in which the user inputs data from the keyboard. The area also can display information. Button An area that triggers an event when clicked. CheckBox A GUI control that is either selected or not selected. ComboBox A drop-down list of items from which the user can make a selection, by clicking an item in the list or by typing into a box, if permitted. ListBox An area in which a list of items is displayed from which the user can make a selection by clicking once on any element. Multiple elements can be selected. Panel A container in which components can be placed. HScrollBar Allows the user to access a range of values that cannot normally fit in its container horizontally. VScrollBar Allows the user to access a range of values that cannot normally fit in its container vertically. Fig. 2 Some basic GUI components. 4 Windows Forms Create GUIs for programs Element on the desktop Represented by: Dialog Window Windows Forms Multiple Document Interface (MDI) window Windows Forms Component Class that implements IComponent interface Lacks visual parts Control Component with graphical part Such as button or label Are visible Event Generated by movement from mouse or keyboard Event handlers performs action Specifics written by programmer
Windows Forms Windows Forms Form Properties and Events AcceptButton Description / Delegate and Event Arguments Which button will be clicked when Enter is pressed. (Cont.) AutoScroll Whether scrollbars appear when needed (if data fill more than one screen). CancelButton FormBorderStyle Font Button that is clicked when the Escape key is pressed. Border of the form (e.g., none, single, D, sizable). Font of text displayed on the form, as well as the default font of controls added to the form. in the form s title bar. Common Methods Close Closes form and releases all resources. A closed form cannot be reopened. Hide Show Common Events Load Hides form (does not release resources). Displays a hidden form. (Delegate EventHandler, event arguments EventArgs) Occurs before a form is shown. Fig.4 Common Form properties and events. Fig. Components and controls for Windows Forms. 8 Event-Handling Model Event-Handling Model GUIs are event driven Event handlers calls Handler 1 for event E Methods that process events and perform tasks Object A raises event E calls Delegate for event E Handler 2 for event E Handler for event E Fig. Event-handling model using delegates. 10 Basic Event Handling Basic Event Handling Event name Event delegate Event argument class Fig. List of events List of Form events. Class name Fig. Details of Click event. 11 12
Labels, Boxes es and Buttons Labels Provide text instruction Read only text Defined with class Label Derived from class Control box Class Box Area for text input Password textbox Labels, Boxes es and Buttons Button Control to trigger a specific action Checkboxes or radio buttons Derived from ButtonBase 1 14 Labels, Boxes es and Buttons Label Properties Description / Delegate and Event Arguments Font The font used by the text on the Label. The text to appear on the Label. Align The alignment of the Label s text on the control. One of three horizontal positions (left, center or right) and one of three vertical positions (top, middle or bottom). Fig. 8 Label properties. Labels, Boxes es and Buttons Box Properties Description / Delegate and Event Arguments and Events AcceptsReturn If true, pressing Enter creates a new line in text box, if that text box spans multiple lines. If false, pressing Enter clicks the default button of the form. Multiline If true, text box can span multiple lines. Default is false. PasswordChar Single character to display instead of typed text, making the text box a password box. If no character is specified, text box displays the typed text. ReadOnly If true, text box has a gray background and its text cannot be edited. Default is false. ScrollBars For multiline text boxes, indicates which scrollbars appear (none, horizontal, vertical or both). to be displayed in the text box. Common Events (Delegate EventHandler, event arguments EventArgs) Changed Raised when text changes in text box (the user added or deleted characters). Fig. Box properties and events. 1 1 Labels, Boxes es and Buttons Button properties Description / Delegate and Event Arguments and events displayed on the Button face. Common Events (Delegate EventHandler, event arguments EventArgs) Click Raised when user clicks the control. Fig. 10 Button properties and events. 1 // Fig. 12.1: 2 // Using a box, Label and Button to display // the hidden text in a password field. 4 #pragma once 8 namespace LabelBoxButtonTest { 10 using namespace System; 11 using namespace System::ComponentModel; 12 using namespace System::Collections; 1 using namespace System::Windows::Forms; 14 using namespace System::Data; 1 using namespace System::Drawing; 1 1 /// <summary> 18 /// Summary for Form1 1 /// 20 /// WARNING: If you change the name of this class, you will need to 21 /// change the Resource File Name property for the managed 22 /// resource compiler tool associated with all.resx files 2 /// this class depends on. Otherwise, the designers will not 24 /// be able to interact properly with localized resources 2 /// associated with this form. 2 /// </summary> 1 of 1 18
2 public gc class Form1 : public System::Windows::Forms::Form 2 System::ComponentModel::Container * components; 28 { 2 public: 4 /// <summary> 0 Form1(void) 1 { 2 InitializeComponent(); } 2 of /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> 8 void InitializeComponent(void) of 4 { protected: 0 this->displaypasswordbutton = void Dispose(Boolean disposing) 1 new System::Windows::Forms::Button(); { 2 this->inputpasswordbox = 8 if (disposing && components) new System::Windows::Forms::Box(); { 40 components->dispose(); 41 } 42 super::dispose(disposing); 4 } 44 private: System::Windows::Forms::Button * displaypasswordbutton; 4 private: System::Windows::Forms::Box * inputpasswordbox; 4 private: System::Windows::Forms::Label * displaypasswordlabel; 4 48 private: 4 /// <summary> 0 /// Required designer variable. 1 /// </summary> 4 this->displaypasswordlabel = new System::Windows::Forms::Label(); this->suspendlayout(); // 8 // displaypasswordbutton // 0 this->displaypasswordbutton->location = 1 System::Drawing::Point(, ); 2 this->displaypasswordbutton->name = S"displayPasswordButton"; this->displaypasswordbutton->size = 4 System::Drawing::Size(, 24); this->displaypasswordbutton->tabindex = 1; 1 20 this->displaypasswordbutton-> = S"Show Me"; 101 // this->displaypasswordbutton->click += new System::EventHandler( 8 this, displaypasswordbutton_click); 102 // Form1 10 // // 80 // inputpasswordbox 81 // 4 of 104 this->autoscalebasesize = System::Drawing::Size(, 1); 10 this->clientsize = System::Drawing::Size(2, 1); 10 this->controls->add(this->displaypasswordlabel); of 82 this->inputpasswordbox->location = 10 this->controls->add(this->inputpasswordbox); 8 System::Drawing::Point(1, 1); 108 this->controls->add(this->displaypasswordbutton); 84 this->inputpasswordbox->name = S"inputPasswordBox"; 10 this->name = S"Form1"; 8 this->inputpasswordbox->passwordchar = * ; 110 this-> = S"LabelBoxButtonTest"; 8 this->inputpasswordbox->size = 111 this->resumelayout(false); 8 System::Drawing::Size(24, 20); 112 88 this->inputpasswordbox->tabindex = 0; 11 } 8 this->inputpasswordbox-> = S""; 0 // 1 // displaypasswordlabel 2 // this->displaypasswordlabel->borderstyle = 4 System::Windows::Forms::BorderStyle::FixedD; this->displaypasswordlabel->location = System::Drawing::Point(1, 48); this->displaypasswordlabel->name = S"displayPasswordLabel"; 8 this->displaypasswordlabel->size = 114 11 // display user input on label 11 private: System::Void displaypasswordbutton_click( 11 System::Object * sender, System::EventArgs * e) 118 { 11 displaypasswordlabel-> = inputpasswordbox->; 120 } 121 122 }; 12 } System::Drawing::Size(24, 2); 100 this->displaypasswordlabel->tabindex = 2; 21 22 1 // Fig. 12.20: 2 // Displaying the hidden text in a password field. #include "" #include <windows.h> GroupBoxes and Panels Arrange components on a GUI 8 using namespace LabelBoxButtonTest; GroupBoxes can display a caption property determines its caption Panels can have scrollbar 1 int ncmdshow) View additional controls inside the Panel 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } 2 24
GroupBoxes and Panels GroupBoxes and Panels GroupBox Description Properties Common Properties Controls The controls that the GroupBox contains. Common Properties (Cont.) displayed on the top portion of the GroupBox (its caption). Fig. 11 GroupBox properties. Panel Properties Description Common Properties AutoScroll Whether scrollbars appear when the Panel is too small to hold its controls. Default is false. BorderStyle Border of the Panel (default None; other options are FixedD and FixedSingle). Controls The controls that the Panel contains. Fig. 12 Panel properties. 2 2 GroupBoxes and Panels 1 // Fig. 12.24: 2 // Using GroupBoxes and Panels to hold buttons. 4 #pragma once namespace GroupBoxPanelTest 1 of 4 8 { Controls inside panel panel using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 1 using namespace System::Data; 14 using namespace System::Drawing; 1 1 /// <summary> 1 /// Summary for Form1 18 /// panel scrollbars 1 /// WARNING: If you change the name of this class, you will need to 20 /// change the Resource File Name property for the managed 21 /// resource compiler tool associated with all.resx files 22 /// this class depends on. Otherwise, the designers will not 2 /// be able to interact properly with localized resources 24 /// associated with this form. 2 /// </summary> Fig. 1 Creating a Panel with scrollbars. 2 28 2 public gc class Form1 : public System::Windows::Forms::Form 2 { 28 public: 1 private: 2 /// <summary> /// Required designer variable. 2 Form1(void) 0 { 1 InitializeComponent(); 2 } 2 of 4 4 /// </summary> System::ComponentModel::Container * components; // Visual Studio.NET generated GUI code of 4 8 4 protected: // event handlers to change messagelabel void Dispose(Boolean disposing) 0 { 1 // event handler for hi button if (disposing && components) 2 private: System::Void hibutton_click(system::object * sender, 8 { System::EventArgs * e) components->dispose(); 4 { 40 } messagelabel-> = S"Hi pressed"; 41 super::dispose(disposing); } 42 } 4 private: System::Windows::Forms::Label * messagelabel; 8 // event handler for bye button 44 private: System::Windows::Forms::Button * byebutton; private: System::Void byebutton_click(system::object * sender, 4 private: System::Windows::Forms::GroupBox * maingroupbox; 0 System::EventArgs * e) 4 private: System::Windows::Forms::Button * hibutton; 1 { 4 private: System::Windows::Forms::Panel * mainpanel; 2 messagelabel-> = S"Bye pressed"; 48 private: System::Windows::Forms::Button * rightbutton; } 4 private: System::Windows::Forms::Button * leftbutton; 4 0 2 0
// event handler for far left button private: System::Void leftbutton_click(system::object * sender, System::EventArgs * e) 1 // Fig. 12.2: 2 // GroupBox and Panel demonstration. 8 { messagelabel-> = S"Far left pressed"; 80 } 81 4 of 4 #include "" #include <windows.h> 82 // event handler for far right button 8 using namespace GroupBoxPanelTest; 8 private: System::Void rightbutton_click(system::object * sender, 84 System::EventArgs * e) 8 { 8 messagelabel-> = S"Far right pressed"; 8 } 1 int ncmdshow) 88 }; 8 } 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } 1 2 CheckBoxes es and RadioButtons Fig. 14 GroupBox and Panel demonstration. State buttons On/off or true/false state Derived from class ButtonBase CheckBox No restriction on usage RadioButton Grouped together Only one can be true Mutually exclusive options 4 CheckBoxes es and RadioButtons CheckBox events and Description / Delegate and Event Arguments properties Checked Whether the CheckBox has been checked. CheckState Whether the CheckBox is checked (contains a black checkmark) or unchecked (blank). An enumeration with values Checked, Unchecked or Indeterminate. displayed to the right of the CheckBox (called the label). Common Events (Delegate EventHandler, event arguments EventArgs) CheckedChanged Raised every time the CheckBox is either checked or unchecked. Default event when this control is double clicked in the designer. CheckStateChanged Raised when the CheckState property changes. Fig. 1 CheckBox properties and events. 1 // Fig. 12.2: 2 // Using CheckBoxes to toggle italic and bold styles. 4 #pragma once namespace CheckBoxTest 8 { using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 1 using namespace System::Data; 14 using namespace System::Drawing; 1 1 /// <summary> 1 /// Summary for Form1 18 /// 1 /// WARNING: If you change the name of this class, you will need to 20 /// change the Resource File Name property for the managed 21 /// resource compiler tool associated with all.resx files 22 /// this class depends on. Otherwise, the designers will not 2 /// be able to interact properly with localized resources 24 /// associated with this form. 2 /// </summary> 1 of
2 public gc class Form1 : public System::Windows::Forms::Form 2 { 28 public: 1 System::ComponentModel::Container * components; 2 // Visual Studio.NET generated GUI code 2 Form1(void) 0 { 1 InitializeComponent(); 2 } 2 of 4 // make text bold if not bold, if already bold make not bold private: System::Void boldcheckbox_checkedchanged( System::Object * sender, System::EventArgs * e) of 8 { 4 protected: outputlabel->font = new Drawing::Font( void Dispose(Boolean disposing) 0 outputlabel->font->name, outputlabel->font->size, { 1 static_cast< FontStyle >( if (disposing && components) 2 outputlabel->font->style ^ FontStyle::Bold ) ); 8 { } components->dispose(); 4 40 } // make text italic if not italic, if already italic make not italic 41 super::dispose(disposing); private: System::Void italiccheckbox_checkedchanged( 42 } System::Object * sender, System::EventArgs * e) 4 private: System::Windows::Forms::CheckBox * boldcheckbox; 8 { 44 private: System::Windows::Forms::Label * outputlabel; outputlabel->font = new Drawing::Font( 4 private: System::Windows::Forms::CheckBox * italiccheckbox; 0 outputlabel->font->name, outputlabel->font->size, 4 1 static_cast< FontStyle >( 4 private: 2 outputlabel->font->style ^ FontStyle::Italic ) ); 48 /// <summary> } 4 /// Required designer variable. 4 }; 0 /// </summary> } 8 1 // Fig. 12.28: 2 // CheckBox demonstration. #include "" #include <windows.h> 8 using namespace CheckBoxTest; 1 int ncmdshow) 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } 40 CheckBoxes es and RadioButtons RadioButton Description / Delegate and Event Arguments properties and events Checked Specifies whether the radio button is checked. displayed to the right of the radio button (called the label). Common Events (Delegate EventHandler, event arguments EventArgs) Click Raised when user clicks the control. CheckedChanged Raised every time the radio button is checked or unchecked. Fig. 1 RadioButton properties and events. 1 // Fig. 12.0: 2 // Using RadioButtons to set message window options. 4 #pragma once namespace RadioButtonTest 8 { using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 1 using namespace System::Data; 14 using namespace System::Drawing; 1 1 /// <summary> 1 /// Summary for Form1 18 /// 1 /// WARNING: If you change the name of this class, you will need to 20 /// change the Resource File Name property for the managed 21 /// resource compiler tool associated with all.resx files 22 /// this class depends on. Otherwise, the designers will not 2 /// be able to interact properly with localized resources 24 /// associated with this form. 2 /// </summary> 1 of 41 42
2 public gc class Form1 : public System::Windows::Forms::Form 2 { 28 public: 1 private: System::Windows::Forms::RadioButton * questionbutton; 2 private: System::Windows::Forms::RadioButton * informationbutton; private: System::Windows::Forms::RadioButton * exclamationbutton; 2 Form1(void) 0 { 1 InitializeComponent(); 2 } 2 of 4 private: System::Windows::Forms::RadioButton * errorbutton; private: System::Windows::Forms::Label * displaylabel; private: System::Windows::Forms::Button * displaybutton; private: System::Windows::Forms::Label * promptlabel; of 8 4 protected: private: static MessageBoxIcon icontype = MessageBoxIcon::Error; void Dispose(Boolean disposing) 0 private: static MessageBoxButtons buttontype = MessageBoxButtons::OK; { 1 if (disposing && components) 2 private: 8 { /// <summary> components->dispose(); 4 /// Required designer variable. 40 } 41 super::dispose(disposing); 42 } 4 private: System::Windows::Forms::GroupBox * buttontypegroupbox; 44 private: System::Windows::Forms::RadioButton * retrycancelbutton; 4 private: System::Windows::Forms::RadioButton * yesnobutton; 4 private: System::Windows::Forms::RadioButton * yesnocancelbutton; 4 private: System::Windows::Forms::RadioButton * abortretryignorebutton; 48 private: System::Windows::Forms::RadioButton * okcancelbutton; 4 private: System::Windows::Forms::RadioButton * okbutton; 0 private: System::Windows::Forms::GroupBox * icontypegroupbox; /// </summary> System::ComponentModel::Container * components; 8 // Visual Studio.NET generated GUI code 0 // change button based on option chosen by sender 1 private: System::Void buttontype_checkedchanged( 2 System::Object * sender, System::EventArgs * e) { 4 if ( sender == okbutton ) // display OK button buttontype = MessageBoxButtons::OK; 4 44 // display OK and Cancel buttons 8 else if ( sender == okcancelbutton ) 8 // change icon based on option chosen by sender private: System::Void icontype_checkedchanged( 100 System::Object * sender, System::EventArgs * e) buttontype = MessageBoxButtons::OKCancel; 80 81 // display Abort, Retry and Ignore buttons 4 of 101 { 102 if ( sender == errorbutton ) // display error icon 10 icontype = MessageBoxIcon::Error; of 82 else if ( sender == abortretryignorebutton ) 104 8 buttontype = MessageBoxButtons::AbortRetryIgnore; 10 // display exclamation point 84 10 else if ( sender == exclamationbutton ) 8 // display Yes, No and Cancel buttons 10 icontype = MessageBoxIcon::Exclamation; 8 else if ( sender == yesnocancelbutton ) 108 8 buttontype = MessageBoxButtons::YesNoCancel; 10 // display information icon 88 110 else if ( sender == informationbutton ) 8 // display Yes and No buttons 111 icontype = MessageBoxIcon::Information; 0 else if ( sender == yesnobutton ) 112 1 buttontype = MessageBoxButtons::YesNo; 11 else // only one option left--display question mark 2 114 icontype = MessageBoxIcon::Question; // only one option left--display Retry and Cancel buttons 11 } // end method icontype_checkedchanged 4 else 11 buttontype = MessageBoxButtons::RetryCancel; 11 // display MessageBox and button user pressed } // end method buttontype_checkedchanged 118 private: System::Void displaybutton_click( 11 System::Object * sender, System::EventArgs * e) 120 { 121 DialogResult = MessageBox::Show( 122 S"This is Your Custom MessageBox.", 12 S"Custom MessageBox", buttontype, icontype ); 4 4 124 12 // check for dialog result and display it in label 12 switch ( DialogResult ) { 148 case DialogResult::Yes: 14 displaylabel-> = S"Yes was pressed."; 10 break; 12 128 case DialogResult::OK: 12 displaylabel-> = S"OK was pressed."; of 11 12 case DialogResult::No: 1 displaylabel-> = S"No was pressed."; of 10 break; 14 break; 11 1 } // end switch 12 case DialogResult::Cancel: 1 } // end method displaybutton_click 1 displaylabel-> = S"Cancel was pressed."; 1 }; 14 break; 18 } 1 1 case DialogResult::Abort: 1 displaylabel-> = S"Abort was pressed."; 18 break; 1 140 case DialogResult::Retry: 141 displaylabel-> = S"Retry was pressed."; 142 break; 14 144 case DialogResult::Ignore: 14 displaylabel-> = S"Ignore was pressed."; 14 break; 14 4 48
1 // Fig. 12.1: 2 // RadioButton demonstration. #include "" #include <windows.h> 8 using namespace RadioButtonTest; 1 int ncmdshow) 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } Exclamation icon type Error icon type OKCancel button type OK button type 4 0 Information icon type AbortRetryIgnore button type Question icon type YesNoCancel button type Class PictureBox Displays an image PictureBoxes es Image set by object of class Image. The Image property sets the Image object to use SizeMode property sets how the image is displayed YesNo button type RetryCancel button type 1 2 PictureBox properties and events Image SizeMode PictureBoxes es Description / Delegate and Event Arguments Image to display in the picture box. Gets value from enumeration PictureBoxSizeMode that controls image sizing and positioning. Values Normal (default), StretchImage, AutoSize and CenterImage. Normal puts image in top-left corner of picture box, and CenterImage puts image in middle. (Both cut off image if too large.) StretchImage resizes image to fit in picture box. AutoSize resizes picture box to hold image. Common Events (Delegate EventHandler, event arguments EventArgs) Click Raised when user clicks the control. Fig. 1 PictureBox properties and events. 1 // Fig. 12.: 2 // Using a PictureBox to display images. 4 #pragma once namespace PictureBoxTest 8 { using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 1 using namespace System::Data; 14 using namespace System::Drawing; 1 using namespace System::IO; 1 1 /// <summary> 18 /// Summary for Form1 1 /// 20 /// WARNING: If you change the name of this class, you will need to 21 /// change the Resource File Name property for the managed 22 /// resource compiler tool associated with all.resx files 2 /// this class depends on. Otherwise, the designers will not 24 /// be able to interact properly with localized resources 2 /// associated with this form. 1 of 4
2 /// </summary> 2 public gc class Form1 : public System::Windows::Forms::Form 28 { 0 /// <summary> 1 /// Required designer variable. 2 /// </summary> 2 public: 0 Form1(void) 1 { 2 InitializeComponent(); 2 of System::ComponentModel::Container * components; 4 // Visual Studio.NET generated GUI code of } // change image whenever PictureBox clicked 4 8 private: System::Void imagepicturebox_click( protected: System::Object * sender,system::eventargs * e) void Dispose(Boolean disposing) 0 { { 1 imagenum = ( imagenum + 1 ) % ; // imagenum from 0 to 2 8 if (disposing && components) 2 { // create Image object from file, display on PictureBox 40 components->dispose(); 41 } 42 super::dispose(disposing); 4 } 44 private: System::Windows::Forms::Label * promptlabel; 4 private: System::Windows::Forms::PictureBox * imagepicturebox; 4 4 imagepicturebox->image = Image::FromFile( String::Concat( Directory::GetCurrentDirectory(), S"\\images\\image", imagenum.tostring(), S".bmp" ) ); } 8 }; } 4 private: static int imagenum = -1; 48 4 private: 1 // Fig. 12.4: 2 // PictureBox demonstration. #include "" #include <windows.h> 8 using namespace PictureBoxTest; 1 int ncmdshow) 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } Mouse Event Handling Class MouseEventArgs Contain coordinates of the mouse pointer The mouse pressed Number of clicks Number of notches the wheel turned Passing mouse event Mouse event-handling methods take an object and MouseEventArgs object as argument The Click event uses delegate EventHandler and event arguments EventArgs 8 Mouse Events, Delegates and Event Arguments Mouse Events (Delegate EventHandler, event arguments EventArgs) MouseEnter MouseLeave Mouse Events (Delegate MouseEventHandler, event arguments MouseEventArgs) MouseDown MouseHover MouseMove MouseUp Class MouseEventArgs Properties Button Clicks Mouse Event Handling Raised if the mouse cursor enters the area of the control. Raised if the mouse cursor leaves the area of the control. Raised if the mouse button (either mouse button) is pressed while its cursor is over the area of the control. Raised if the mouse cursor hovers over the area of the control. Raised if the mouse cursor is moved while in the area of the control. Raised if the mouse button (either mouse button) is released when the cursor is over the area of the control. Mouse button that was pressed (left, right, middle or none). The number of times the mouse button (either mouse button) was clicked. X The x-coordinate of the event, relative to the control. Y The y-coordinate of the event, relative to the control. Fig. 18 Mouse events, delegates and event arguments. 1 // Fig. 12.: 2 // Using the mouse to draw on a form. 4 #pragma once namespace PainterTest 8 { using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 1 using namespace System::Data; 14 using namespace System::Drawing; 1 1 /// <summary> 1 /// Summary for Form1 18 /// 1 /// WARNING: If you change the name of this class, you will need to 20 /// change the Resource File Name property for the managed 21 /// resource compiler tool associated with all.resx files 22 /// this class depends on. Otherwise, the designers will not 2 /// be able to interact properly with localized resources 24 /// associated with this form. 2 /// </summary> 1 of 4 0
2 public gc class Form1 : public System::Windows::Forms::Form 2 { 28 public: 1 2 // Visual Studio.NET generated GUI code 2 Form1(void) 0 { 1 InitializeComponent(); 2 of 4 4 // should paint after mouse button has been pressed private: System::Void Form1_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) of 4 2 } { 8 shouldpaint = true; 4 protected: } void Dispose(Boolean disposing) 0 { 1 // stop painting when mouse button released if (disposing && components) 2 private: System::Void Form1_MouseUp(System::Object * sender, 8 { System::Windows::Forms::MouseEventArgs * e) components->dispose(); 4 { 40 } shouldpaint = false; 41 super::dispose(disposing); } 42 } 4 44 private: static bool shouldpaint = false; // whether to paint 4 4 private: 4 /// <summary> 48 /// Required designer variable. 4 /// </summary> 0 System::ComponentModel::Container * components; 1 2 8 // draw circle whenever mouse button moves (and mouse is down) private: System::Void Form1_MouseMove(System::Object * sender, 0 System::Windows::Forms::MouseEventArgs * e) 1 // Fig. 12.: 2 // Mouse event handling demonstration. 1 { 2 if ( shouldpaint ) { Graphics *graphics = CreateGraphics(); 4 graphics->fillellipse( new SolidBrush( 4 of 4 #include "" #include <windows.h> Color::BlueViolet ), e->x, e->y, 4, 4 ); 8 using namespace PainterTest; } // end if } 8 }; } 1 int ncmdshow) 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } 4 Keyboard Event Handling Key events Control that inherits from System::Windows::Forms::Control Delegate KeyPressEventHandler Event argument KeyPressEventArgs KeyPress ASCII character pressed No modifier keys Delegate KeyEventHandler Event argument KeyEventArgs KeyUp or KeyDown Special modifier keys Key enumeration value Keyboard Event Handling Keyboard Events, Delegates and Event Arguments Key Events (Delegate KeyEventHandler, event arguments KeyEventArgs) KeyDown Raised when key is initially pushed down. KeyUp Raised when key is released. Key Events (Delegate KeyPressEventHandler, event arguments KeyPressEventArgs) KeyPress Raised when key is pressed. Occurs repeatedly while key is held down, at a rate specified by the operating system. Class KeyPressEventArgs Properties KeyChar Returns the ASCII character for the key pressed. Handled Indicates whether the KeyPress event was handled (i.e., has an event handler associated with it). Class KeyEventArgs Properties Alt Indicates whether the Alt key was pressed. Control Indicates whether the Control key was pressed. Shift Indicates whether the Shift key was pressed. Fig. 1 Keyboard events, delegates and event arguments. (1 of 2)
Keyboard Event Handling Handled Indicates whether the event was handled (i.e., has an event handler associated with it). KeyCode Returns the key code for the key, as a Keys enumeration. This does not include modifier key information. Used to test for a specific key. KeyData Returns the key code as a Keys enumeration, combined with modifier information. Used to determine all information about the key pressed. KeyValue Returns the key code as an int, rather than as a Keys enumeration. Used to obtain a numeric representation of the key pressed. Modifiers Returns a Keys enumeration for any modifier keys pressed (Alt, Control and Shift). Used to determine modifier key information only. Fig. 20 Keyboard events, delegates and event arguments. Fig. 20 Keyboard events, delegates and event arguments. (2 of 2) 1 // Fig. 12.: 2 // Displaying information about the key the user pressed. 4 #pragma once namespace KeyDemoTest 8 { using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 1 using namespace System::Data; 14 using namespace System::Drawing; 1 1 /// <summary> 1 /// Summary for Form1 18 /// 1 /// WARNING: If you change the name of this class, you will need to 20 /// change the Resource File Name property for the managed 21 /// resource compiler tool associated with all.resx files 22 /// this class depends on. Otherwise, the designers will not 2 /// be able to interact properly with localized resources 24 /// associated with this form. 2 /// </summary> 1 of 4 8 2 public gc class Form1 : public System::Windows::Forms::Form 2 { 28 public: 1 2 // Visual Studio.NET generated GUI code 2 Form1(void) 0 { 1 InitializeComponent(); 2 of 4 4 // display the name of the pressed key private: System::Void Form1_KeyPress(System::Object * sender, System::Windows::Forms::KeyPressEventArgs * e) of 4 2 } { 8 charlabel-> = String::Concat( S"Key pressed: ", 4 protected: ( e->keychar ).ToString() ); void Dispose(Boolean disposing) 0 } { 1 if (disposing && components) 2 // display modifier keys, key code, key data and key value 8 { private: System::Void Form1_KeyDown(System::Object * sender, components->dispose(); 4 System::Windows::Forms::KeyEventArgs * e) 40 } { 41 super::dispose(disposing); keyinfolabel-> = String::Concat( 42 } S"Alt: ", ( e->alt? S"Yes" : S"No" ), S"\n", 4 private: System::Windows::Forms::Label * charlabel; 8 S"Shift: ", ( e->shift? S"Yes" : S"No" ), S"\n", 44 private: System::Windows::Forms::Label * keyinfolabel; S"Ctrl: ", ( e->control? S"Yes" : S"No" ), S"\n", 4 0 S"KeyCode: ", box( e->keycode ), S"\n", 4 private: 1 S"KeyData: ", box( e->keydata ), S"\n", 4 /// <summary> 2 S"KeyValue: ", e->keyvalue ); 48 /// Required designer variable. } 4 /// </summary> 4 0 System::ComponentModel::Container * components; 0 // clear labels when key released private: System::Void Form1_KeyUp(System::Object * sender, System::Windows::Forms::KeyEventArgs * e) 1 // Fig. 12.40: 2 // Keyboard event handling demonstration. 8 { keyinfolabel-> = S""; 80 charlabel-> = S""; 4 of 4 #include "" #include <windows.h> 81 } 82 }; 8 using namespace KeyDemoTest; 8 } 1 int ncmdshow) 1 System::Threading::Thread::CurrentThread->ApartmentState = 1 System::Threading::ApartmentState::STA; 1 Application::Run(new Form1()); 1 } 1 2