WxWidgets For MFC Programmers

http://wiki.wxwidgets.org/WxWidgets_For_MFC_Programmers#Dialog_Pitfalls

WxWidgets For MFC Programmers

First, look at Compiling A WxWidgets Application to see what you need for your new project settings.

If you plan to use both MFC and wxWidgets (and are using stdafx.h), make sure to have "include <wx/wx.h>" above the MFC include lines. Otherwise, you may get errors about a "constant" in the CreateDialog function.

Unlike MFC's CString::Format function, the wxString::Format function is a static member function. This means that calling "myStringInstance.Format(...)" will return the formatted text in a new wxString rather than change the string in the myStringInstance variable. You might be able to get away with a search and replace of ".Format(" to " = wxString::Format(" or ".Printf(" when porting your application.

There is a guide for porting an MFC application to wxWidgets. See Porting MFC applications to Linux.

Contents

[hide]

Dialog Data Handling

First, forget about MFC's DDX/DDV macros. wxWidgets takes a more object-oriented approach with wxValidators. The help is very good on the topic of wxValidators. Just remember that wxTextValidator works for wxTextCtrls and wxGenericValidator works for just about everything else.

Second, no more having to remember if you want to send TRUE or FALSE to the UpdateData Function. The function names TransferDataToWindow and TransferDataFromWindow are clear enough.

If you have a problem with errors when transferring data between the variables and the window, try changing the data type of the variable. BOOL and bool aren't the same and might cause you issues.

Finally, a pitfall that might come up with radio buttons. wxGenericValidator will connect an integer variable with a wxRadioBox, but NOT with a wxRadioButton. You can only connect a bool variable with a wxRadioButton. If your radio buttons are spaced out with controls in between them (possibly other controls that go with a particular radio button), you'll need to use a wxRadioButton rather than a wxRadioBox. Unfortunately, this may cause large code changes if you had an integer associated with these.

Associating HWNDs with wxWindow Instances

From a recent discussion on the comp.soft-sys.wxwindows newsgroup (searchable via http://groups.google.com), here is an easy way to associate a Win32 HWND (hWnd) with a wxWindow instance:

  wxWindow * win = new wxWindow();
  win->SetHWND((WXHWND)hWnd);
  win->AdoptAttributesFromHWND();
  win->Reparent(wxGetApp().GetTopWindow());

OnInitDialog

A common thing to do in MFC is add some dialog initialization code in the OnInitDialog message handling function. wxWidgets allows this as well with the EVT_INIT_DIALOG message and the wxInitDialogEvent event class. The documentation mentions this under the topic wxInitDialogEvent, but none of the samples demonstrate using this functionality. Your OnInitDialog function should fit the following declaration:

  void MyDialog::OnInitDialogFunction(wxInitDialogEvent& event)

The key point is the wxInitDialogEvent parameter. If you use no parameters, the code will work fine in Debug mode but crash in Release mode (this experience was using VC 6.0 SP4).

wxWidgets and MFC Functions with the Same Name but Different Functionality

  • wxListBox::FindString is equivalent to CListBox::FindStringExact rather than CListBox::FindString. CListBox::FindString looks for list box entries "prefixed" with the search string rather than exact matches. I don't believe that wxWidgets has a function equivalent to CListBox::FindString.
  • As mentioned previously, wxString::Format is a static wxString member that returns a modified wxString, where CString::Format actually affects the CString instance that calls it.

wxWidgets and MFC DC Differences

A common error in MFC code is not restoring the original CDC brush, pen, or font after changing them with the CDC::SelectObject function. In wxWidgets, this problem doesn't exist since the restoration of the original brush/pen/font is taken care of by wxWidgets itself.

So You are About to Convert Your MFC Application

Change all your

  • ASSERT() to wxASSERT()
  • TRACE to wxLogDebug()
  • CString to wxString
  • MyString.GetLength() to MyString.Length()
  • MyString.Format("Some number %i", x) to MyString.Printf( "Some number %i",x)

See also Helpers For Automated Rescue From MFC.

Dialog Pitfalls

If you are taking the opportunity to change your MFC dialogs to use sizers, have a look at the advice on the wxSizer page first.

TRACE Pitfall

If instead of using wxLogDebug you change your TRACE to wxLogTrace() some will work, some will fail to show. Why? Because wxLogTrace is polymorphic and if you do something like wxLogTrace("Filename is: %s", pFilename), the first two arguments are char* so the compiler happily matches this up with the wxLogTrace variant with prototype wxLogTrace(const wxChar *mask, const wxChar *szFormat, ...). Oops!

Format Pitfall

Your use of Format will compile happily and do sweet FA if you don't make the change shown above. In wxWidgets wxString::Format does not change the string in place, instead it is a static function that returns the formatted string.

Changing Icon Formats

You may want to switch over to using xpm (X PixMap) format for all your smaller bitmaps and icons. ImageMagick (GPL) includes a command line utility with a nice convert function that handles xpm and it is available in both a Windows and a Linux version.

Class Translation Table

Here's a list of most MFC classes and their wxWidgets counterparts. (wxWidgets headers need to be prefixed with wx/):

(TODO: Put all classes from here)

MFCwxWidgetsHeaderComments
CAnimateCtrlwxMediaCtrlmediactrl.h 
CArchive  No Serialization in wxWidgets.
CArchiveException  No Exceptions in wxWidgets.
CArraywxArraydynarray.hRequires macros and arrimpl.cpp.
CAsyncMonikerFile ??wxFile??  
CAsyncSocket ??wxSocketBase??  
CBitmapwxBitmapbitmap.h 
CBitmapButtonwxBitmapButtonbmpbuttn.h 
CBrushwxBrushbrush.h 
CButtonwxButtonbutton.h 
CCachedDataPathProperty  No support in wxWidgets(?).
CCheckListBoxwxCheckListBoxchecklst.h 
CClientDCwxClientDCdc.h 
CCmdTargetwxEvtHandlerevent.hwxEvtHandler is probably the most fitting

counterpart.

CCmdUI ???  
CComboBoxwxComboBox  
CComboBoxEx  wxComboBox does not support images
CCommandLineInfowxCmdLineParser  
CCommonDialogwxDialog  
CCriticalSectionwxCriticalSectionthread.h 
CDialogwxDialogdialog.h 
CDatabasewxDbdb.h 
CDCwxDCdc.h 
CEditwxTextCtrltextctrl.h 
CEditViewwxTextCtrltextctrl.hUse wxTextCtrl inside wxView, see IBM developerWorks "Porting MFC applications to Linux"
CEventwxConditionevent.h 
CFilewxFilefile.hWrapper around unix functions.
CFileStatuswxFSFilefilesys.hPart of wxFileSystem
CFrameWndwxFrameframe.h 
CImageListwxImageList  
CListwxList Macro based.
CListBoxwxListBox Macro based.
CMapwxHashMap wxHashMap is unsorted - for sorted values use wxMap instead[1]
CMapPtrToPtrwxHashMap wxHashMap is unsorted - for sorted values use wxMap instead[1]
CMapPtrToWordwxHashMap wxHashMap is unsorted - for sorted values use wxMap instead[1]
CMDIChildWndwxDocMDIChildFramedocmdi.h 
CMDIFrameWndwxDocMDIParentFramedocmdi.h 
CMenuwxMenumenu.h 
CMenuItemInfowxMenuItemmenuitem.h 
CMultiDocTemplatewxDocTemplatedocview.h 
CMutexwxMutexthread.h 
CObjectwxObjectobject.h 
COleDropSourcewxDropSource  
CPenwxPen  
CPropertySheetwxNotebook  
CRecentFileListwxFileHistory  
CRectwxRectgdicmn.h 
CSemaphorewxSemaphorethread.h 
CSpinButtonCtrlwxSpinButton Portable programs: use wxSpinCtrl instead
CStaticwxStaticText  
CTimewxDateTime  
CTreeCtrlwxTreeCtrltreectrl.h 
CTreeView  No support in wxWidgets(?).
CTypedPtrArray  No templates in wxWidgets, needs C++ compiler

support.

CTypedPtrList  No templates in wxWidgets, needs C++ compiler

support.

CTypedPtrMap  No templates in wxWidgets, needs C++ compiler

support.

CUserException  No exceptions in wxWidgets.
CUIntArraywxArrayUintdynarray.h 
CViewwxViewdocview.h 
CWaitCursorwxBusyCursorcursor.h 
CWindowDCwxWindowDCdcclient.h 
CWinThreadwxThreadthread.hMain thread only for GUI routines in wxWidgets
CWndwxWindowwindow.h 
CWordArraywxArraydynarray.hwxArrayShort on 32-bit platforms, etc.

[1] - wxMap is an unofficial user contribution - see this forum thread for the source.

 

Other Documentation on the Issue

posted on 2012-02-14 12:07 clq 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/-clq/archive/2012/02/14/2350830.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值