Nu har jeg samlet mit program med en Progress bar men den virker bare ikke :/
#include <windows.h>
#include <commctrl.h>
#include <string>
#include <sstream>
#include <vfw.h>
#include <stdio.h>
#define IDC_STATIC 1002
#define IDC_TIMER 1003
#define IDC_ProgressCtrlId 1004
enum CtrlIdEnmu
{
OpenButton = 1030,
PlayButton,
ProgressText
};
enum WindowsId
{
FirstEditId = 256,
FirstUpDownId,
SecondEditId,
SecondUpDownId,
ResultListBoxId,
SubRadioId,
AddRadioId,
HexCheckId,
ButtonId,
GroupBoxId,
ProgressCtrlId
};
enum TimerId
{
ProgressTimer = 1024
};
enum StateEnum
{
IdleState,
StoppedState,
PlayingState
};
StateEnum State = IdleState;
HINSTANCE InstanceHandle;
HWND MciWnd;
HWND PlayButtonWnd;
bool TimerRunning;
int TimerIdx;
char CurrentFileName[256];
bool OnFileOpen(HWND aHwnd)
{
static char Filter[] = "mp3 (*.mp3)0*.mp30All (*.*)0*.*0";
OPENFILENAME OpenFilename;
memset(&OpenFilename, 0, sizeof(OpenFilename));
OpenFilename.lStructSize = sizeof(OpenFilename);
OpenFilename.hwndOwner = aHwnd;
OpenFilename.hInstance = InstanceHandle;
OpenFilename.lpstrFilter = Filter;
OpenFilename.lpstrFile = CurrentFileName;
OpenFilename.nMaxFile = sizeof(CurrentFileName);
OpenFilename.Flags = OFN_FILEMUSTEXIST;
OpenFilename.lpstrDefExt = "mp3";
if(GetOpenFileName(&OpenFilename))
{
return true;
}
return false;
}
BOOL CALLBACK SetFontFunc(HWND hwnd, LPARAM)
{
PostMessage(hwnd, WM_SETFONT, (WPARAM )GetStockObject(DEFAULT_GUI_FONT), TRUE);
return TRUE;
}
LRESULT CALLBACK DialogProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
MciWnd = MCIWndCreate(hwndDlg, InstanceHandle, MCIWNDF_NOTIFYPOS, "");
ShowWindow(MciWnd, SW_HIDE);
MCIWndSetTimers(MciWnd, 1000, 1000);
MCIWndUseTime(MciWnd);
CreateWindow("STATIC",
"Ver. 1.0 beta",
WS_CHILD | WS_VISIBLE,
195, 150, 100, 20,
hwndDlg,
(HMENU)(IDC_STATIC),
InstanceHandle,
0);
CreateWindow("BUTTON",
"Open",
WS_CHILD | WS_VISIBLE,
5, 25, 60, 30,
hwndDlg,
(HMENU)(OpenButton),
InstanceHandle,
0);
PlayButtonWnd = CreateWindow("BUTTON",
"Play",
WS_CHILD | WS_VISIBLE | WS_DISABLED,
70, 25, 60, 30,
hwndDlg,
(HMENU)(PlayButton),
InstanceHandle,
0);
CreateWindow("STATIC",
"",
WS_CHILD | WS_VISIBLE | SS_CENTER,
5, 5, 100, 20,
hwndDlg,
(HMENU)(ProgressText),
InstanceHandle,
0);
//----------------------------------------
CreateWindow(PROGRESS_CLASS,
"",
WS_CHILD | WS_VISIBLE | PBS_SMOOTH,
5, 60, 250, 20,
hwndDlg,
(HMENU )ProgressCtrlId,
InstanceHandle,
0);
SendDlgItemMessage(hwndDlg,
ProgressCtrlId,
PBM_SETRANGE,
0,
MAKELPARAM(0, 50));
//-----------------------------------------
SendDlgItemMessage(hwndDlg, ProgressCtrlId, PBM_SETRANGE, 0, MAKELPARAM(0, 50));
CheckDlgButton(hwndDlg, AddRadioId, BST_CHECKED);
EnumChildWindows(hwndDlg, SetFontFunc, 0);
break;
//--------------------------
case WM_TIMER:
TimerIdx++;
SendDlgItemMessage(hwndDlg,
ProgressCtrlId,
PBM_SETPOS,
TimerIdx,
0);
if(TimerIdx > 50)
{
KillTimer(hwndDlg, ProgressTimer);
TimerRunning = false;
// Do normal calculations
//-----------------------------
case WM_COMMAND:
if(HIWORD(wParam) == BN_CLICKED &&
LOWORD(wParam) == ButtonId)
if(!TimerRunning)
{
TimerIdx = 1;
TimerRunning = true;
SetTimer(hwndDlg, ProgressTimer, 100, 0);
SendDlgItemMessage(hwndDlg,
ProgressCtrlId,
PBM_SETPOS,
TimerIdx, 0);
}
}
{
switch(LOWORD(wParam))
{
case OpenButton:
if(State == PlayingState)
{
MCIWndStop(MciWnd);
SetWindowText(PlayButtonWnd, "Play");
State = StoppedState;
}
long Style;
Style = GetWindowLong(PlayButtonWnd, GWL_STYLE);
Style |= WS_DISABLED;
SetWindowLong(PlayButtonWnd, GWL_STYLE, Style);
InvalidateRect(PlayButtonWnd, 0, TRUE);
if(OnFileOpen(hwndDlg))
{
MCIWndOpen(MciWnd, CurrentFileName, 0);
State = StoppedState;
Style = GetWindowLong(PlayButtonWnd, GWL_STYLE);
Style &= ~WS_DISABLED;
SetWindowLong(PlayButtonWnd, GWL_STYLE, Style);
InvalidateRect(PlayButtonWnd, 0, TRUE);
State = StoppedState;
}
break;
case PlayButton:
if(State == StoppedState)
{
MCIWndPlay(MciWnd);
SetWindowText(PlayButtonWnd, "Stop");
State = PlayingState;
}
else
{
MCIWndStop(MciWnd);
SetWindowText(PlayButtonWnd, "Play");
State = StoppedState;
MCIWndHome(MciWnd);
}
break;
}
}
break;
case MCIWNDM_NOTIFYPOS:
char Buffer[256];
long Pos, Length;
Pos = MCIWndGetPosition(MciWnd);
Length = MCIWndGetLength(MciWnd);
sprintf(Buffer, "%ld/%ld", Pos/1000, Length/1000);
SetDlgItemText(hwndDlg, ProgressText, Buffer);
if(Pos == Length)
{
MCIWndStop(MciWnd);
SetWindowText(PlayButtonWnd, "Play");
State = StoppedState;
}
break;
}
return DefWindowProc(hwndDlg, msg, wParam, lParam);
}
void OnSize(HWND aMainWnd, int aWidth, int aHeight)
{
HWND Control;
Control = GetDlgItem(aMainWnd, ResultListBoxId);
MoveWindow(Control, 5, 60, aWidth - 10, aHeight - 170, FALSE);
Control = GetDlgItem(aMainWnd, AddRadioId);
MoveWindow(Control, 10, aHeight - 90, 50, 14, FALSE);
Control = GetDlgItem(aMainWnd, SubRadioId);
MoveWindow(Control, 60, aHeight - 90, 50, 14, FALSE);
Control = GetDlgItem(aMainWnd, HexCheckId);
MoveWindow(Control, 120, aHeight - 90, 50, 14, FALSE);
Control = GetDlgItem(aMainWnd, GroupBoxId);
MoveWindow(Control, 5, aHeight - 110, 110, 40, FALSE);
Control = GetDlgItem(aMainWnd, ButtonId);
MoveWindow(Control, 5, aHeight - 62, 75, 25, FALSE);
Control = GetDlgItem(aMainWnd, ProgressCtrlId);
MoveWindow(Control, 5, aHeight - 32, aWidth - 10, 25, FALSE);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
InstanceHandle = hInstance;
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = DialogProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "WhateverClass";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if(!RegisterClass(&wc))
return FALSE;
HWND WindowHandle = CreateWindow("WhateverClass",
"GreenFaBo - Ver. 1.0 beta",
WS_MINIMIZEBOX | WS_VISIBLE | WS_CLIPSIBLINGS |
WS_CLIPCHILDREN | WS_MAXIMIZEBOX | WS_CAPTION | WS_BORDER | WS_SYSMENU,
100, 100, 270, 200, // Position
NULL,
NULL,
InstanceHandle,
0);
MSG Msg;
while(GetMessage(&Msg, WindowHandle, 0, 0xFFFF) > 0)
{
if(!IsDialogMessage(WindowHandle, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return 0;
}