ListBox example for Dev C++

vegaseat 0 Tallied Votes 1K Views Share

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.

// test the listbox creation and selection
// modified from BCX generated C code for Dev-C++
// a Dev-C++ tested Windows Application by  vegaseat  04nov2004

#include <windows.h>

static HINSTANCE BCX_hInstance;
static int     BCX_ScaleX;
static int     BCX_ScaleY;
static char    BCX_ClassName[2048];  // default size
static char    text[2048];
static HWND    Form1;
static HWND    List1;

#define Show(Window)  RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);

HWND    BCX_Form(char*,int=0,int=0,int=250,int=150,int=0,int=0);
HWND    BCX_Listbox(char*,HWND,int,int,int,int,int,int=0,int=-1);
void    Center (HWND,HWND=0,HWND=0);
char*   BCX_TmpStr(size_t);

void    FormLoad (void);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void    addLB (HWND, char *);
char    * getLB (HWND);

// sample data for the list box
static char names[7][10]=
{
  "Heidi", "Bertha", "Samantha", "Rubin", 
  "Frank", "Sandy", "Sunny"
};


// this is the standard windows main() function
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR CmdLine,int CmdShow)
{
 WNDCLASS Wc;
 MSG      Msg;
 // *****************************
 strcpy(BCX_ClassName,"ListBox1");
 // ***************************************
 // Programmer has selected to use pixels
 // ***************************************
 BCX_ScaleX       = 1;          // for generic scaling
 BCX_ScaleY       = 1;
 BCX_hInstance    =  hInst;
 // ******************************************************
 Wc.style         =  CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
 Wc.lpfnWndProc   =  WndProc;
 Wc.cbClsExtra    =  0;
 Wc.cbWndExtra    =  0;
 Wc.hInstance     =  hInst;
 Wc.hIcon         =  LoadIcon(NULL,IDI_WINLOGO);
 Wc.hCursor       =  LoadCursor(NULL,IDC_ARROW);
 Wc.hbrBackground =  (HBRUSH)(COLOR_BTNFACE+1);
 Wc.lpszMenuName  =  NULL;
 Wc.lpszClassName =  BCX_ClassName;
 RegisterClass(&Wc);

 FormLoad();
  // the event message loop
 while(GetMessage(&Msg,NULL,0,0))
   {
    HWND hActiveWindow = GetActiveWindow();
    if (!IsWindow(hActiveWindow) || !IsDialogMessage(hActiveWindow,&Msg))
      {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
      }
    }
 return Msg.wParam;
}


// circular storage for strings
char *BCX_TmpStr (size_t Bites)
{
  static int   StrCnt;
  static char *StrFunc[2048];
  StrCnt=(StrCnt + 1) & 2047;
  if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
  return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
}


// center the form in the screen (really optional, for looks)
void Center (HWND hwnd, HWND Xhwnd, HWND Yhwnd)
{
  RECT rect, rectP;
  int  x, y, width, height;
  int  screenwidth, screenheight;
  if(Xhwnd==0)
    {
      RECT  DesktopArea;
      RECT  rc;
      SystemParametersInfo(SPI_GETWORKAREA,0,&DesktopArea,0);
      GetWindowRect(hwnd,&rc);
      SetWindowPos(hwnd,HWND_TOP,
        ((DesktopArea.right-DesktopArea.left)-(rc.right-rc.left))/2+
          DesktopArea.left,((DesktopArea.bottom-DesktopArea.top)-
         (rc.bottom-rc.top))/2 + DesktopArea.top,0,0,SWP_NOSIZE);
      return;
    }
  GetWindowRect (hwnd,&rect);
  GetWindowRect (Xhwnd,&rectP);
  width = rect.right-rect.left;
  x = ((rectP.right-rectP.left)-width)/2 + rectP.left;
  if(Yhwnd==NULL)
    {
      height = rect.bottom-rect.top;
      y = ((rectP.bottom-rectP.top)-height)/2 + rectP.top;
    }
  else
    {
      GetWindowRect(Yhwnd,&rectP);
      height = rect.bottom-rect.top;
      y = ((rectP.bottom-rectP.top)-height)/2+rectP.top;
    }
  screenwidth = GetSystemMetrics(SM_CXSCREEN);
  screenheight = GetSystemMetrics(SM_CYSCREEN);
  if ((x<0)) x=0;
  if ((y<0)) y=0;
  if ((x+width>screenwidth))   x = screenwidth-width;
  if ((y+height>screenheight)) y = screenheight-height;
  MoveWindow (hwnd, x, y, width, height, FALSE);
}


// create the windows form
HWND BCX_Form(char *Caption, int X, int Y, int W, int H, int Style, int Exstyle)
{
   HWND  A;
   // assign a default style
   if (!Style)
   {
     Style= WS_MINIMIZEBOX  |
     WS_SIZEBOX      |
     WS_CAPTION      |
     WS_MAXIMIZEBOX  |
     WS_POPUP        |
     WS_SYSMENU;
   }
   A = CreateWindowEx(Exstyle,BCX_ClassName,Caption,
     Style,
     X*BCX_ScaleX,
     Y*BCX_ScaleY,
     (4+W)*BCX_ScaleX,
     (12+H)*BCX_ScaleY,
     NULL,(HMENU)NULL,BCX_hInstance,NULL);
   SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),
     (LPARAM)MAKELPARAM(FALSE,0));
   return A;
}


// create the list box with desired styles
HWND BCX_Listbox
(char* Text,HWND hWnd,int id,int X,int Y,int W,int H,int Style,int Exstyle)
{ 
  HWND  A;
  // assign a default style if needed
  if (!Style)
  {
    Style=LBS_STANDARD | WS_CHILD | WS_VISIBLE |
    LBS_SORT | WS_VSCROLL | WS_TABSTOP;
  }
  if (Exstyle == -1)
  {
    Exstyle=WS_EX_CLIENTEDGE;
  }
  A = CreateWindowEx(Exstyle,"Listbox",NULL,Style,
        X*BCX_ScaleX, Y*BCX_ScaleY, W*BCX_ScaleX, H*BCX_ScaleY,
        hWnd,(HMENU)id,BCX_hInstance,NULL);
  SendMessage(A,(UINT)WM_SETFONT,(WPARAM)GetStockObject
        (DEFAULT_GUI_FONT),(LPARAM)MAKELPARAM(FALSE,0));
  return A;
}


// the details for Form1 and List1 (corner,width,height)
void FormLoad (void)
{
  static char A[2048];
  memset(&A,0,sizeof(A));
  static int      k;
  memset(&k,0,sizeof(k));
  Form1=BCX_Form("Click on a name ...",0,0,260,200);
  List1=BCX_Listbox("",Form1,1009,10,15,100,150);
  for(k=0; k<=6; k+=1)
  {
    strcpy(A,(char*)names[k]);
    addLB(List1,A);
  }
  Center(Form1);   // optional
  Show(Form1);
}


// standard windows message handling
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  while(1)
  {
    if (Msg==WM_COMMAND)
    {
      //  list box item clicked (selected) 
      if (LOWORD(wParam)==1009)
      {
        if (HIWORD(wParam)==LBN_SELCHANGE)
        {
          strcpy(text,(char*)getLB(List1));
          //  just for test 
          Beep(400,500);
          //  selected item to form title 
          SetWindowText(Form1,text);
        }
      }
    }
    break;
  }
  // exit from the window form
  if (Msg==WM_DESTROY)
  {
    UnregisterClass(BCX_ClassName,BCX_hInstance);
    PostQuitMessage(0);
  }
  return DefWindowProc(hWnd,Msg,wParam,lParam);
}


//  add a string to the listbox 
void addLB (HWND idnr, char *ltext)
{
  SendMessage(idnr,(UINT)LB_ADDSTRING,(WPARAM)0,(LPARAM)ltext);
}

//  return selected listbox string
char * getLB (HWND idnr)
{
  static int      index;
  memset(&index,0,sizeof(index));
  static char buf[2048];
  memset(&buf,0,sizeof(buf));
  char *BCX_RetStr={0};
  index=SendMessage(idnr,(UINT)LB_GETCURSEL,(WPARAM)0,(LPARAM)0);
  SendMessage(idnr,(UINT)LB_GETTEXT,(WPARAM)index,(LPARAM)buf);
  BCX_RetStr=BCX_TmpStr(strlen(buf));
  strcpy(BCX_RetStr,buf);
  return BCX_RetStr;
}

// *************************************************************
//   Created with BCX -- The BASIC To C Translator (ver 5.02)
//  BCX (c) 1999, 2000, 2001, 2002, 2003, 2004 by Kevin Diggins
// *************************************************************
paracavern 0 Newbie Poster

Cheers, thats exactly what i was looking for!

lashatt2 -5 Light Poster

how can I move listbox items? I mean ownerdrawn listbox. how can I use HIMAGELIST on listbox?

lashatt2 -5 Light Poster

I can send messages on lan on local but I cann't send messages on internet on global. please help me. do you have a source of sending messages on global on internet?

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.