944,014 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 1550
  • C++ RSS
0

WinAPI Dialogs class without MFC

by on May 31st, 2009
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.
C++ Code Snippet (Toggle Plain Text)
  1. /* This partial library from WGCL only works with Dev-Cpp, may not work with any other compiler */
  2.  
  3. /*
  4.  * This source code/file is part of the WGCL C++ Compiler Library.
  5.  *
  6.  * WGCL: Windows GUI Compiler Library
  7.  * Version: 1.2
  8.  * Original Mod date: 10/11/2008
  9.  * Partial Product Release Date: 5/31/2009
  10.  * Author: Michael Brandon Miller
  11.  * Original FileName: Dialogs.h
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this library; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. /* * * * * BEGIN COLORDIALOG CLASS * * * * */
  24.  
  25. typedef class ColorDialog
  26. {
  27. public:
  28. HWND _Parent;
  29. bool AllowAnyColor;
  30. void ShowDialog();
  31. COLORREF Color(void);
  32. private:
  33. COLORREF DefaultColor;
  34. COLORREF Result;
  35. bool Success;
  36. bool Opened;
  37. };
  38.  
  39. void ColorDialog::ShowDialog()
  40. {
  41. CHOOSECOLOR color;
  42. Result = DefaultColor;
  43.  
  44. ZeroMemory(&color, sizeof(color));
  45.  
  46. color.hwndOwner = _Parent;
  47. color.lStructSize = sizeof(color);
  48.  
  49. if (AllowAnyColor)
  50. color.Flags = CC_RGBINIT | CC_FULLOPEN | CC_ANYCOLOR;
  51. else
  52. color.Flags = CC_RGBINIT | CC_FULLOPEN;
  53.  
  54. color.rgbResult = Result;
  55. Success = ChooseColor(&color);
  56. Opened = true;
  57. }
  58.  
  59. COLORREF ColorDialog::Color(void)
  60. {
  61. if(Opened)
  62. {
  63. if(Success)
  64. return Result;
  65. else
  66. return NULL;
  67. }
  68. }
  69.  
  70. /* * * * * END COLORDIALOG CLASS * * * * */
  71.  
  72. /* * * * * BEGIN OPENFILEDIALOG CLASS * * * * */
  73.  
  74. typedef class OpenFileDialog
  75. {
  76. public:
  77. HWND _Parent;
  78. LPCTSTR DefaultExt;
  79. LPSTR InitialDir;
  80. bool AllowMultiSelect;
  81. WORD SelFilterIndex;
  82. LPSTR Filter;
  83. LPSTR Title;
  84. void ShowDialog();
  85. char* File(void);
  86. private:
  87. char FileName[MAX_PATH];
  88. DWORD flags;
  89. bool Success;
  90. bool Opened;
  91. };
  92.  
  93. void OpenFileDialog::ShowDialog()
  94. {
  95. OPENFILENAME ofn;
  96.  
  97. ZeroMemory(&ofn, sizeof(ofn));
  98. FileName[0] = 0;
  99.  
  100. ofn.lStructSize = sizeof(ofn);
  101. ofn.hwndOwner = _Parent;
  102. ofn.nMaxFile = MAX_PATH;
  103. ofn.lpstrFilter = Filter;
  104. ofn.lpstrFile = FileName;
  105. ofn.lpstrTitle = Title;
  106. ofn.nFilterIndex = SelFilterIndex;
  107. ofn.lpstrDefExt = DefaultExt;
  108.  
  109. if(AllowMultiSelect == true)
  110. flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT;
  111. else
  112. flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
  113.  
  114. ofn.Flags = flags;
  115.  
  116. Success = GetOpenFileName(&ofn);
  117. Opened = true;
  118. }
  119.  
  120. char* OpenFileDialog::File(void)
  121. {
  122. if(Opened)
  123. {
  124. if(Success)
  125. return FileName;
  126. else
  127. return NULL;
  128. }
  129. }
  130.  
  131. /* * * * * END OPENFILEDIALOG CLASS * * * * */
  132.  
  133. /* * * * * BEGIN SAVEFILEDIALOG CLASS * * * * */
  134.  
  135. typedef class SaveFileDialog
  136. {
  137. public:
  138. HWND _Parent;
  139. LPCTSTR DefaultExt;
  140. LPSTR InitialDir;
  141. bool AllowMultiSelect;
  142. WORD SelFilterIndex;
  143. LPSTR Filter;
  144. LPSTR Title;
  145. void ShowDialog();
  146. char* File(void);
  147. private:
  148. char FileName[MAX_PATH];
  149. bool Success;
  150. bool Opened;
  151. };
  152.  
  153. void SaveFileDialog::ShowDialog()
  154. {
  155. OPENFILENAME sfn;
  156.  
  157. ZeroMemory(&sfn, sizeof(sfn));
  158. FileName[0] = 0;
  159.  
  160. sfn.lStructSize = sizeof(sfn);
  161. sfn.hwndOwner = _Parent;
  162. sfn.nMaxFile = MAX_PATH;
  163. sfn.lpstrFilter = Filter;
  164. sfn.lpstrFile = FileName;
  165. sfn.lpstrTitle = Title;
  166. sfn.nFilterIndex = SelFilterIndex;
  167. sfn.lpstrDefExt = DefaultExt;
  168. sfn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
  169.  
  170. Success = GetSaveFileName(&sfn);
  171. Opened = true;
  172. }
  173.  
  174. char* SaveFileDialog::File(void)
  175. {
  176. if(Opened)
  177. {
  178. if(Success)
  179. return FileName;
  180. else
  181. return NULL;
  182. }
  183. }
  184.  
  185. /* * * * * END SAVEFILEDIALOG CLASS * * * * */
  186.  
  187. /* * * * * BEGIN FONTDIALOG CLASS * * * * */
  188.  
  189. typedef class FontDialog
  190. {
  191. public:
  192. HWND _Parent;
  193. int MaxSize, MinSize;
  194. bool TrueTypesOnly, LimitSize;
  195. void ShowDialog();
  196. HFONT Font(void);
  197. private:
  198. bool Success;
  199. bool Opened;
  200.  
  201. LOGFONT lgFont;
  202. HFONT hFont, PrevFont;
  203. DWORD CurRGB, PrevRGB;
  204. HDC hDC;
  205. };
  206.  
  207. void FontDialog::ShowDialog()
  208. {
  209. CHOOSEFONT CF;
  210.  
  211. ZeroMemory(&CF, sizeof(CF));
  212.  
  213. CF.lStructSize = sizeof (CHOOSEFONT);
  214. CF.hwndOwner = _Parent;
  215. CF.lpLogFont = &lgFont;
  216. CF.rgbColors = CurRGB;
  217.  
  218. if(LimitSize)
  219. {
  220. CF.nSizeMin = MinSize;
  221. CF.nSizeMax = MaxSize;
  222. }
  223.  
  224. if(TrueTypesOnly)
  225. {
  226. if(LimitSize)
  227. CF.Flags = CF_SCREENFONTS | CF_TTONLY | CF_LIMITSIZE;
  228. else
  229. CF.Flags = CF_SCREENFONTS | CF_TTONLY;
  230. } else {
  231. if(LimitSize)
  232. CF.Flags = CF_SCREENFONTS | CF_LIMITSIZE;
  233. else
  234. CF.Flags = CF_SCREENFONTS;
  235. }
  236.  
  237. Success = ChooseFont(&CF);
  238. Opened = true;
  239. }
  240.  
  241. HFONT FontDialog::Font(void)
  242. {
  243. if(Opened)
  244. {
  245. hFont = CreateFontIndirect(&lgFont);
  246. if(Success)
  247. return hFont;
  248. else
  249. return NULL;
  250. }
  251. }
  252.  
  253. /* * * * * END FONTDIALOG CLASS * * * * */
Comments on this Code Snippet
May 31st, 2009
0

Re: WinAPI Dialogs class without MFC

Good Work.
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
May 31st, 2009
0

Re: WinAPI Dialogs class without MFC

>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

Nice snippet!
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jun 1st, 2009
0

Re: WinAPI Dialogs class without MFC

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.
Light Poster
killdude69 is offline Offline
45 posts
since Jul 2008
Message:
Previous Thread in C++ Forum Timeline: How do I use custom icons for toolbar?
Next Thread in C++ Forum Timeline: Need help with payroll programm with structures C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC