fixed MFC sample (bug 978194)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28550 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2004-07-30 23:01:04 +00:00
parent 3754265e32
commit 885ebd2b32
2 changed files with 87 additions and 118 deletions

View File

@@ -255,6 +255,7 @@ wxMSW:
- set wxKeyEvent::m_uniChar in Unicode build - set wxKeyEvent::m_uniChar in Unicode build
- support for alpha channel in toolbar bitmaps (Jurgen Doornik) - support for alpha channel in toolbar bitmaps (Jurgen Doornik)
- wxFileDialog can now be moved and centered (Randall Fox) - wxFileDialog can now be moved and centered (Randall Fox)
- restored (and improved) possibility to use wx with MFC broken in 2.5.2
wxUniv/X11: wxUniv/X11:

View File

@@ -28,19 +28,18 @@
// //
// IMPORTANT NOTES: // IMPORTANT NOTES:
// //
// (1) You need to set wxUSE_MFC to 1 in include/wx/msw/setup.h, which switches // (1) You may need to set wxUSE_MFC to 1 in include/wx/msw/setup.h but
// off some debugging features and also removes the windows.h inclusion // normally this shouldn't be needed any longer, i.e. it works without
// in wxprec.h (MFC headers don't like this to have been included previously). // it for me (VZ)
// Set to 'Use MFC in a shared DLL' or add _AFXDLL to preprocessor settings.
// Then recompile wxWidgets and this sample.
// //
// (2) I can't get the sample to link and run using a static MFC library, only the DLL // (2) You should link with MFC DLL, not static libraries
// version. Perhaps someone else is a wizard at working out the required settings
// in the wxWin library and the sample; then debugging the assert problem may be // suppress warning about WINVER not being defined from MFC
// easier. #ifndef WINVER
// #define WINVER 0x7000
// (3) Compiling wxWidgets in DLL mode currently includes windows.h, so you must only #endif
// try linking wxWidgets statically.
#include "stdafx.h"
// For compilers that support precompilation, includes "wx/wx.h". // For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h" #include "wx/wxprec.h"
@@ -49,21 +48,11 @@
#pragma hdrstop #pragma hdrstop
#endif #endif
#include "wx/wx.h" #ifndef WX_PRECOMP
#include "wx/wx.h"
#if !wxUSE_MFC
#error "Sorry, you need to edit include/wx/msw/setup.h, set wxUSE_MFC to 1, and recompile the library."
#endif #endif
#ifdef new #include "wx/evtloop.h"
#undef new
#endif
#include "stdafx.h"
#ifdef DrawText
#undef DrawText
#endif
#include "resource.h" #include "resource.h"
@@ -80,9 +69,11 @@ CTheApp theApp;
// Define a new application type // Define a new application type
class MyApp: public wxApp class MyApp: public wxApp
{ public: {
bool OnInit(void); public:
wxFrame *CreateFrame(void); virtual bool OnInit();
wxFrame *CreateFrame();
}; };
class MyCanvas: public wxScrolledWindow class MyCanvas: public wxScrolledWindow
@@ -97,33 +88,27 @@ public:
class MyChild: public wxFrame class MyChild: public wxFrame
{ {
public: public:
MyCanvas *canvas;
MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style); MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
~MyChild(void); virtual ~MyChild();
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnNew(wxCommandEvent& event); void OnNew(wxCommandEvent& event);
void OnActivate(wxActivateEvent& event); void OnActivate(wxActivateEvent& event);
MyCanvas *canvas;
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
// For drawing lines in a canvas
long xpos = -1;
long ypos = -1;
// ID for the menu quit command // ID for the menu quit command
#define HELLO_QUIT 1 #define HELLO_QUIT 1
#define HELLO_NEW 2 #define HELLO_NEW 2
DECLARE_APP(MyApp) DECLARE_APP(MyApp)
IMPLEMENT_APP(MyApp)
///////////////////////////////////////////////////////////////////////////// // notice use of IMPLEMENT_APP_NO_MAIN() instead of the usual IMPLEMENT_APP!
IMPLEMENT_APP_NO_MAIN(MyApp)
// CMainWindow constructor:
// Create the window with the appropriate style, size, menu, etc.
//
CMainWindow::CMainWindow() CMainWindow::CMainWindow()
{ {
LoadAccelTable( "MainAccelTable" ); LoadAccelTable( "MainAccelTable" );
@@ -131,13 +116,6 @@ CMainWindow::CMainWindow()
WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" ); WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
} }
// OnPaint:
// This routine draws the string "Hello, Windows!" in the center of the
// client area. It is called whenever Windows sends a WM_PAINT message.
// Note that creating a CPaintDC automatically does a BeginPaint and
// an EndPaint call is done when it is destroyed at the end of this
// function. CPaintDC's constructor needs the window (this).
//
void CMainWindow::OnPaint() void CMainWindow::OnPaint()
{ {
CString s = "Hello, Windows!"; CString s = "Hello, Windows!";
@@ -152,14 +130,6 @@ void CMainWindow::OnPaint()
s, s.GetLength() ); s, s.GetLength() );
} }
// OnAbout:
// This member function is called when a WM_COMMAND message with an
// IDM_ABOUT code is received by the CMainWindow class object. The
// message map below is responsible for this routing.
//
// We create a ClDialog object using the "AboutBox" resource (see
// hello.rc), and invoke it.
//
void CMainWindow::OnAbout() void CMainWindow::OnAbout()
{ {
CDialog about( "AboutBox", this ); CDialog about( "AboutBox", this );
@@ -189,21 +159,20 @@ ON_COMMAND( IDM_TEST, OnTest )
//}}AFX_MSG_MAP //}}AFX_MSG_MAP
END_MESSAGE_MAP() END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTheApp
// InitInstance:
// When any CTheApp object is created, this member function is automatically
// called. Any data may be set up at this point.
//
// Also, the main window of the application should be created and shown here.
// Return TRUE if the initialization is successful.
//
BOOL CTheApp::InitInstance() BOOL CTheApp::InitInstance()
{ {
SetDialogBkColor(); // hook gray dialogs (was default in MFC V1) if ( !CWinApp::InitInstance() )
return FALSE;
wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE); // TODO: cmd line parsing
WXDLLIMPEXP_BASE void wxSetInstance(HINSTANCE hInst);
wxSetInstance(m_hInstance);
wxApp::m_nCmdShow = m_nCmdShow;
int argc = 0;
char **argv = NULL;
wxEntryStart(argc, argv);
if ( !wxTheApp || !wxTheApp->CallOnInit() )
return FALSE;
#if START_WITH_MFC_WINDOW #if START_WITH_MFC_WINDOW
// Demonstrate creation of an initial MFC main window. // Demonstrate creation of an initial MFC main window.
@@ -225,37 +194,33 @@ BOOL CTheApp::InitInstance()
int CTheApp::ExitInstance() int CTheApp::ExitInstance()
{ {
// OnExit isn't called by CleanUp so must be called explicitly. if ( wxTheApp )
wxTheApp->OnExit(); wxTheApp->OnExit();
wxApp::CleanUp(); wxEntryCleanup();
return CWinApp::ExitInstance(); return CWinApp::ExitInstance();
} }
// Override this to provide wxWidgets message loop // Override this to provide wxWidgets message loop compatibility
// compatibility
BOOL CTheApp::PreTranslateMessage(MSG *msg) BOOL CTheApp::PreTranslateMessage(MSG *msg)
{ {
if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg)) wxEventLoop *evtLoop = wxEventLoop::GetActive();
if ( evtLoop && evtLoop->PreProcessMessage(msg) )
return TRUE; return TRUE;
else
return CWinApp::PreTranslateMessage(msg); return CWinApp::PreTranslateMessage(msg);
} }
BOOL CTheApp::OnIdle(LONG lCount) BOOL CTheApp::OnIdle(LONG WXUNUSED(lCount))
{ {
if (wxTheApp) return wxTheApp && wxTheApp->ProcessIdle();
return wxTheApp->ProcessIdle();
else
return FALSE;
} }
/********************************************************************* /*********************************************************************
* wxWidgets elements * wxWidgets elements
********************************************************************/ ********************************************************************/
bool MyApp::OnInit(void) bool MyApp::OnInit()
{ {
#if !START_WITH_MFC_WINDOW #if !START_WITH_MFC_WINDOW
@@ -268,7 +233,7 @@ bool MyApp::OnInit(void)
return TRUE; return TRUE;
} }
wxFrame *MyApp::CreateFrame(void) wxFrame *MyApp::CreateFrame()
{ {
MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300), MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
wxDEFAULT_FRAME_STYLE); wxDEFAULT_FRAME_STYLE);
@@ -309,13 +274,13 @@ BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
END_EVENT_TABLE() END_EVENT_TABLE()
// Define a constructor for my canvas // Define a constructor for my canvas
MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size): MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
wxScrolledWindow(parent, -1, pos, size) : wxScrolledWindow(parent, -1, pos, size)
{ {
} }
// Define the repainting behaviour // Define the repainting behaviour
void MyCanvas::OnPaint(wxPaintEvent& event) void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event))
{ {
wxPaintDC dc(this); wxPaintDC dc(this);
@@ -330,7 +295,6 @@ void MyCanvas::OnPaint(wxPaintEvent& event)
dc.DrawRoundedRectangle(150, 150, 100, 50, 20); dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
dc.DrawEllipse(250, 250, 100, 50); dc.DrawEllipse(250, 250, 100, 50);
dc.DrawSpline(50, 200, 50, 100, 200, 10);
dc.DrawLine(50, 230, 200, 230); dc.DrawLine(50, 230, 200, 230);
dc.DrawText("This is a test string", 50, 230); dc.DrawText("This is a test string", 50, 230);
} }
@@ -339,15 +303,19 @@ void MyCanvas::OnPaint(wxPaintEvent& event)
// the left button. // the left button.
void MyCanvas::OnMouseEvent(wxMouseEvent& event) void MyCanvas::OnMouseEvent(wxMouseEvent& event)
{ {
static long s_xpos = -1;
static long s_ypos = -1;
wxClientDC dc(this); wxClientDC dc(this);
dc.SetPen(* wxBLACK_PEN); dc.SetPen(* wxBLACK_PEN);
wxPoint pos = event.GetPosition(); wxPoint pos = event.GetPosition();
if (xpos > -1 && ypos > -1 && event.Dragging()) if (s_xpos > -1 && s_ypos > -1 && event.Dragging())
{ {
dc.DrawLine(xpos, ypos, pos.x, pos.y); dc.DrawLine(s_xpos, s_ypos, pos.x, pos.y);
} }
xpos = pos.x;
ypos = pos.y; s_xpos = pos.x;
s_ypos = pos.y;
} }
BEGIN_EVENT_TABLE(MyChild, wxFrame) BEGIN_EVENT_TABLE(MyChild, wxFrame)
@@ -356,22 +324,22 @@ BEGIN_EVENT_TABLE(MyChild, wxFrame)
EVT_ACTIVATE(MyChild::OnActivate) EVT_ACTIVATE(MyChild::OnActivate)
END_EVENT_TABLE() END_EVENT_TABLE()
MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style): MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style)
wxFrame(frame, -1, title, pos, size, style) : wxFrame(frame, -1, title, pos, size, style)
{ {
canvas = NULL; canvas = NULL;
} }
MyChild::~MyChild(void) MyChild::~MyChild()
{ {
} }
void MyChild::OnQuit(wxCommandEvent& event) void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
{ {
Close(TRUE); Close(TRUE);
} }
void MyChild::OnNew(wxCommandEvent& event) void MyChild::OnNew(wxCommandEvent& WXUNUSED(event))
{ {
CMainWindow *mainWin = new CMainWindow(); CMainWindow *mainWin = new CMainWindow();
mainWin->ShowWindow( TRUE ); mainWin->ShowWindow( TRUE );
@@ -392,7 +360,7 @@ CDummyWindow::CDummyWindow(HWND hWnd):CWnd()
} }
// Don't let the CWnd destructor delete the HWND // Don't let the CWnd destructor delete the HWND
CDummyWindow::~CDummyWindow(void) CDummyWindow::~CDummyWindow()
{ {
Detach(); Detach();
} }