WxWidgets for MFC programmers

Source: Internet
Author: User
Tags ibm developerworks wxwidgets

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 ". 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]

  • 1 Dialog Data Handling
  • 2 Associating hwnds with wxwindow instances
  • 3 Oninitdialog
  • 4 WxWidgets and MFC functions with the same name but different functionality
  • 5 WxWidgets and mfc dc differences
  • 6 So you are about to convert your MFC Application
    • 6.1 Dialog pitfalls
    • 6.2 Trace pitfall
    • 6.3 Format pitfall
    • 6.4 Changing icon formats
  • 7 Class translation table
  • 8 Other documentation on the issue
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 particle 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 shocould 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 previusly, wxstring: format is a static wxstring member that returns a modified wxstring, where cstring: format actually affects the cstring instance that callit.
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) between des a command line utility with a niceConvertFunction 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)

MFC WxWidgets Header Comments
Canimatectrl Wxmediactrl Mediactrl. h  
Carchive     No serialization in wxWidgets.
Carchiveexception     No exceptions in wxWidgets.
Carray Wxarray Dynarray. h Requires macros and arrimpl. cpp.
Casyncmonikerfile ?? Wxfile ??    
Casyncsocket ?? Wxsocketbase ??    
Cbitmap Wxbitmap Bitmap. h  
Cbitmapbutton Wxbitmapbutton BMP buttn. h  
Cbrush Wxbrush Brush. h  
Cbutton Wxbutton Button. h  
Ccacheddatapathproperty     No support in wxWidgets (?).
Cchecklistbox Wxchecklistbox Checklst. h  
Cclientdc Wxclientdc DC. h  
Csf-target Wxevthandler Event. h Wxevthandler is probably the most fitting

Counterpart.

Ccmdui ???    
Ccombobox Wxcombobox    
Ccomboboxex     Wxcombobox does not support Images
Ccommandlineinfo Wxw.lineparser    
Ccommondialog Wxdialog    
Ccriticalsection Wxcriticalsection Thread. h  
Cdialog Wxdialog Dialog. h  
Cdatabase Wxdb DB. h  
CDC Wxdc DC. h  
Cedit Wxtextctrl Textctrl. h  
Ceditview Wxtextctrl Textctrl. h Use wxtextctrl inside wxview, see IBM developerworks "porting MFC applications to Linux"
Cevent Wxcondition Event. h  
Cfile Wxfile File. h Wrapper around UNIX functions.
Cfilestatus Wxfsfile Filesys. h Part of wxfilesystem
Cframewnd Wxframe Frame. h  
Cimagelist Wximagelist    
Clist Wxlist   Macro based.
Clistbox Wxlistbox   Macro based.
Cmap Wxhashmap   Wxhashmap is unsorted-for sorted values use wxmap instead [1]
Cmapptrtoptr Wxhashmap   Wxhashmap is unsorted-for sorted values use wxmap instead [1]
Cmapptrtoword Wxhashmap   Wxhashmap is unsorted-for sorted values use wxmap instead [1]
Cmdichildwnd Wxdow.ichildframe Docmdi. h  
Cmdiframewnd Wxdow.iparentframe Docmdi. h  
Cmenu Wxmenu Menu. h  
Cmenuiteminfo Wxmenuitem Menuitem. h  
Cmultidoctemplate Wxdoctemplate Docview. h  
Cmutex Wxmutex Thread. h  
Cobject Wxobject Object. h  
Coledropsource Wxdropsource    
Cpen Wxpen    
Cpropertysheet Wxnotebook    
Crecentfilelist Wxfilehistory    
Crect Wxrect Gdicmn. h  
Csemaphore Wxsemaphore Thread. h  
Cspinbuttonctrl Wxspinbutton   Portable programs: Use wxspinctrl instead
Cstatic Wxstatictext    
Ctime Wxdatetime    
Ctreectrl Wxtreectrl Treectrl. 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.
Cuintarray Wxarrayuint Dynarray. h  
Cview Wxview Docview. h  
Cwaitcursor Wxbusycursor Cursor. h  
Cwindowdc Wxwindowdc Dcclient. h  
Cwinthread Wxthread Thread. h Main thread only for GUI routines in wxWidgets
Cwnd Wxwindow Window. h  
Cwordarray Wxarray Dynarray. h Wxarrayshort on 32-bit platforms, etc.

[1]-wxmap is an unofficial user contribution-see this Forum Thread for the source.

 

Other documentation on the issue
    • IBM developerworks published: [1]
    • The Code project: Introduction to wxWidgets -- see "MFC and wxWidgets classes" paragraph

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.