User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,577 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,250 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Nov 4th, 2004
Views: 22,008
This shows how to create, load and access a ListBox. BCX was used to create the original code which was then modified to work with the Dev C++ (really GCC/G++) compiler, compile as a Windows Application.
cplusplus Syntax | 3 stars
  1. // test the listbox creation and selection
  2. // modified from BCX generated C code for Dev-C++
  3. // a Dev-C++ tested Windows Application by vegaseat 04nov2004
  4.  
  5. #include <windows.h>
  6.  
  7. static HINSTANCE BCX_hInstance;
  8. static int BCX_ScaleX;
  9. static int BCX_ScaleY;
  10. static char BCX_ClassName[2048]; // default size
  11. static char text[2048];
  12. static HWND Form1;
  13. static HWND List1;
  14.  
  15. #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);
  16.  
  17. HWND BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
  18. HWND BCX_Listbox(char*,HWND,int,int,int,int,int,int=0,int=-1);
  19. void Center (HWND,HWND=0,HWND=0);
  20. char* BCX_TmpStr(size_t);
  21.  
  22. void FormLoad (void);
  23. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  24. void addLB (HWND, char *);
  25. char * getLB (HWND);
  26.  
  27. // sample data for the list box
  28. static char names[7][10]=
  29. {
  30. "Heidi", "Bertha", "Samantha", "Rubin",
  31. "Frank", "Sandy", "Sunny"
  32. };
  33.  
  34.  
  35. // this is the standard windows main() function
  36. int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
  37. {
  38. WNDCLASS Wc;
  39. MSG Msg;
  40. // *****************************
  41. strcpy(BCX_ClassName,"ListBox1");
  42. // ***************************************
  43. // Programmer has selected to use pixels
  44. // ***************************************
  45. BCX_ScaleX = 1; // for generic scaling
  46. BCX_ScaleY = 1;
  47. BCX_hInstance = hInst;
  48. // ******************************************************
  49. Wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  50. Wc.lpfnWndProc = WndProc;
  51. Wc.cbClsExtra = 0;
  52. Wc.cbWndExtra = 0;
  53. Wc.hInstance = hInst;
  54. Wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
  55. Wc.hCursor = LoadCursor(NULL,IDC_ARROW);
  56. Wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
  57. Wc.lpszMenuName = NULL;
  58. Wc.lpszClassName = BCX_ClassName;
  59. RegisterClass(&Wc);
  60.  
  61. FormLoad();
  62. // the event message loop
  63. while(GetMessage(&Msg,NULL,0,0))
  64. {
  65. HWND hActiveWindow = GetActiveWindow();
  66. if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
  67. {
  68. TranslateMessage(&Msg);
  69. DispatchMessage(&Msg);
  70. }
  71. }
  72. return Msg.wParam;
  73. }
  74.  
  75.  
  76. // circular storage for strings
  77. char *BCX_TmpStr (size_t Bites)
  78. {
  79. static int StrCnt;
  80. static char *StrFunc[2048];
  81. StrCnt=(StrCnt + 1) & 2047;
  82. if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
  83. return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
  84. }
  85.  
  86.  
  87. // center the form in the screen (really optional, for looks)
  88. void Center (HWND hwnd, HWND Xhwnd, HWND Yhwnd)
  89. {
  90. RECT rect, rectP;
  91. int x, y, width, height;
  92. int screenwidth, screenheight;
  93. if(Xhwnd==0)
  94. {
  95. RECT DesktopArea;
  96. RECT rc;
  97. SystemParametersInfo(SPI_GETWORKAREA,0,&DesktopArea,0);
  98. GetWindowRect(hwnd,&rc);
  99. SetWindowPos(hwnd,HWND_TOP,
  100. ((DesktopArea.right-DesktopArea.left)-(rc.right-rc.left))/2+
  101. DesktopArea.left,((DesktopArea.bottom-DesktopArea.top)-
  102. (rc.bottom-rc.top))/2 + DesktopArea.top,0,0,SWP_NOSIZE);
  103. return;
  104. }
  105. GetWindowRect (hwnd,&rect);
  106. GetWindowRect (Xhwnd,&rectP);
  107. width = rect.right-rect.left;
  108. x = ((rectP.right-rectP.left)-width)/2 + rectP.left;
  109. if(Yhwnd==NULL)
  110. {
  111. height = rect.bottom-rect.top;
  112. y = ((rectP.bottom-rectP.top)-height)/2 + rectP.top;
  113. }
  114. else
  115. {
  116. GetWindowRect(Yhwnd,&rectP);
  117. height = rect.bottom-rect.top;
  118. y = ((rectP.bottom-rectP.top)-height)/2+rectP.top;
  119. }
  120. screenwidth = GetSystemMetrics(SM_CXSCREEN);
  121. screenheight = GetSystemMetrics(SM_CYSCREEN);
  122. if ((x<0)) x=0;
  123. if ((y<0)) y=0;
  124. if ((x+width>screenwidth)) x = screenwidth-width;
  125. if ((y+height>screenheight)) y = screenheight-height;
  126. MoveWindow (hwnd, x, y, width, height, FALSE);
  127. }
  128.  
  129.  
  130. // create the windows form
  131. HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
  132. {
  133. HWND A;
  134. // assign a default style
  135. if (!Style)
  136. {
  137. Style= WS_MINIMIZEBOX |
  138. WS_SIZEBOX |
  139. WS_CAPTION |
  140. WS_MAXIMIZEBOX |
  141. WS_POPUP |
  142. WS_SYSMENU;
  143. }
  144. A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
  145. Style,
  146. X*BCX_ScaleX,
  147. Y*BCX_ScaleY,
  148. (4+W)*BCX_ScaleX,
  149. (12+H)*BCX_ScaleY,
  150. NULL,(HMENU)NULL,BCX_hInstance,NULL);
  151. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
  152. (LPARAM)MAKELPARAM(FALSE,0));
  153. return A;
  154. }
  155.  
  156.  
  157. // create the list box with desired styles
  158. HWND BCX_Listbox
  159. (char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Style,int Exstyle)
  160. {
  161. HWND A;
  162. // assign a default style if needed
  163. if (!Style)
  164. {
  165. Style=LBS_STANDARD | WS_CHILD | WS_VISIBLE |
  166. LBS_SORT | WS_VSCROLL | WS_TABSTOP;
  167. }
  168. if (Exstyle == -1)
  169. {
  170. Exstyle=WS_EX_CLIENTEDGE;
  171. }
  172. A = CreateWindowEx(Exstyle,"Listbox",NULL,Style,
  173. X*BCX_ScaleX, Y*BCX_ScaleY, W*BCX_ScaleX, H*BCX_ScaleY,
  174. hWnd,(HMENU)id,BCX_hInstance,NULL);
  175. SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject
  176. (DEFAULT_GUI_FONT),(LPARAM)MAKELPARAM(FALSE,0));
  177. return A;
  178. }
  179.  
  180.  
  181. // the details for Form1 and List1 (corner,width,height)
  182. void FormLoad (void)
  183. {
  184. static char A[2048];
  185. memset(&A,0,sizeof(A));
  186. static int k;
  187. memset(&k,0,sizeof(k));
  188. Form1=BCX_Form("Click on a name ...",0,0,260,200);
  189. List1=BCX_Listbox("",Form1,1009,10,15,100,150);
  190. for(k=0; k<=6; k+=1)
  191. {
  192. strcpy(A,(char*)names[k]);
  193. addLB(List1,A);
  194. }
  195. Center(Form1); // optional
  196. Show(Form1);
  197. }
  198.  
  199.  
  200. // standard windows message handling
  201. LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  202. {
  203. while(1)
  204. {
  205. if (Msg==WM_COMMAND)
  206. {
  207. // list box item clicked (selected)
  208. if (LOWORD(wParam)==1009)
  209. {
  210. if (HIWORD(wParam)==LBN_SELCHANGE)
  211. {
  212. strcpy(text,(char*)getLB(List1));
  213. // just for test
  214. Beep(400,500);
  215. // selected item to form title
  216. SetWindowText(Form1,text);
  217. }
  218. }
  219. }
  220. break;
  221. }
  222. // exit from the window form
  223. if (Msg==WM_DESTROY)
  224. {
  225. UnregisterClass(BCX_ClassName,BCX_hInstance);
  226. PostQuitMessage(0);
  227. }
  228. return DefWindowProc(hWnd,Msg,wParam,lParam);
  229. }
  230.  
  231.  
  232. // add a string to the listbox
  233. void addLB (HWND idnr, char *ltext)
  234. {
  235. SendMessage(idnr,(UINT)LB_ADDSTRING,(WPARAM)0,(LPARAM)ltext);
  236. }
  237.  
  238. // return selected listbox string
  239. char * getLB (HWND idnr)
  240. {
  241. static int index;
  242. memset(&index,0,sizeof(index));
  243. static char buf[2048];
  244. memset(&buf,0,sizeof(buf));
  245. char *BCX_RetStr={0};
  246. index=SendMessage(idnr,(UINT)LB_GETCURSEL,(WPARAM)0,(LPARAM)0);
  247. SendMessage(idnr,(UINT)LB_GETTEXT,(WPARAM)index,(LPARAM)buf);
  248. BCX_RetStr=BCX_TmpStr(strlen(buf));
  249. strcpy(BCX_RetStr,buf);
  250. return BCX_RetStr;
  251. }
  252.  
  253. // *************************************************************
  254. // Created with BCX -- The BASIC To C Translator (ver 5.02)
  255. // BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
  256. // *************************************************************
  257.  
Comments (Newest First)
paracavern | Newbie Poster | Mar 29th, 2008
Cheers, thats exactly what i was looking for!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 2:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC