WinAPI Dialogs class without MFC

killdude69 0 Tallied Votes 203 Views Share

This is the Dialogs.h part from the WGCL C++ Compiler Library I created. This creates classes for dialogs, it could cut the amount of code for dialogs you need to write in half, while still having the same amount of functionality.

This library is licensed under the GNU General Public License, it is completely free, and you can customize the source code to fit your needs. But I request you leave the lisence and credit comments at the top. Also, this is not commented very well because it was not originally created to teach people, people with moderate to advanced programming skills will be able to understand the code very will without the aid of comments.

/* This partial library from WGCL only works with Dev-Cpp, may not work with any other compiler */

/*
 * This source code/file is part of the WGCL C++ Compiler Library.
 * 
 * WGCL: Windows GUI Compiler Library
 * Version: 1.2
 * Original Mod date: 10/11/2008
 * Partial Product Release Date: 5/31/2009
 * Author: Michael Brandon Miller
 * Original FileName: Dialogs.h
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* * * * * BEGIN COLORDIALOG CLASS * * * * */

typedef class ColorDialog
{
    public:
      HWND _Parent;
	  bool AllowAnyColor;
	  void ShowDialog();
	  COLORREF Color(void);
	private:
	  COLORREF DefaultColor;
	  COLORREF Result;
	  bool Success;
	  bool Opened;
};

void ColorDialog::ShowDialog()
{
    CHOOSECOLOR color;
    Result = DefaultColor;
    
	ZeroMemory(&color, sizeof(color));
    
	color.hwndOwner = _Parent;
	color.lStructSize = sizeof(color);
	
	if (AllowAnyColor)
        color.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR;
	else
	    color.Flags = CC_RGBINIT | CC_FULLOPEN;
	
    color.rgbResult = Result;
	Success = ChooseColor(&color);
	Opened = true;
}

COLORREF ColorDialog::Color(void)
{
    if(Opened)
	{
        if(Success)
            return Result;
	    else
	        return NULL;
	}
}

/* * * * * END COLORDIALOG CLASS * * * * */

/* * * * * BEGIN OPENFILEDIALOG CLASS * * * * */

typedef class OpenFileDialog
{
    public:
	  HWND _Parent;
	  LPCTSTR DefaultExt;
	  LPSTR InitialDir;
	  bool AllowMultiSelect;
	  WORD SelFilterIndex;
	  LPSTR Filter;
	  LPSTR Title;
	  void ShowDialog();
	  char* File(void);
	private:
	  char FileName[MAX_PATH];
	  DWORD flags;
	  bool Success;
	  bool Opened;
};

void OpenFileDialog::ShowDialog()
{
    OPENFILENAME ofn;
	
	ZeroMemory(&ofn, sizeof(ofn));
	FileName[0] = 0;
	
	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = _Parent;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFilter = Filter;
	ofn.lpstrFile = FileName;
	ofn.lpstrTitle = Title;
	ofn.nFilterIndex = SelFilterIndex;
	ofn.lpstrDefExt = DefaultExt;
	
	if(AllowMultiSelect == true)
	  flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
	else
	  flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
	
	ofn.Flags = flags;
	
	Success = GetOpenFileName(&ofn);
	Opened = true;
}

char* OpenFileDialog::File(void)
{
    if(Opened)
	{
        if(Success)
	        return FileName;
	    else
	        return NULL;
	}
}

/* * * * * END OPENFILEDIALOG CLASS * * * * */

/* * * * * BEGIN SAVEFILEDIALOG CLASS * * * * */

typedef class SaveFileDialog
{
    public:
	  HWND _Parent;
	  LPCTSTR DefaultExt;
	  LPSTR InitialDir;
	  bool AllowMultiSelect;
	  WORD SelFilterIndex;
	  LPSTR Filter;
	  LPSTR Title;
	  void ShowDialog();
	  char* File(void);
	private:
	  char FileName[MAX_PATH];
	  bool Success;
	  bool Opened;
};

void SaveFileDialog::ShowDialog()
{
    OPENFILENAME sfn;
	
	ZeroMemory(&sfn, sizeof(sfn));
	FileName[0] = 0;
	
	sfn.lStructSize = sizeof(sfn);
	sfn.hwndOwner = _Parent;
	sfn.nMaxFile = MAX_PATH;
	sfn.lpstrFilter = Filter;
	sfn.lpstrFile = FileName;
	sfn.lpstrTitle = Title;
	sfn.nFilterIndex = SelFilterIndex;
	sfn.lpstrDefExt = DefaultExt;
	sfn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
	
	Success = GetSaveFileName(&sfn);
	Opened = true;
}

char* SaveFileDialog::File(void)
{
    if(Opened)
	{
        if(Success)
	        return FileName;
	    else
	        return NULL;
	}
}

/* * * * * END SAVEFILEDIALOG CLASS * * * * */

/* * * * * BEGIN FONTDIALOG CLASS * * * * */

typedef class FontDialog
{
    public:
	  HWND _Parent;
	  int MaxSize, MinSize;
	  bool TrueTypesOnly, LimitSize;
	  void ShowDialog();
	  HFONT Font(void);
	private:
	  bool Success;
	  bool Opened;
	  
	  LOGFONT lgFont;
	  HFONT hFont, PrevFont;
	  DWORD CurRGB, PrevRGB;
	  HDC hDC;
};

void FontDialog::ShowDialog()
{
    CHOOSEFONT CF;
    
    ZeroMemory(&CF, sizeof(CF));
    
	CF.lStructSize = sizeof (CHOOSEFONT);
    CF.hwndOwner = _Parent;
    CF.lpLogFont = &lgFont;
    CF.rgbColors = CurRGB;
	
	if(LimitSize)
	{
	    CF.nSizeMin = MinSize;
		CF.nSizeMax = MaxSize;
	}
	
	if(TrueTypesOnly)
	{
		if(LimitSize)
		    CF.Flags = CF_SCREENFONTS | CF_TTONLY | CF_LIMITSIZE;
		else
	        CF.Flags = CF_SCREENFONTS | CF_TTONLY;
	} else {
        if(LimitSize)
		    CF.Flags = CF_SCREENFONTS | CF_LIMITSIZE;
		else
			CF.Flags = CF_SCREENFONTS;
	}

    Success = ChooseFont(&CF);
    Opened = true;
}

HFONT FontDialog::Font(void)
{
    if(Opened)
	{
	    hFont = CreateFontIndirect(&lgFont);
        if(Success)
		    return hFont;
		else
		    return NULL;
	}
}

/* * * * * END FONTDIALOG CLASS * * * * */
William Hemsworth 1,339 Posting Virtuoso

Good Work.

mvmalderen 2,072 Postaholic

>only works with Dev-Cpp, may not work with any other compiler
Dev-C++ isn't a compiler, it's an IDE which uses MinGW as it's compiler :P

Nice snippet!

killdude69 8 Light Poster

You know what I meant tux4life. :)

I actually have alot more of these:
RichTextBox
TextBox
ComboBox
Label
ect.

It is like my own .NET library, lol.
They all use the WinAPI. All open source.

You can download them @: http://sourceforge.net/projects/wgcl/

That is just a beta version, I am still working on the full release.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.