
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Code examples for creating a form in c# with menus and handling events such as menu clicks and color changes. It includes creating a mainmenu, menuitem, and setting event handlers for menu clicks and color changes.
Typology: Slides
1 / 40
This page cannot be seen from the preview
Don't miss anything!

































1 // Fig. 12.33: Form1.h 2 // Using a PictureBox to display images. 3 4 #pragma once 5 6 7 namespace PictureBoxTest 8 { 9 using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 13 using namespace System::Data; 14 using namespace System::Drawing; 15 using namespace System::IO; 16 17 ///
26 /// 27 public __gc class Form1 : public System::Windows::Forms::Form 28 { 29 public: 30 Form1(void) 31 { 32 InitializeComponent(); 33 } 34 35 protected: 36 void Dispose(Boolean disposing) 37 { 38 if (disposing && components) 39 { 40 components->Dispose(); 41 } 42 __super::Dispose(disposing); 43 } 44 private: System::Windows::Forms::Label * promptLabel; 45 private: System::Windows::Forms::PictureBox * imagePictureBox; 46 47 private: static int imageNum = - 1 ; 48 49 private:
1 // Fig. 12.34: Form1.cpp 2 // PictureBox demonstration. 3 4 #include "stdafx.h" 5 #include "Form1.h" 6 #include <windows.h> 7 8 using namespace PictureBoxTest; 9 10 int APIENTRY _tWinMain(HINSTANCE hInstance, 11 HINSTANCE hPrevInstance, 12 LPTSTR lpCmdLine, 13 int nCmdShow) 14 { 15 System::Threading::Thread::CurrentThread->ApartmentState = 16 System::Threading::ApartmentState::STA; 17 Application::Run(new Form1()); 18 return 0 ; 19 }
1 // Fig. 12.36: Form1.h 2 // Using the mouse to draw on a form. 3 4 #pragma once 5 6 7 namespace PainterTest 8 { 9 using namespace System; 10 using namespace System::ComponentModel; 11 using namespace System::Collections; 12 using namespace System::Windows::Forms; 13 using namespace System::Data; 14 using namespace System::Drawing; 15 16 ///
26 public __gc class Form1 : public System::Windows::Forms::Form 27 { 28 public: 29 Form1(void) 30 { 31 InitializeComponent(); 32 } 33 34 protected: 35 void Dispose(Boolean disposing) 36 { 37 if (disposing && components) 38 { 39 components->Dispose(); 40 } 41 __super::Dispose(disposing); 42 } 43 44 private: static bool shouldPaint = false; // whether to paint 45 46 private: 47 ///
68 // draw circle whenever mouse button moves (and mouse is down) 69 private: System::Void Form1_MouseMove(System::Object * sender, 70 System::Windows::Forms::MouseEventArgs * e) 71 { 72 if ( shouldPaint ) { 73 Graphics *graphics = CreateGraphics(); 74 graphics->FillEllipse( new SolidBrush( 75 Color::BlueViolet ), e->X, e->Y, 4 , 4 ); 76 } // end if 77 } 78 }; 79 }
1 // Fig. 12.37: Form1.cpp 2 // Mouse event handling demonstration. 3 4 #include "stdafx.h" 5 #include "Form1.h" 6 #include <windows.h> 7 8 using namespace PainterTest; 9 10 int APIENTRY _tWinMain(HINSTANCE hInstance, 11 HINSTANCE hPrevInstance, 12 LPTSTR lpCmdLine, 13 int nCmdShow) 14 { 15 System::Threading::Thread::CurrentThread->ApartmentState = 16 System::Threading::ApartmentState::STA; 17 Application::Run(new Form1()); 18 return 0 ; 19 }
Keyboard events, delegates and event arguments. (1 of 2)
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. 12.38 Keyboard events, delegates and event arguments.
Keyboard events, delegates and event arguments. (2 of 2)
26 public __gc class Form1 : public System::Windows::Forms::Form 27 { 28 public: 29 Form1(void) 30 { 31 InitializeComponent(); 32 } 33 34 protected: 35 void Dispose(Boolean disposing) 36 { 37 if (disposing && components) 38 { 39 components->Dispose(); 40 } 41 __super::Dispose(disposing); 42 } 43 private: System::Windows::Forms::Label * charLabel; 44 private: System::Windows::Forms::Label * keyInfoLabel; 45 46 private: 47 ///
52 // Visual Studio .NET generated GUI code 53 54 // display the name of the pressed key 55 private: System::Void Form1_KeyPress(System::Object * sender, 56 System::Windows::Forms::KeyPressEventArgs * e) 57 { 58 charLabel->Text = String::Concat( S"Key pressed: ", 59 ( e->KeyChar ).ToString() ); 60 } 61 62 // display modifier keys, key code, key data and key value 63 private: System::Void Form1_KeyDown(System::Object * sender, 64 System::Windows::Forms::KeyEventArgs * e) 65 { 66 keyInfoLabel->Text = String::Concat( 67 S"Alt: ", ( e->Alt? S"Yes" : S"No" ), S"\n", 68 S"Shift: ", ( e->Shift? S"Yes" : S"No" ), S"\n", 69 S"Ctrl: ", ( e->Control? S"Yes" : S"No" ), S"\n", 70 S"KeyCode: ", __box( e->KeyCode ), S"\n", 71 S"KeyData: ", __box( e->KeyData ), S"\n", 72 S"KeyValue: ", e->KeyValue ); 73 } 74