MFC sample now works properly; WinMain not defined in wxMSW

if wxUSE_MFC is defined.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@18294 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2002-12-17 10:52:10 +00:00
parent 71ea40203d
commit 5f7a7263c3
2 changed files with 93 additions and 87 deletions

View File

@@ -89,31 +89,31 @@ CTheApp theApp;
// Define a new application type // Define a new application type
class MyApp: public wxApp class MyApp: public wxApp
{ public: { public:
bool OnInit(void); bool OnInit(void);
wxFrame *CreateFrame(void); wxFrame *CreateFrame(void);
}; };
class MyCanvas: public wxScrolledWindow class MyCanvas: public wxScrolledWindow
{ {
public: public:
MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size); MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size);
void OnPaint(wxPaintEvent& event); void OnPaint(wxPaintEvent& event);
void OnMouseEvent(wxMouseEvent& event); void OnMouseEvent(wxMouseEvent& event);
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
class MyChild: public wxFrame class MyChild: public wxFrame
{ {
public: public:
MyCanvas *canvas; 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); ~MyChild(void);
void OnQuit(wxCommandEvent& event); void OnQuit(wxCommandEvent& event);
void OnNew(wxCommandEvent& event); void OnNew(wxCommandEvent& event);
void OnActivate(wxActivateEvent& event); void OnActivate(wxActivateEvent& event);
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };
// For drawing lines in a canvas // For drawing lines in a canvas
@@ -151,13 +151,13 @@ void CMainWindow::OnPaint()
CString s = "Hello, Windows!"; CString s = "Hello, Windows!";
CPaintDC dc( this ); CPaintDC dc( this );
CRect rect; CRect rect;
GetClientRect( rect ); GetClientRect( rect );
dc.SetTextAlign( TA_BASELINE | TA_CENTER ); dc.SetTextAlign( TA_BASELINE | TA_CENTER );
dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) ); dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
dc.SetBkMode(TRANSPARENT); dc.SetBkMode(TRANSPARENT);
dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ), dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ),
s, s.GetLength() ); s, s.GetLength() );
} }
// OnAbout: // OnAbout:
@@ -176,8 +176,8 @@ void CMainWindow::OnAbout()
void CMainWindow::OnTest() void CMainWindow::OnTest()
{ {
wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK); wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK);
wxGetApp().CreateFrame(); wxGetApp().CreateFrame();
} }
// CMainWindow message map: // CMainWindow message map:
@@ -190,11 +190,11 @@ void CMainWindow::OnTest()
// receive no arguments and are void of return type, e.g., "void OnAbout()". // receive no arguments and are void of return type, e.g., "void OnAbout()".
// //
BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd ) BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
//{{AFX_MSG_MAP( CMainWindow ) //{{AFX_MSG_MAP( CMainWindow )
ON_WM_PAINT() ON_WM_PAINT()
ON_COMMAND( IDM_ABOUT, OnAbout ) ON_COMMAND( IDM_ABOUT, OnAbout )
ON_COMMAND( IDM_TEST, OnTest ) ON_COMMAND( IDM_TEST, OnTest )
//}}AFX_MSG_MAP //}}AFX_MSG_MAP
END_MESSAGE_MAP() END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
@@ -210,32 +210,32 @@ END_MESSAGE_MAP()
BOOL CTheApp::InitInstance() BOOL CTheApp::InitInstance()
{ {
TRACE( "HELLO WORLD\n" ); TRACE( "HELLO WORLD\n" );
SetDialogBkColor(); // hook gray dialogs (was default in MFC V1) SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE); wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE);
/* /*
m_pMainWnd = new CMainWindow(); m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow( m_nCmdShow ); m_pMainWnd->ShowWindow( m_nCmdShow );
m_pMainWnd->UpdateWindow(); m_pMainWnd->UpdateWindow();
*/ */
if (wxTheApp && wxTheApp->GetTopWindow()) if (wxTheApp && wxTheApp->GetTopWindow())
{ {
m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND()); m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND());
} }
return TRUE; return TRUE;
} }
int CTheApp::ExitInstance() int CTheApp::ExitInstance()
{ {
// OnExit isn't called by CleanUp so must be called explicitly. // OnExit isn't called by CleanUp so must be called explicitly.
wxTheApp->OnExit(); wxTheApp->OnExit();
wxApp::CleanUp(); wxApp::CleanUp();
return CWinApp::ExitInstance(); return CWinApp::ExitInstance();
} }
// Override this to provide wxWindows message loop // Override this to provide wxWindows message loop
@@ -243,10 +243,10 @@ int CTheApp::ExitInstance()
BOOL CTheApp::PreTranslateMessage(MSG *msg) BOOL CTheApp::PreTranslateMessage(MSG *msg)
{ {
if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg)) if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg))
return TRUE; return TRUE;
else else
return CWinApp::PreTranslateMessage(msg); return CWinApp::PreTranslateMessage(msg);
} }
BOOL CTheApp::OnIdle(LONG lCount) BOOL CTheApp::OnIdle(LONG lCount)
@@ -258,54 +258,54 @@ BOOL CTheApp::OnIdle(LONG lCount)
} }
/********************************************************************* /*********************************************************************
* wxWindows elements * wxWindows elements
********************************************************************/ ********************************************************************/
bool MyApp::OnInit(void) bool MyApp::OnInit(void)
{ {
// Don't exit app when the top level frame is deleted // Exit app when the top level frame is deleted
// SetExitOnFrameDelete(FALSE); SetExitOnFrameDelete(TRUE);
wxFrame* frame = CreateFrame(); (void) CreateFrame();
return TRUE; return TRUE;
} }
wxFrame *MyApp::CreateFrame(void) wxFrame *MyApp::CreateFrame(void)
{ {
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);
subframe->SetTitle("wxWindows canvas frame"); subframe->SetTitle("wxWindows canvas frame");
// Give it a status line // Give it a status line
subframe->CreateStatusBar(); subframe->CreateStatusBar();
// Make a menubar // Make a menubar
wxMenu *file_menu = new wxMenu; wxMenu *file_menu = new wxMenu;
file_menu->Append(HELLO_NEW, "&New MFC Window"); file_menu->Append(HELLO_NEW, "&New MFC Window");
file_menu->Append(HELLO_QUIT, "&Close"); file_menu->Append(HELLO_QUIT, "&Close");
wxMenuBar *menu_bar = new wxMenuBar; wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "&File"); menu_bar->Append(file_menu, "&File");
// Associate the menu bar with the frame // Associate the menu bar with the frame
subframe->SetMenuBar(menu_bar); subframe->SetMenuBar(menu_bar);
int width, height; int width, height;
subframe->GetClientSize(&width, &height); subframe->GetClientSize(&width, &height);
MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height)); MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
canvas->SetCursor(wxCursor(wxCURSOR_PENCIL)); canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
subframe->canvas = canvas; subframe->canvas = canvas;
// Give it scrollbars // Give it scrollbars
// canvas->SetScrollbars(20, 20, 50, 50, 4, 4); // canvas->SetScrollbars(20, 20, 50, 50, 4, 4);
subframe->Show(TRUE); subframe->Show(TRUE);
// Return the main frame window // Return the main frame window
return subframe; return subframe;
} }
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
@@ -315,7 +315,7 @@ 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)
{ {
} }
@@ -323,17 +323,17 @@ MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
void MyCanvas::OnPaint(wxPaintEvent& event) void MyCanvas::OnPaint(wxPaintEvent& event)
{ {
wxPaintDC dc(this); wxPaintDC dc(this);
dc.SetFont(* wxSWISS_FONT); dc.SetFont(* wxSWISS_FONT);
dc.SetPen(* wxGREEN_PEN); dc.SetPen(* wxGREEN_PEN);
dc.DrawLine(0, 0, 200, 200); dc.DrawLine(0, 0, 200, 200);
dc.DrawLine(200, 0, 0, 200); dc.DrawLine(200, 0, 0, 200);
dc.SetBrush(* wxCYAN_BRUSH); dc.SetBrush(* wxCYAN_BRUSH);
dc.SetPen(* wxRED_PEN); dc.SetPen(* wxRED_PEN);
dc.DrawRectangle(100, 100, 100, 50); dc.DrawRectangle(100, 100, 100, 50);
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.DrawSpline(50, 200, 50, 100, 200, 10);
dc.DrawLine(50, 230, 200, 230); dc.DrawLine(50, 230, 200, 230);
@@ -362,9 +362,9 @@ BEGIN_EVENT_TABLE(MyChild, wxFrame)
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)
@@ -382,23 +382,23 @@ void MyChild::OnNew(wxCommandEvent& event)
mainWin->ShowWindow( TRUE ); mainWin->ShowWindow( TRUE );
mainWin->UpdateWindow(); mainWin->UpdateWindow();
} }
void MyChild::OnActivate(wxActivateEvent& event) void MyChild::OnActivate(wxActivateEvent& event)
{ {
if (event.GetActive() && canvas) if (event.GetActive() && canvas)
canvas->SetFocus(); canvas->SetFocus();
} }
// Dummy MFC window for specifying a valid main window to MFC, using // Dummy MFC window for specifying a valid main window to MFC, using
// a wxWindows HWND. // a wxWindows HWND.
CDummyWindow::CDummyWindow(HWND hWnd):CWnd() CDummyWindow::CDummyWindow(HWND hWnd):CWnd()
{ {
Attach(hWnd); Attach(hWnd);
} }
// Don't let the CWnd destructor delete the HWND // Don't let the CWnd destructor delete the HWND
CDummyWindow::~CDummyWindow(void) CDummyWindow::~CDummyWindow(void)
{ {
Detach(); Detach();
} }

View File

@@ -33,6 +33,12 @@
#include "wx/msw/private.h" #include "wx/msw/private.h"
// Don't implement WinMain if we're building an MFC/wxWindows
// hybrid app.
#if wxUSE_MFC && !defined(NOMAIN)
#define NOMAIN 1
#endif
// from src/msw/app.cpp // from src/msw/app.cpp
extern void WXDLLEXPORT wxEntryCleanup(); extern void WXDLLEXPORT wxEntryCleanup();