Creating a basic wxWidgets GUI Unicode application with Visual C++ Express

Creating a basic wxWidgets GUI Unicode application with Visual C++ Express
Development Environment:
  • Windows 7, 8.1, 10, XP
  • Microsoft Visual C++ 2010 Express, 2013, 2015 Express / Community Edition
  • wxWidgets v3.0.2 / v3.0.3 / v3.0.4
Step 1: Create Project :
  1. Unzip wxWidgets archive to a folder and compile it following its install instructions. It takes time and it would take some time too to configure it properly for compile both for debug and release.
  2. File >> New Project >> Visual C++ >> Win32 >> Win32 Project.
  3. Input solution title and press [OK] button. No need to check "Create directory for solution."wx_0001_1
  4. [Next]
    wx_0001_2
  5. Check [Empty Project] >> [Finish]
    wx_0001_3
  6. Result: A new wxWidgets GUI Project has been created.
    Now create/add necessary source/header files and configure the project:
    wx_0001_4
  7. File >> New >> File... >> Visual C++ >>
    • C++ File(.cpp) >> [Open]
      wx_0001_5

      File >> Save All
      File >> Save File As... >> Browse for the folder of the project then save the newly created 'Source1.cpp' with a proper name into it. That may well be the main source file of the project:
      wx_0001_6

    • or
      Right-click on [Source] item of VC++'s [Solution Explorer] >> Add >> New Item...
      wx_0001_7
    • Input proper name of the source file then click [Add] button:
      wx_0001_7-2-1
      Then, save all.
      Result:
      wx_0001_7-2-2

  8. Now create a main header file named after the source file with an '.h' extension the same way as done above.
Step 2: Configure Project :
  1. Project >> (Project) Properties...(Alt+F7) :
    wx_0001_8
  2. Configuration >> All Configuration :
    wx_0001_9
  3. Configuration Properties >> General >> Project Defaults >> Character Set >> Use Unicode Character Set :
    wx_0001_10
  4. Configuration Properties >> C++ >> General >> Additional Include Directories >> Click its drop-down box's <Edit...> item :
    wx_0001_11
    Then add the following lines :
    $(WX3)\include <- The location of the wxWidgets include directory.
    $(WX3)\include\msvc <- This automatically includes the build-specific setup.h files.
    wx_0001_12
    [OK]
  5. Configuration Properties >> C++ >> Preprocessor >> Preprocessor Definitions >> <Edit...>.
    Then add the following definitions: WIN32 <- Already there.
    WINVER=0x0500 (or WINVER=0x0400 for old version of Windows)
    _WINDOWS
    __WXMSW__
    wxUSE_GUI=1 <- If using GUI components(non-console app)
    WXUSINGDLL <- If compiling using DLLs instead of a static method.
    _CRT_SECURE_NO_DEPRECATE<- Optional, to suppress deprecation notification warnings.
    _CRT_NONSTDC_NO_DEPRECATE<- Optional, to suppress security warnings.
    _CRT_NON_CONFORMING_SWPRINTFS<- Optional, for swprintf.
    wx_0001_13

    [Apply]

    Now, switch to <Debug> configuration to add the following definition :
    __WXDEBUG__ <- only for debug configuration
    wx_0001_14

    [APPLY]

    Now, if the wxWidgets library was compiled with parameter switches as follows:
    DEBUG_INFO=0 DEBUG_FLAG=0 DEBUG_RUNTIME_LIBS=0
    switch to configuration to add the following definition :
    (otherwise, nothing to do with release definitions)
    wxDEBUG_LEVEL=0
    wx_0001_14_1
    [APPLY]

  6. For static build (no wxWidgets DLL will be distributed accompanied) :
    • Configuration >> <Debug>:
      Configuration Properties >> C++ >> Code Generation >> Runtime Library >> Multi-threaded Debug (/MTd)
    • Configuration >> <Release>:
      Configuration Properties >> C++ >> Code Generation >> Runtime Library >> Multi-threaded (/MT)

    [APPLY]

    Excerpt from wxWidgets:

    The multi-threaded DLL libraries are the ones wxWidgets is linked with by default. If you get something like MSVCRT.lib(MSVCRT.dll) : error LNK2005: _free already defined in LIBC.lib(free.obj) then you're linking with the wrong run-time library.

  7. Configuration >> : <All Configuration>:
    Configuration Properties >> Linker >> General >> Additional Library Directories >> <Edit...>.
    $(WX3)\lib\vc_lib
    or
    $(WX3)\lib\vc_dll for projects using DLLs : (WXUSINGDLL)
    [APPLY]
  8. Configuration Properties >> Linker >> Input >> Additional Dependencies >> <Edit...>.
    comctl32.lib
    rpcrt4.lib
    winmm.lib
    wsock32.lib
    advapi32.lib
    (VC defaults: already there)<- wxWidgets needs these.

    wxmsw30[ud]_core.lib
    wxbase30[ud].lib
    <- The core wxWidgets libraries.

    wxpng[d].lib
    wxzlib[d].lib
    wxjpeg[d].lib
    wxtiff[d].lib
    <- wxWidgets support libraries (optional, modify file names according to need). For example, for debug ANSI static :
    wxmsw30d_core.lib
    wxbase30d.lib

    For example, for debug Unicode static :
    wxmsw30ud_core.lib
    wxbase30ud.lib

    For example, for release Unicode static :
    wxmsw30u_core.lib
    wxbase30u.lib

    Configuration >> <Debug Configuration>
    wx_0001_15_debug
    ...
    [APPLY]

    Configuration >> <Release Configuration>
    wx_0001_15_release
    ...
    [APPLY]

  9. Configuration >> <All Configuration> :
    Configuration Properties >> Linker >> System >> SubSystem >> Windows (/SUBSYSTEM:WINDOWS)
  10. Configuration >> <All Configuration> :
    Configuration Properties >> Linker >> Manifest File >> Generate Manifest >> No (/MANIFEST:NO)
  11. Finally click [OK] to close the project configuration dialog box.
    What follows here are to walk around a bug that a VC 2013 project gets wrong version information of Windows v8.1 or later (...9200 not ..9600):
  12. Right click on the project name inside Solution Explorer >> Add >> Resource... >> Version.
  13. Again in the Solution Explorer, right click on the created resource file ´....rc´ then select View Code menu item to open the rc file in the IDE for edit.
  14. Write down the following line at the top followed by #include statement of the rc file:1 /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */ "ProjectName.exe.manifest" where the ´ProjectName.exe´ is the name of the executable file being created by the project on its build:
    wx_menifest
  15. Now create its manifest file with the above name of the following content under the project´s directory:
    
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
      <security>
        <requestedPrivileges>
        <requestedExecutionLevel
          level="asInvoker"
          uiAccess="False"/>
        </requestedPrivileges>
      </security>
      </trustInfo>
      <assemblyIdentity
        version="1.0.0.0"
        processorArchitecture="X86"
        name="Microsoft.Windows.AppName"
        type="win32"
      />
      <description>AppName</description>
      <!--dependency>
        <dependentAssembly>
        <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="X86"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
        </dependentAssembly>
      </dependency-->
    
      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
          <!-- Windows Vista/Server 2008 functionality -->
          <!-- supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/-->
    
          <!-- Windows 7/Server 2008 R2 functionality-->
          <!-- supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/-->
    
          <!-- Windows 8/Server 2012 functionality-->
          <!-- supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/-->
    
          <!-- Windows 8.1 and Windows Server 2012 R2 -->
          <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
    
          <!-- Windows Blue/Server 2012 R2 functionality-->
          <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
    
          <!-- Windows 10 -->
          <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
        </application>
      </compatibility>
    
    </assembly>
    
    
    
    - Available requestedExecutionLevel´s level values:
    • asInvoker
    • highestAvailable
    • requireAdministrator
Step 3: Write Code for a Skeleton Window Application :
  1. Main Header File :
    wx_0001_003_1-1
  2. Main Source File :
    wx_0001_003_2
  3. Now build the project and try running it :
    Voila!
    The following window appears on the screen which contains no menu, no toolbar , no status bar or other controls at all. Just the skeleton of a window. That's it. Succeeded in creating the first bare basic window, which means a difficult and complicated hurdle has been removed now:
    wx_0001_003_3

Step 4: Add a Main Menu and a Status Bar :

  1. Declare menu item IDs in the header file :
    enum {ID_FME_MAIN = 4096, ID_HELLO};
  2. Declare event handler methods for each menu item and declare an event table in the header :
    private:
    void OnAboutClick(wxCommandEvent &event);
    void OnHelloClick(wxCommandEvent &event);
    void OnExitClick(wxCommandEvent &event);
    wxDECLARE_EVENT_TABLE();
  3. In the source file :
    wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
        EVT_MENU(ID_HELLO   , MainFrame::OnHelloClick)
        EVT_MENU(wxID_EXIT  , MainFrame::OnExitClick)
        EVT_MENU(wxID_ABOUT , MainFrame::OnAboutClick)
    wxEND_EVENT_TABLE()
    
  4. Also in the source file :
      void MainFrame::OnExitClick(wxCommandEvent &event)
      {
        Close(true);
      }
      //----------------------------------------------------------------------------
      void MainFrame::OnAboutClick(wxCommandEvent &event)
      {
        wxMessageBox(L"About clicked!!", L"About PanCake" , wxOK|wxICON_INFORMATION);
      }
      //----------------------------------------------------------------------------
      void MainFrame::OnHelloClick(wxCommandEvent &event)
      {
        wxLogMessage("Hello world from wxWidgets!");
      }
      //----------------------------------------------------------------------------
    
  5. Also in the source file's main frame constructor :Excerpt from wxWidgets Help
    {
        wxMenu *menuFile = new wxMenu;
        menuFile->Append(ID_HELLO, L"&Hello...\tCtrl-H",
          L"Help string shown in status bar for this menu item");
        menuFile->AppendSeparator();
        menuFile->Append(wxID_EXIT);
    
        wxMenu *menuHelp = new wxMenu;
        menuHelp->Append(wxID_ABOUT);
    
        wxMenuBar *menuBar = new wxMenuBar;
        menuBar->Append(menuFile, L"&File");
        menuBar->Append(menuHelp, L"&Help");
    
        SetMenuBar(menuBar);
        CreateStatusBar();
        SetStatusText(L"Welcome to wxWidgets!");
      }
  6. Now again run the project and the Result is as follows :
    wx_0001_004_1 wx_0001_004_2
Done creating a basic wxWidgets GUI Application with Visual C++!
Download Project Source Code (src_sample_wx_vc2015_Setana.rar, 854 KB) for Visual Studio 2015.

No comments:

Post a Comment