Download presentation
Presentation is loading. Please wait.
Published byErica Rogers Modified over 9 years ago
1
Copyright © 2007-2014 – Curt Hill Building Windows Applications in wxDev-C++
2
Introduction The wxDev system is a very powerful code generator It comes with a framework: a library of visual components A framework is a large collection of objects that are integrated together Copyright © 2007-2014 – Curt Hill
3
Windows programming Levels of code (recall from previous presentation): –First is Windows –Second is the WinMain - yours –Third is back to Windows –Fourth is your event handlers Source code files –There are several files that are generated –There are two (or three) that you actually modify Copyright © 2007-2014 – Curt Hill
4
Picture of Code Levels Copyright © 2007-2014 – Curt Hill Operating System WinMain Event Handler
5
Background A component is an object that encapsulates a particular window thing such as: –Menu item –Dialog box –Radio button –Check box –Edit box –Button Copyright © 2007-2014 – Curt Hill
6
Characteristics of Components Components are objects, instances of a class Each component may have three kinds of things: –Properties –Methods –Events Copyright © 2007-2014 – Curt Hill
7
Properties Variables that belong to an object –Properties determine most of the behavior of the item –There is almost always a reasonable default –We may change them using the property inspector or through code –The most important property is the name –Some are modifiable at design time, others at run time, most at both –EG the height of a window is a property Copyright © 2007-2014 – Curt Hill
8
Methods Methods are object member functions They allow us to initiate and move along the process They usually have simple parameters –The member data does most of the data transfer Copyright © 2007-2014 – Curt Hill
9
Events Events are reactions to user actions on the interface Events are unpredictable –Unlike the console interface For example what do you do when they click a menu? This is an event, which starts a method or member function Copyright © 2007-2014 – Curt Hill
10
Using Properties The properties (variables) and methods (functions) belong to the object or component We cannot access these directly –Must use a dereferencing item So you use a pointer plus field notation x->y –Where x is the name of the component and y is the name of either the property or method Copyright © 2007-2014 – Curt Hill
11
The process of building a Windows program Create a new project, an application Save it under a new name –Create a directory just for this project Name after the goalie –Save the unit and project under different names –There are two C++ files, so must have different names Next screens show this for wxDev C++ Copyright © 2007-2014 – Curt Hill
12
New Project Copyright © 2007-2014 – Curt Hill
13
Choose wxWidgets Frame Copyright © 2007-2014 – Curt Hill
14
Creating New wxFrame This gives a project with an wxFrame –Do not use any of the others Fill in the project name on the bottom When you save it you get the following dialog –Directory should have same name as project Copyright © 2007-2014 – Curt Hill
15
Save Project Copyright © 2007-2014 – Curt Hill
16
Create a new directory Each project should be in its own directory When you click on new button you get the following screen Prefer that the directory name is the goalie name Copyright © 2007-2014 – Curt Hill
17
Naming Directory Copyright © 2007-2014 – Curt Hill
18
After rename Move into this new directory by clicking on it If you lose the opportunity to rename, just right click Copyright © 2007-2014 – Curt Hill
19
Save Project Copyright © 2007-2014 – Curt Hill
20
New wxFrame Dialog Copyright © 2007-2014 – Curt Hill
21
Finally Copyright © 2007-2014 – Curt Hill
22
Project Files There are many –FirstGUIApp.cpp –FirstGUIApp.h –FirstGUIApp.rc –FirstGUIFrm.cpp –FirstGUIFrm.h –FirstGUIFrm.wxfrm Because of the multitude we will zip entire directories to hand these in –Discuss this later Copyright © 2007-2014 – Curt Hill
23
Demo Program For the duration of the demonstration we need the program to do something This program will read in a Fahrenheit temperature and output the equivalent Celsius temperature We will need: –Two Labels, one Edit, three buttons Copyright © 2007-2014 – Curt Hill
24
Add components Pull the component from the palette –Click the component –Drag the place where it should be on the form Some give you a choice –Labels and buttons must be positioned and sized –A menu always is in the menu position, upper left part of window –A common file dialog box can be put anywhere because it does not show Copyright © 2007-2014 – Curt Hill
25
Add components (continued) Customize it by changing needed properties –Start with the property inspector –Most of this will be done here –The second tab of the property inspector is events Test it –Run and see how it looks –Even before any actions Copyright © 2007-2014 – Curt Hill
26
Important components Button Label Edit MessageBox Others will be seen later Copyright © 2007-2014 – Curt Hill
27
Static Text wxStaticText is component name What is it? –Just text that adorns your window –Usually labeling that does not change, but you can change it in the code Where: Common Controls Copyright © 2007-2014 – Curt Hill
28
Common Controls Copyright © 2007-2014 – Curt Hill
29
Static Text properties: Label –Usually will generate LabelN, where N is the number of these that you currently have done –The contents of the text box –The caption is a String field Name –Usually it will generate WxStaticTextN –This is fine if you do not reference from code Copyright © 2007-2014 – Curt Hill
30
Static Text properties: Alignment –Left or right or center Font –Most things that display have the font property positioning –Height –Left –Top –Width Copyright © 2007-2014 – Curt Hill
31
Dragging a Label Copyright © 2007-2014 – Curt Hill
32
Once in Place Copyright © 2007-2014 – Curt Hill
33
Customization Copyright © 2007-2014 – Curt Hill
34
Frame The Frame (main window) is also an object Its Title property is the Title Bar text This is also changed with the Property Inspector –Select the entire frame –Type in Title Default value for Captions and Texts is the variable name Copyright © 2007-2014 – Curt Hill
35
Edit box What is it? –The windows way to enter pieces of text or numeric data through the screen –Contains one line of text –It allows the user to type new things, edit old things or combination of both –Class name is wxTextCtrl Where: Common Controls Copyright © 2007-2014 – Curt Hill
36
Edit principal properties: Name: what you will call it in code Text: Its current contents –This can be set by Property Inspector at design time –At runtime we use GetValue() or SetValue(string) If it is numeric there is one more step –Converting to a numeric value –This requires a String method Copyright © 2007-2014 – Curt Hill
37
Conversion We have two problems that need handling Even if an edit contains a single digit it is not numeric Thus we need to be able to: –Convert a string to a number –Convert a number to a string We will need to see wxString to start to solve these problems Copyright © 2007-2014 – Curt Hill
38
wxString Not a visual component It is the type used in Text or Label objects A variable length string object The two methods we want today are: ToInt() ToDouble() These are separated by a dot rather than the -> we usually see Copyright © 2007-2014 – Curt Hill
39
An Example Suppose that we have: –An Edit box named Edit1 Extract the value from Edit1 double d = Edit1-> GetValue().ToDouble(); An error will occur if the value is not numeric Copyright © 2007-2014 – Curt Hill
40
Creating a String A wxString may be created by a cast wxString str = wxString(“Hi”); You may concatenate with the put to Thus: str = str << x << “ ” << y; Remember, comma does not do what you want This may be done without declaring a variable Copyright © 2007-2014 – Curt Hill
41
The One Step You may set a label and create the label in one statement Suppose lab is a static text item: lab-> SetLabelText( wxString("Fahrenheit: ") <<f<<", Celcius: "<<c); Copyright © 2007-2014 – Curt Hill
42
Once more on String It will not accept the concatenation of a string and number Thus Edit->SetLabelText(“x: ”<<x); will not work The quoted string is constant text and << is not overloaded for it Instead use C++ cast to make constant into String: Edit1->SetLabelText( wxString(“x: “)<<x); Copyright © 2007-2014 – Curt Hill
43
Button What is it? –A control which may be clicked –Usually labeling that does not change, but you can change it in the code Where: Common Controls The principal properties: –Label: text of button –Name: Your name instead of Button1 –Event: Onclick Copyright © 2007-2014 – Curt Hill
44
Next Adding Edits and Buttons is mostly the same as Labels A button must have an event handler Let consider the creation of this Copyright © 2007-2014 – Curt Hill
45
Button has been drug Copyright © 2007-2014 – Curt Hill
46
Caption Changed Copyright © 2007-2014 – Curt Hill
47
Select Event Tab Copyright © 2007-2014 – Curt Hill
48
Select New Function Copyright © 2007-2014 – Curt Hill
49
Code Pane Copyright © 2007-2014 – Curt Hill
50
Event Handler When you allow DevC++ to build a new event handler, it creates a blank method We now need to type in the code for this Our process is as follows: –Get the data –Do calculations –Display the result Copyright © 2007-2014 – Curt Hill
51
The Code Copyright © 2007-2014 – Curt Hill
52
Good Practice Every good Windows program should have three options –In menu or buttons These are: –Do the calculation –Exit the program –Show who is responsible Lets now do the last two Copyright © 2007-2014 – Curt Hill
53
Exit We will need a button In the event handler is the single statement: Destroy(); If Destroy is executed it will end the program Copyright © 2007-2014 – Curt Hill
54
About Every good windows needs either a Help menu with an About sub-menu or a button that does the same We will use a MessageBox for this Copyright © 2007-2014 – Curt Hill
55
MessageBoxW Displays a UniCode string in a predefind dialog box Quick dialog box with very limited controls Only allow some static text and several pre-labeled buttons There are four parameters Copyright © 2007-2014 – Curt Hill
56
Example MessageBoxW(NULL,,,MB_OK) Four parameters: –NULL, all in caps –Text in the box –Caption of dialog box –ID which indicates type of buttons: MB_OK Several other as well MB_OKCANCEL This function returns something based on button pressed Copyright © 2007-2014 – Curt Hill
57
UniCode and Windows MessageBoxes in recent versions of Windows use UniCode but before this used ASCII –They were just in quotes –Now need a L prefixing the string These are long character strings –Two bytes per character Prefix the quote with a L MessageBoxW(NULL, L”You have done wrong”, L”Error”, MB_OK); Copyright © 2007-2014 – Curt Hill
58
More Code The IDE generates lots of code in our program Our code generally starts at about line 70 Do not change the generated code! –You have to understand what it does to be able to modify it Copyright © 2007-2014 – Curt Hill
59
So Far Copyright © 2007-2014 – Curt Hill
60
Inheritance Our programs will use a variety of existing components These contain substantial behavior that we did nothing to provide For example our main window is a wxFrame component We did nothing to make it have a kill button Rather it inherited that functionality Copyright © 2007-2014 – Curt Hill
61
Other Thoughts You may name components any legal C++ name If the code will reference the component it is a good idea Avoid names that you will use for other things, such as Destroy Copyright © 2007-2014 – Curt Hill
62
Now Do This It is time for you to write this exact program We will write a program that will convert Fahrenheit temperatures and convert to Celsius using following formula Copyright © 2007-2014 – Curt Hill
63
What Did We Learn? How to make the IDE generate a Windows program Several components: –Labels, Edits, Buttons MessageBox New notation –-> and. Copyright © 2007-2014 – Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.