Code:
#include <windows.h>
#include <commctrl.h>
#include <mmsystem.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "AmbientWindow";
/* This is the main function where the stuff happens */
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
/* This purge loads all the sounds into memory */
PlaySound("", hThisInstance, SND_PURGE);
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
InitCommonControls();
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon(hThisInstance, "A");
wincl.hIconSm = LoadIcon(hThisInstance, "A");
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
int winX = 434;
int winY = 265;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Ambient", /* Title Text */
WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
GetSystemMetrics(SM_CXSCREEN)/2-winX/2, /* Windows decides the position */
GetSystemMetrics(SM_CYSCREEN)/2-winY/2, /* where the window ends up on the screen */
winX, /* The programs width */
winY, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_CREATE:
{
char spAddStrings[][99] = { { "White" },
{ "B" },
{ "h" },
{ "h" },
{ "!" } };
int i;
CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTBOX, "Hello", WS_CHILD | WS_VISIBLE | TVS_HASLINES | WS_VSCROLL, 10, 10, 180, 200, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
SendDlgItemMessage(hwnd, 1, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
for(i = 0; spAddStrings[i][0] != '\0'; i++)
SendMessage(GetDlgItem(hwnd,1), LB_ADDSTRING, 0,(LPARAM)spAddStrings[i]);
}
break;
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case 1:
{
int nSelection = SendMessage(GetDlgItem(hwnd,1), LB_GETCURSEL, (WPARAM)0, (LPARAM)0);
char szSelection[MAX_PATH];
ZeroMemory(szSelection,MAX_PATH);
wsprintf(szSelection,"AMBIENT%d",nSelection);
PlaySound(szSelection, hThisInstance, SND_SYNC | SND_LOOP);
return 0;
}
break;
}
break;
}
break;
case WM_DESTROY:
{
/* This purge loads just stops all sounds before quiting */
PlaySound("", NULL, SND_PURGE);
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
I have tried some other combanations of code just to make it worse. And I still haven't got it. How can I get PlaySound() to play when I want it too?