C++. Wxwidgets get available drive letters

Hello there, I am back again. Now I will write a tutorial how to get available drive letters in windows using wxwidgets. The screenshot of the application to be made as follows:


To create this applications, firs in your project create a file with name frame.h. Frame.h will be used as a prototype of the mainframe applications.

#ifndef FRAME_H
#define FRAME_H

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>

#ifndef WX_PRECOMP
	#include <wx/frame.h>
	#include <wx/sizer.h>
	#include <wx/combobox.h>
	#include <wx/button.h>
#endif

#include <string.h>
#include <iostream>
#include <wx/volume.h>

class MainFrame : public wxFrame
{
public:
	MainFrame(const wxString& title);

	// Destructor
	virtual ~MainFrame();

private:
	wxComboBox* driveBox;
	wxStaticText* driveLabel;
	wxButton* buttonOk;
	wxButton* buttonQuit;

private:
	void OnQuit(wxCommandEvent& event);

private:
	wxDECLARE_EVENT_TABLE();
};

#endif // FRAME_H

Explanation:
From above code, see the highlighted code. That code will be used to get available drive letters (we use wxwidgets builtin function). More info see here http://docs.wxwidgets.org/trunk/classwx_f_s_volume.html#details
Now after we create header of frame, we must create the source file.

#include "Frame.h"

using namespace std;

MainFrame::MainFrame(const wxString& title)
: wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(300, 150),
wxSYSTEM_MENU | wxMINIMIZE_BOX | wxCLOSE_BOX | wxCAPTION | wxCLIP_CHILDREN)
{
	wxPanel* panel = new wxPanel(this, -1);
	wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer* hbox1 = new wxBoxSizer(wxHORIZONTAL);

	driveLabel = new wxStaticText(panel, wxID_ANY, wxT("Drive Letters: "),
		wxDefaultPosition, wxSize(80, -1));

	// Get All Drive Letter
	wxArrayString volumes = wxFSVolume::GetVolumes();

	driveBox = new wxComboBox(panel, wxID_ANY, volumes[0], wxDefaultPosition,
		wxDefaultSize, volumes, wxCB_READONLY);
	hbox1->Add(driveLabel, 0);
	hbox1->Add(driveBox, 1);
	vbox->Add(hbox1, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);

	wxBoxSizer* hbox2 = new wxBoxSizer(wxHORIZONTAL);
	buttonOk = new wxButton(panel, wxID_ANY, wxT("Ok"));
	buttonQuit = new wxButton(panel, wxID_EXIT, wxT("Quit"));
	hbox2->Add(buttonOk, 0);
	hbox2->Add(buttonQuit, 1);
	vbox->Add(hbox2, 0, wxALIGN_RIGHT | wxTOP | wxRIGHT, 10);

	panel->SetSizer(vbox);

	Centre();
}

wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
	EVT_BUTTON(wxID_EXIT, MainFrame::OnQuit)
wxEND_EVENT_TABLE()

void MainFrame::OnQuit(wxCommandEvent& event)
{
	Close(true);
}

MainFrame::~MainFrame() {}

As I have explained previously. Builtin functions that use of wxwidgets there on line 17, that is wxFSVolume::GetVolumes().
Then we must create main function to call main frame code using wxApp.

#ifndef MAIN_H
#define MAIN_H

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
	#ifndef WX_PRECOMP
	#include <wx/app.h>
#endif

class Main : public wxApp
{
public:
	virtual bool OnInit();
};

#endif // MAIN_H
#include "Main.h"
#include "Frame.h"

bool Main::OnInit()
{
	MainFrame *mainFrame = new MainFrame(wxT("Druve Letters"));
	mainFrame->Show(true);

	return true;
}

wxIMPLEMENT_APP(Main);

Now build all source code and you can get result like the picture of the initial post.

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s