2003-03-17 08:41:00 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: mfctest.cpp
|
|
|
|
// Purpose: Sample to demonstrate mixing MFC and wxWindows code
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Id: $Id$
|
|
|
|
// Copyright: (c) Julian Smart
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
1998-05-20 10:21:00 -04:00
|
|
|
|
|
|
|
// This sample pops up an initial wxWindows frame, with a menu item
|
|
|
|
// that allows a new MFC window to be created. Note that CDummyWindow
|
|
|
|
// is a class that allows a wxWindows window to be seen as a CWnd
|
|
|
|
// for the purposes of specifying a valid main window to the
|
|
|
|
// MFC initialisation.
|
|
|
|
//
|
|
|
|
// You can easily modify this code so that an MFC window pops up
|
|
|
|
// initially as the main frame, and allows wxWindows frames to be
|
2002-12-17 06:18:09 -05:00
|
|
|
// created subsequently.
|
1998-05-20 10:21:00 -04:00
|
|
|
//
|
2002-12-17 06:18:09 -05:00
|
|
|
// (1) Make MyApp::OnInit not create a main window.
|
|
|
|
// (2) Make MFC's InitInstance create a main window, and remove
|
1998-05-20 10:21:00 -04:00
|
|
|
// creation of CDummyWindow.
|
2002-12-17 06:18:09 -05:00
|
|
|
//
|
|
|
|
// This can be accomplished by setting START_WITH_MFC_WINDOW to 1 below.
|
|
|
|
|
|
|
|
#define START_WITH_MFC_WINDOW 0
|
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
//
|
2000-03-10 06:18:22 -05:00
|
|
|
// IMPORTANT NOTES:
|
|
|
|
//
|
|
|
|
// (1) You need to set wxUSE_MFC to 1 in include/wx/msw/setup.h, which switches
|
|
|
|
// off some debugging features and also removes the windows.h inclusion
|
|
|
|
// in wxprec.h (MFC headers don't like this to have been included previously).
|
2002-12-17 06:18:09 -05:00
|
|
|
// Set to 'Use MFC in a shared DLL' or add _AFXDLL to preprocessor settings.
|
2000-03-10 06:18:22 -05:00
|
|
|
// Then recompile wxWindows and this sample.
|
|
|
|
//
|
2002-12-17 06:18:09 -05:00
|
|
|
// (2) I can't get the sample to link and run using a static MFC library, only the DLL
|
2000-03-10 06:18:22 -05:00
|
|
|
// 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
|
|
|
|
// easier.
|
2000-07-15 15:51:35 -04:00
|
|
|
//
|
2002-12-17 06:18:09 -05:00
|
|
|
// (3) Compiling wxWindows in DLL mode currently includes windows.h, so you must only
|
2000-07-15 15:51:35 -04:00
|
|
|
// try linking wxWindows statically.
|
1998-05-20 10:21:00 -04:00
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "wx/wx.h"
|
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
#if defined(_WINDOWS_) || !wxUSE_MFC
|
|
|
|
#error Sorry, you need to edit include/wx/msw/setup.h, set wxUSE_MFC to 1, and recompile.
|
1999-02-05 10:06:25 -05:00
|
|
|
#endif
|
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
#ifdef new
|
|
|
|
#undef new
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#ifdef DrawText
|
|
|
|
#undef DrawText
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "resource.h"
|
|
|
|
|
|
|
|
#include "mfctest.h"
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// theApp:
|
|
|
|
// Just creating this application object runs the whole application.
|
|
|
|
//
|
|
|
|
CTheApp theApp;
|
|
|
|
|
|
|
|
// wxWindows elements
|
|
|
|
|
|
|
|
// Define a new application type
|
|
|
|
class MyApp: public wxApp
|
|
|
|
{ public:
|
2002-12-17 06:18:09 -05:00
|
|
|
bool OnInit(void);
|
|
|
|
wxFrame *CreateFrame(void);
|
|
|
|
};
|
1998-05-20 10:21:00 -04:00
|
|
|
|
|
|
|
class MyCanvas: public wxScrolledWindow
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
public:
|
1998-05-20 10:21:00 -04:00
|
|
|
MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size);
|
|
|
|
void OnPaint(wxPaintEvent& event);
|
|
|
|
void OnMouseEvent(wxMouseEvent& event);
|
2002-12-17 06:18:09 -05:00
|
|
|
DECLARE_EVENT_TABLE()
|
1998-05-20 10:21:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
class MyChild: public wxFrame
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
public:
|
1998-05-20 10:21:00 -04:00
|
|
|
MyCanvas *canvas;
|
|
|
|
MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style);
|
|
|
|
~MyChild(void);
|
2002-12-17 06:18:09 -05:00
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnNew(wxCommandEvent& event);
|
|
|
|
void OnActivate(wxActivateEvent& event);
|
2002-12-17 06:18:09 -05:00
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
1998-05-20 10:21:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// For drawing lines in a canvas
|
|
|
|
long xpos = -1;
|
|
|
|
long ypos = -1;
|
|
|
|
|
|
|
|
// ID for the menu quit command
|
|
|
|
#define HELLO_QUIT 1
|
|
|
|
#define HELLO_NEW 2
|
|
|
|
|
|
|
|
DECLARE_APP(MyApp)
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// CMainWindow constructor:
|
|
|
|
// Create the window with the appropriate style, size, menu, etc.
|
|
|
|
//
|
|
|
|
CMainWindow::CMainWindow()
|
|
|
|
{
|
2002-03-17 09:16:03 -05:00
|
|
|
LoadAccelTable( "MainAccelTable" );
|
|
|
|
Create( NULL, "Hello Foundation Application",
|
|
|
|
WS_OVERLAPPEDWINDOW, rectDefault, NULL, "MainMenu" );
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
2002-03-17 09:16:03 -05:00
|
|
|
CString s = "Hello, Windows!";
|
|
|
|
CPaintDC dc( this );
|
|
|
|
CRect rect;
|
2002-12-17 06:18:09 -05:00
|
|
|
|
2002-03-17 09:16:03 -05:00
|
|
|
GetClientRect( rect );
|
|
|
|
dc.SetTextAlign( TA_BASELINE | TA_CENTER );
|
|
|
|
dc.SetTextColor( ::GetSysColor( COLOR_WINDOWTEXT ) );
|
|
|
|
dc.SetBkMode(TRANSPARENT);
|
|
|
|
dc.TextOut( ( rect.right / 2 ), ( rect.bottom / 2 ),
|
2002-12-17 06:18:09 -05:00
|
|
|
s, s.GetLength() );
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
{
|
2002-03-17 09:16:03 -05:00
|
|
|
CDialog about( "AboutBox", this );
|
|
|
|
about.DoModal();
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMainWindow::OnTest()
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
wxMessageBox("This is a wxWindows message box.\nWe're about to create a new wxWindows frame.", "wxWindows", wxOK);
|
|
|
|
wxGetApp().CreateFrame();
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CMainWindow message map:
|
|
|
|
// Associate messages with member functions.
|
|
|
|
//
|
|
|
|
// It is implied that the ON_WM_PAINT macro expects a member function
|
|
|
|
// "void OnPaint()".
|
|
|
|
//
|
|
|
|
// It is implied that members connected with the ON_COMMAND macro
|
|
|
|
// receive no arguments and are void of return type, e.g., "void OnAbout()".
|
|
|
|
//
|
|
|
|
BEGIN_MESSAGE_MAP( CMainWindow, CFrameWnd )
|
2002-12-17 06:18:09 -05:00
|
|
|
//{{AFX_MSG_MAP( CMainWindow )
|
|
|
|
ON_WM_PAINT()
|
|
|
|
ON_COMMAND( IDM_ABOUT, OnAbout )
|
|
|
|
ON_COMMAND( IDM_TEST, OnTest )
|
|
|
|
//}}AFX_MSG_MAP
|
1998-05-20 10:21:00 -04:00
|
|
|
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()
|
|
|
|
{
|
2002-03-17 09:16:03 -05:00
|
|
|
SetDialogBkColor(); // hook gray dialogs (was default in MFC V1)
|
2002-12-17 06:18:09 -05:00
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
wxEntry((WXHINSTANCE) m_hInstance, (WXHINSTANCE) m_hPrevInstance, m_lpCmdLine, m_nCmdShow, FALSE);
|
2002-12-17 06:18:09 -05:00
|
|
|
|
|
|
|
#if START_WITH_MFC_WINDOW
|
|
|
|
// Demonstrate creation of an initial MFC main window.
|
2002-03-17 09:16:03 -05:00
|
|
|
m_pMainWnd = new CMainWindow();
|
|
|
|
m_pMainWnd->ShowWindow( m_nCmdShow );
|
|
|
|
m_pMainWnd->UpdateWindow();
|
2002-12-17 06:18:09 -05:00
|
|
|
#else
|
|
|
|
// Demonstrate creation of an initial wxWindows main window.
|
|
|
|
// Wrap wxWindows window in a dummy MFC window and
|
|
|
|
// make the main window.
|
1998-05-20 10:21:00 -04:00
|
|
|
if (wxTheApp && wxTheApp->GetTopWindow())
|
|
|
|
{
|
|
|
|
m_pMainWnd = new CDummyWindow((HWND) wxTheApp->GetTopWindow()->GetHWND());
|
|
|
|
}
|
2002-12-17 06:18:09 -05:00
|
|
|
#endif
|
|
|
|
|
2002-03-17 09:16:03 -05:00
|
|
|
return TRUE;
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int CTheApp::ExitInstance()
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
// OnExit isn't called by CleanUp so must be called explicitly.
|
|
|
|
wxTheApp->OnExit();
|
|
|
|
wxApp::CleanUp();
|
|
|
|
|
|
|
|
return CWinApp::ExitInstance();
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override this to provide wxWindows message loop
|
|
|
|
// compatibility
|
|
|
|
|
|
|
|
BOOL CTheApp::PreTranslateMessage(MSG *msg)
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
if (wxTheApp && wxTheApp->ProcessMessage((WXMSG*) msg))
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return CWinApp::PreTranslateMessage(msg);
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CTheApp::OnIdle(LONG lCount)
|
|
|
|
{
|
|
|
|
if (wxTheApp)
|
|
|
|
return wxTheApp->ProcessIdle();
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************
|
2002-12-17 06:18:09 -05:00
|
|
|
* wxWindows elements
|
|
|
|
********************************************************************/
|
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
bool MyApp::OnInit(void)
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
#if !START_WITH_MFC_WINDOW
|
|
|
|
|
|
|
|
// Exit app when the top level frame is deleted
|
|
|
|
SetExitOnFrameDelete(TRUE);
|
|
|
|
|
|
|
|
(void) CreateFrame();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
wxFrame *MyApp::CreateFrame(void)
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
MyChild *subframe = new MyChild(NULL, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
|
|
|
|
wxDEFAULT_FRAME_STYLE);
|
|
|
|
|
|
|
|
subframe->SetTitle("wxWindows canvas frame");
|
|
|
|
|
|
|
|
// Give it a status line
|
|
|
|
subframe->CreateStatusBar();
|
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
|
|
|
file_menu->Append(HELLO_NEW, "&New MFC Window");
|
|
|
|
file_menu->Append(HELLO_QUIT, "&Close");
|
|
|
|
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
|
|
|
|
menu_bar->Append(file_menu, "&File");
|
|
|
|
|
|
|
|
// Associate the menu bar with the frame
|
|
|
|
subframe->SetMenuBar(menu_bar);
|
|
|
|
|
|
|
|
int width, height;
|
|
|
|
subframe->GetClientSize(&width, &height);
|
|
|
|
|
|
|
|
MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
|
|
|
|
canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
|
|
|
|
subframe->canvas = canvas;
|
|
|
|
subframe->Show(TRUE);
|
|
|
|
|
|
|
|
// Return the main frame window
|
|
|
|
return subframe;
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
|
|
|
|
EVT_PAINT(MyCanvas::OnPaint)
|
|
|
|
EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// Define a constructor for my canvas
|
|
|
|
MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
|
2002-12-17 06:18:09 -05:00
|
|
|
wxScrolledWindow(parent, -1, pos, size)
|
1998-05-20 10:21:00 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define the repainting behaviour
|
|
|
|
void MyCanvas::OnPaint(wxPaintEvent& event)
|
|
|
|
{
|
|
|
|
wxPaintDC dc(this);
|
2002-12-17 06:18:09 -05:00
|
|
|
|
1999-02-04 17:06:15 -05:00
|
|
|
dc.SetFont(* wxSWISS_FONT);
|
1999-02-04 16:12:30 -05:00
|
|
|
dc.SetPen(* wxGREEN_PEN);
|
1998-05-20 10:21:00 -04:00
|
|
|
dc.DrawLine(0, 0, 200, 200);
|
|
|
|
dc.DrawLine(200, 0, 0, 200);
|
2002-12-17 06:18:09 -05:00
|
|
|
|
1999-02-04 16:12:30 -05:00
|
|
|
dc.SetBrush(* wxCYAN_BRUSH);
|
|
|
|
dc.SetPen(* wxRED_PEN);
|
1998-05-20 10:21:00 -04:00
|
|
|
dc.DrawRectangle(100, 100, 100, 50);
|
|
|
|
dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
|
2002-12-17 06:18:09 -05:00
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
dc.DrawEllipse(250, 250, 100, 50);
|
|
|
|
dc.DrawSpline(50, 200, 50, 100, 200, 10);
|
|
|
|
dc.DrawLine(50, 230, 200, 230);
|
|
|
|
dc.DrawText("This is a test string", 50, 230);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This implements a tiny doodling program! Drag the mouse using
|
|
|
|
// the left button.
|
|
|
|
void MyCanvas::OnMouseEvent(wxMouseEvent& event)
|
|
|
|
{
|
|
|
|
wxClientDC dc(this);
|
1999-02-04 16:12:30 -05:00
|
|
|
dc.SetPen(* wxBLACK_PEN);
|
2000-03-10 06:18:22 -05:00
|
|
|
wxPoint pos = event.GetPosition();
|
1998-05-20 10:21:00 -04:00
|
|
|
if (xpos > -1 && ypos > -1 && event.Dragging())
|
|
|
|
{
|
2000-03-10 06:18:22 -05:00
|
|
|
dc.DrawLine(xpos, ypos, pos.x, pos.y);
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
2000-03-10 06:18:22 -05:00
|
|
|
xpos = pos.x;
|
|
|
|
ypos = pos.y;
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE(MyChild, wxFrame)
|
|
|
|
EVT_MENU(HELLO_QUIT, MyChild::OnQuit)
|
|
|
|
EVT_MENU(HELLO_NEW, MyChild::OnNew)
|
|
|
|
EVT_ACTIVATE(MyChild::OnActivate)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
MyChild::MyChild(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size, const long style):
|
2002-12-17 06:18:09 -05:00
|
|
|
wxFrame(frame, -1, title, pos, size, style)
|
1998-05-20 10:21:00 -04:00
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
canvas = NULL;
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
MyChild::~MyChild(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyChild::OnQuit(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
Close(TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyChild::OnNew(wxCommandEvent& event)
|
|
|
|
{
|
|
|
|
CMainWindow *mainWin = new CMainWindow();
|
|
|
|
mainWin->ShowWindow( TRUE );
|
|
|
|
mainWin->UpdateWindow();
|
|
|
|
}
|
2002-12-17 06:18:09 -05:00
|
|
|
|
1998-05-20 10:21:00 -04:00
|
|
|
void MyChild::OnActivate(wxActivateEvent& event)
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
if (event.GetActive() && canvas)
|
|
|
|
canvas->SetFocus();
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dummy MFC window for specifying a valid main window to MFC, using
|
|
|
|
// a wxWindows HWND.
|
|
|
|
CDummyWindow::CDummyWindow(HWND hWnd):CWnd()
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
Attach(hWnd);
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't let the CWnd destructor delete the HWND
|
|
|
|
CDummyWindow::~CDummyWindow(void)
|
|
|
|
{
|
2002-12-17 06:18:09 -05:00
|
|
|
Detach();
|
1998-05-20 10:21:00 -04:00
|
|
|
}
|
|
|
|
|