]>
Commit | Line | Data |
---|---|---|
01111366 RR |
1 | /* |
2 | * Program: image | |
3 | * | |
4 | * Author: Robert Roebling | |
5 | * | |
6 | * Copyright: (C) 1998, Robert Roebling | |
7 | * | |
8 | */ | |
9 | ||
10 | #include "wx/wx.h" | |
11 | #include "wx/image.h" | |
12 | ||
13 | // derived classes | |
14 | ||
15 | class MyFrame; | |
16 | class MyApp; | |
17 | ||
18 | // MyCanvas | |
19 | ||
20 | class MyCanvas: public wxScrolledWindow | |
21 | { | |
22 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
23 | ||
24 | public: | |
25 | ||
26 | MyCanvas(void) {}; | |
27 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); | |
28 | ~MyCanvas(void); | |
29 | void OnPaint( wxPaintEvent &event ); | |
30 | ||
31 | wxBitmap *my_horse; | |
e8fdc264 | 32 | wxBitmap *my_square; |
01111366 RR |
33 | |
34 | DECLARE_EVENT_TABLE() | |
35 | }; | |
36 | ||
37 | // MyFrame | |
38 | ||
39 | class MyFrame: public wxFrame | |
40 | { | |
41 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
42 | ||
43 | public: | |
44 | ||
45 | MyFrame(void); | |
46 | void OnSize( wxSizeEvent &event ); | |
47 | void OnAbout( wxCommandEvent &event ); | |
48 | void OnQuit( wxCommandEvent &event ); | |
49 | ||
50 | MyCanvas *m_canvas; | |
51 | ||
52 | DECLARE_EVENT_TABLE() | |
53 | }; | |
54 | ||
55 | // MyApp | |
56 | ||
57 | class MyApp: public wxApp | |
58 | { | |
59 | public: | |
60 | ||
61 | MyApp(void); | |
62 | virtual bool OnInit(void); | |
63 | }; | |
64 | ||
65 | // main program | |
66 | ||
67 | IMPLEMENT_APP(MyApp) | |
68 | ||
69 | // MyCanvas | |
70 | ||
71 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
72 | ||
73 | BEGIN_EVENT_TABLE(MyCanvas,wxScrolledWindow) | |
74 | EVT_PAINT (MyCanvas::OnPaint) | |
75 | END_EVENT_TABLE() | |
76 | ||
77 | MyCanvas::MyCanvas( wxWindow *parent, const wxWindowID id, const wxPoint &pos, const wxSize &size ) | |
78 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER ) | |
79 | { | |
80 | wxImage image; | |
910276d7 | 81 | |
dc86cb34 RR |
82 | wxBitmap bitmap( 100, 100 ); |
83 | ||
84 | wxMemoryDC dc; | |
85 | dc.SelectObject( bitmap ); | |
910276d7 | 86 | dc.SetBrush( wxBrush( "orange", wxSOLID ) ); |
e8fdc264 | 87 | dc.SetPen( *wxWHITE_PEN ); |
dc86cb34 RR |
88 | dc.DrawRectangle( 0, 0, 100, 100 ); |
89 | dc.SelectObject( wxNullBitmap ); | |
90 | ||
91 | image = bitmap.ConvertToImage(); | |
92 | image.SaveFile( "../test.png", wxBITMAP_TYPE_PNG ); | |
910276d7 | 93 | |
e8fdc264 | 94 | image.LoadFile( "../horse.png", wxBITMAP_TYPE_PNG ); |
910276d7 | 95 | my_horse = new wxBitmap( image ); |
e8fdc264 RR |
96 | |
97 | image.LoadFile( "../test.png", wxBITMAP_TYPE_PNG ); | |
98 | my_square = new wxBitmap( image ); | |
01111366 RR |
99 | } |
100 | ||
101 | MyCanvas::~MyCanvas(void) | |
102 | { | |
103 | delete my_horse; | |
e8fdc264 | 104 | delete my_square; |
01111366 RR |
105 | } |
106 | ||
107 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
108 | { | |
109 | wxPaintDC dc( this ); | |
110 | PrepareDC( dc ); | |
111 | ||
910276d7 | 112 | dc.DrawText( "Loaded image", 30, 100 ); |
e8fdc264 | 113 | if (my_square->Ok()) dc.DrawBitmap( *my_square, 30, 120 ); |
910276d7 RR |
114 | |
115 | dc.DrawText( "Drawn directly", 150, 100 ); | |
116 | dc.SetBrush( wxBrush( "orange", wxSOLID ) ); | |
e8fdc264 | 117 | dc.SetPen( *wxWHITE_PEN ); |
910276d7 | 118 | dc.DrawRectangle( 150, 120, 100, 100 ); |
e8fdc264 RR |
119 | |
120 | if (my_horse->Ok()) dc.DrawBitmap( *my_horse, 30, 240 ); | |
01111366 RR |
121 | } |
122 | ||
123 | // MyFrame | |
124 | ||
125 | const ID_QUIT = 108; | |
126 | const ID_ABOUT = 109; | |
127 | ||
128 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
129 | ||
130 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
131 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
132 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
133 | END_EVENT_TABLE() | |
134 | ||
135 | MyFrame::MyFrame(void) : | |
910276d7 | 136 | wxFrame( (wxFrame *) NULL, -1, "wxImage sample", wxPoint(20,20), wxSize(470,360) ) |
01111366 RR |
137 | { |
138 | wxMenu *file_menu = new wxMenu(); | |
139 | file_menu->Append( ID_ABOUT, "About.."); | |
140 | file_menu->Append( ID_QUIT, "Exit"); | |
141 | ||
142 | wxMenuBar *menu_bar = new wxMenuBar(); | |
143 | menu_bar->Append(file_menu, "File"); | |
144 | menu_bar->Show( TRUE ); | |
145 | ||
146 | SetMenuBar( menu_bar ); | |
147 | ||
917e533b RR |
148 | CreateStatusBar(2); |
149 | int widths[] = { -1, 100 }; | |
150 | SetStatusWidths( 2, widths ); | |
151 | ||
01111366 RR |
152 | m_canvas = new MyCanvas( this, -1, wxPoint(2,62), wxSize(300-4,120-4) ); |
153 | m_canvas->SetScrollbars( 10, 10, 50, 50 ); | |
154 | } | |
155 | ||
156 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
157 | { | |
158 | Close( TRUE ); | |
159 | } | |
160 | ||
161 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
162 | { | |
910276d7 | 163 | (void) wxMessageBox( "wxImage demo\nRobert Roebling (c) 1998", "About wxImage Demo", wxOK ); |
01111366 RR |
164 | } |
165 | ||
166 | //----------------------------------------------------------------------------- | |
167 | // MyApp | |
168 | //----------------------------------------------------------------------------- | |
169 | ||
170 | MyApp::MyApp(void) : | |
171 | wxApp( ) | |
172 | { | |
173 | } | |
174 | ||
175 | bool MyApp::OnInit(void) | |
176 | { | |
177 | wxImage::AddHandler( new wxPNGHandler ); | |
178 | ||
179 | wxFrame *frame = new MyFrame(); | |
180 | frame->Show( TRUE ); | |
181 | ||
182 | return TRUE; | |
183 | } | |
184 | ||
185 | ||
186 | ||
187 | ||
188 |