I am following "theForger's Win32 API programming tutorial" and have become stuck on one the controls example. Standard Controls: Button, Edit, List Box, Static
The aim of the example is to create a dialog box which allows the user to enter a string and the amount of times the user wants the string to be printed in a separate box (see fig below).
I believe I have followed the code exactly and included the resource and resource script files in the project folder.
My question is: when I go to run the program the process returns with "status -1" and no dialog window is shown like in the figure above. Why would this be happening? I have attached the source code, resource file and resource script below.
Source code:
Code:
#include <windows.h>
#include "resource1.h"
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
// This is where we set up the dialog box, and initialise any default values
SetDlgItemText(hwnd, IDC_TEXT, "This is a string!");
SetDlgItemInt(hwnd, IDC_NUMBER, 5, FALSE);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_ADD:
{
// When somebody clicks the add button, first we get the number
// they entered.
BOOL bSuccess;
int nTimes = GetDlgItemInt(hwnd, IDC_NUMBER, &bSuccess, FALSE);
// Then we get the string they entered.
// First we need to find out how long it is
// so that we can allocate memory.
if(bSuccess)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
if(len > 0)
{
// Now we allocate, and get the string into the buffer.
int i;
char* buf;
buf = (char*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
// Now we add the string to the list the number of times
// the user has asked us to.
for(i = 0; i < nTimes; i++)
{
int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
// Here we are associating the value nTimes with the item
// just for the heck of it, we'll use it to display later.
// Normally you would put some more useful data here, such
// as a pointer.
SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
}
// DON'T FORGET TO FREE THE MEMORY!!!
GlobalFree((HANDLE)buf);
}
else
{
MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
}
}
else
{
MessageBox(hwnd, "Couldn't translate that message :(", "Warning", MB_OK);
}
}
break;
case IDC_REMOVE:
{
// When the user clicks the Remove button, first we get the
//number of selected items.
HWND hList = GetDlgItem(hwnd, IDC_LIST);
int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
if(count != LB_ERR)
{
if(count != 0)
{
//And then allocate room to store the list of selected items.
int i;
int *buf = (int*)GlobalAlloc(GPTR, sizeof(int) * count);
SendMessage(hList, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buf);
// Now we loop through the list to remove each item
// that was selected.
// WARNING!!!
// We loop backwards, because if we loop from top to
// bottom , it would change the indexes of the other items!!
for(i = count - 1; i >= 0; i--)
{
SendMessage(hList, LB_DELETESTRING, (WPARAM)buf[i], 0);
}
GlobalFree(buf);
}
else
{
MessageBox(hwnd, "Nothing was selected!", "Warning", MB_OK);
}
}
else
{
MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
}
}
break;
case IDC_CLEAR:
SendDlgItemMessage(hwnd, IDC_LIST, LB_RESETCONTENT, 0, 0);
break;
case IDC_LIST:
switch(HIWORD(wParam))
{
case LBN_SELCHANGE:
{
// Get number of selected items.
HWND hList = GetDlgItem(hwnd, IDC_LIST);
int count = SendMessage(hList, LB_GETSELCOUNT, 0, 0);
if(count != LB_ERR)
{
// We only want to continue if one and only one item
// is selected.
if(count == 1)
{
// Since we know ahead of time we are only getting
// one index, there's no need to allocate an array.
int index;
int err = SendMessage(hList, LB_GETSELITEMS, (WPARAM)1, (LPARAM)&index);
if(err != LB_ERR)
{
// Get the data we associated with the item above
// (the number of times it was added)
int data = SendMessage(hList, LB_GETITEMDATA, (WPARAM)index, 0);
SetDlgItemInt(hwnd, IDC_SHOWCOUNT, data, FALSE);
}
else
{
MessageBox(hwnd, "Error getting selected item :(", "Warning", MB_OK);
}
}
else
{
// No items or more than one item was selected.
// Either way we aren't going to process this.
SetDlgItemText(hwnd, IDC_SHOWCOUNT, "-");
}
}
else
{
MessageBox(hwnd, "Error counting items :(", "Warning", MB_OK);
}
}
break;
}
break;
}
break;
case WM_CLOSE:
EndDialog(hwnd, 0);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return DialogBoxA(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
}
Resource file:
Code:
#define IDC_STATIC -1
#define IDD_MAIN 101
#define IDC_TEXT 1002
#define IDC_NUMBER 1003
#define IDC_LIST 1004
#define IDC_ADD 1005
#define IDC_REMOVE 1006
#define IDC_CLEAR 1007
#define IDC_SHOWCOUNT 1008
Resource script:
Code:
#include "resource1.h"
IDD_MAIN DIALOG DISCARDABLE 0, 0, 207, 156
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Controls one"
FONT 8, "MS Sans Serif"
BEGIN
LTEXT "Add" IDC_STATIC,7,10,14,8
EDITTEXT IDC_TEXT, 25,7,120,14,ES_AUTOHSCROLL
EDITTEXT IDC_NUMBER, 150,7,21,14,ES_NUMBER
LTEXT "times." IDC_STATIC, 177,10,23,8
LISTBOX IDC_LIST,7,25,138,106,LBS_NOINTEGRALHEIGHT |
LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
PUSHBUTTON "&Add",IDC_ADD,150,30,50,14
PUSHBUTTON "&Remove",IDC_REMOVE,150,47,50,14
PUSHBUTTON "&Clear",IDC_CLEAR,150,64,50,14
LTEXT "This item was added",IDC_STATIC,7,141,66,8
CTEXT "-",IDC_SHOWCOUNT,77,141,32,8
LTEXT "times",IDC_STATIC,114,141,17,8
END
Thanks (& excuse the beginner questions!)