📄 simpleui.cpp
字号:
{
if(!m_liveInTray)
{
extendedStyles ^= WS_EX_TOOLWINDOW;
RemoveTrayIcon();
}
}
else
{
if(m_liveInTray)
{
extendedStyles |= WS_EX_TOOLWINDOW;
AddTrayIcon();
}
}
SetWindowLong(m_hwnd, GWL_STYLE, WS_VISIBLE);
SetWindowLong(m_hwnd, GWL_STYLE, styles);
SetWindowLong(m_hwnd, GWL_EXSTYLE, extendedStyles);
ShowWindow(m_hwnd, TRUE);
}
void
SimpleUI::
AddTrayIcon()
{
NOTIFYICONDATA nid;
int rc;
// Fill out NOTIFYICONDATA structure
nid.cbSize = sizeof(NOTIFYICONDATA); // sanitycheck
nid.hWnd = m_hwnd; // window to receive notifications
nid.uID = 1; // application defined identifier for icon
nid.uFlags = NIF_MESSAGE | // uCallbackMessage is valid, use it
NIF_ICON | // hIcon is valid, use it
NIF_TIP; // there is tooltip specified
nid.uCallbackMessage = UWM_TRAY; // that's what we will receive in wndProc
nid.hIcon = m_trayIcon;
strcpy(nid.szTip, m_trayTooltip);
rc = Shell_NotifyIcon(NIM_ADD, &nid); // this adds the icon
}
void
SimpleUI::
RemoveTrayIcon()
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON;
nid.hIcon = m_trayIcon;
Shell_NotifyIcon(NIM_DELETE, &nid);
}
void
SimpleUI::
SetTrayTooltip(char *str)
{
NOTIFYICONDATA nid;
strncpy(m_trayTooltip, str, sizeof(m_trayTooltip));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hwnd;
nid.uID = 1;
nid.uFlags = NIF_TIP; // just change tip
strcpy(nid.szTip, m_trayTooltip);
Shell_NotifyIcon(NIM_MODIFY, &nid); // now, modify our tooltip
}
Error
SimpleUI::
Init(int32 startup_type)
{
ParseArgs(m_context->argc, m_context->argv);
return kError_NoErr;
}
BOOL CALLBACK SimpleUI::MainProc( HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam )
{
BOOL result = FALSE;
static SimpleUI* m_ui = NULL;
switch(msg)
{
case WM_INITDIALOG:
{
HWND hwndStatus = NULL;
HWND hwndPlay = GetDlgItem(hwnd, IDC_PLAY);
HWND hwndStop = GetDlgItem(hwnd, IDC_STOP);
HWND hwndPause = GetDlgItem(hwnd, IDC_PAUSE);
HWND hwndNext = GetDlgItem(hwnd, IDC_NEXTSONG);
HWND hwndLast = GetDlgItem(hwnd, IDC_LASTSONG);
HWND hwndSlider = GetDlgItem(hwnd, IDC_SLIDER);
EnableWindow(hwndPlay, FALSE);
EnableWindow(hwndNext, FALSE);
EnableWindow(hwndLast, FALSE);
EnableWindow(hwndStop, FALSE);
EnableWindow(hwndPause, FALSE);
EnableWindow(hwndSlider, FALSE);
hwndStatus= CreateStatusWindow( WS_CHILD | WS_VISIBLE,
"",
hwnd,
IDC_STATUS);
const int32 kNumPanes = 3;
int32 panes[kNumPanes]= {60, 125,-1};
SendMessage(hwndStatus, SB_SETPARTS, kNumPanes, (LPARAM) panes);
m_ui = (SimpleUI*)lParam;
m_ui->SetHwnd(hwnd);
m_ui->m_trayIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_EXE_ICON));
m_ui->ReadPreferences();
m_ui->SetTrayTooltip("Welcome to "the_BRANDING);
result = TRUE;
m_ui->m_uiSemaphore->Signal();
break;
}
case WM_COMMAND:
{
static bool isPaused = false;
switch(wParam)
{
case IDC_PLAY:
{
if(isPaused)
{
m_ui->m_target->AcceptEvent(new Event(CMD_TogglePause));
isPaused = false;
}
else
{
m_ui->m_target->AcceptEvent(new Event(CMD_Play));
}
break;
}
case IDC_PAUSE:
{
isPaused = true;
m_ui->m_target->AcceptEvent(new Event(CMD_TogglePause));
break;
}
case IDC_STOP:
{
m_ui->m_target->AcceptEvent(new Event(CMD_Stop));
break;
}
case IDC_NEXTSONG:
{
m_ui->m_target->AcceptEvent(new Event(CMD_NextMediaPiece));
break;
}
case IDC_LASTSONG:
{
m_ui->m_target->AcceptEvent(new Event(CMD_PrevMediaPiece));
break;
}
case IDC_OPEN:
{
OPENFILENAME ofn;
char szTitle[] = "Open Audio File";
char szFilter[] =
"MPEG Audio Streams (.mp1, .mp2, .mp3, .mpp)\0"
"*.mp1;*.mp2;*.mp3;*.mpp\0"
//"Playlists (.txt, .lst, .m3u)\0"
//"*.lst;*.m3u;*.txt\0"
"All Files (*.*)\0"
"*.*\0";
const int32 kBufferSize = MAX_PATH * 128;
char* filelist = new char[kBufferSize];
*filelist = 0x00;
// Setup open file dialog box structure
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = (HINSTANCE)GetWindowLong(hwnd,
GWL_HINSTANCE);
ofn.lpstrFilter = szFilter;
ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 1;
ofn.lpstrFile = filelist;
ofn.nMaxFile = kBufferSize;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = "";
ofn.lpstrTitle = szTitle;
ofn.Flags = OFN_FILEMUSTEXIST |
OFN_PATHMUSTEXIST |
OFN_HIDEREADONLY |
OFN_ALLOWMULTISELECT |
OFN_EXPLORER;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = "MP*";
ofn.lCustData = 0;
ofn.lpfnHook = NULL;
ofn.lpTemplateName = NULL;
if(GetOpenFileName(&ofn))
{
char file[MAX_PATH + 1];
char* cp = NULL;
PlaylistManager* Playlist = m_ui->m_plm;
Playlist->RemoveAll();
strcpy(file, filelist);
strcat(file, "\\");
cp = filelist + ofn.nFileOffset;
while(*cp)
{
strcpy(file + ofn.nFileOffset, cp);
Playlist->AddItem(file,0);
cp += strlen(cp) + 1;
}
EnableWindow(m_ui->m_hwndPlay, TRUE);
}
delete [] filelist;
break;
}
case IDC_ABOUT:
{
HINSTANCE hinst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
DialogBox( hinst,
MAKEINTRESOURCE(IDD_ABOUT),
NULL,
AboutProc);
break;
}
case IDC_EDIT_PREFS:
{
HINSTANCE hinst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
if(DisplayPreferences(hwnd, m_ui->m_prefs))
m_ui->ReadPreferences();
break;
}
case IDC_EXIT:
{
PostQuitMessage(0);
break;
}
}
result = TRUE;
break;
}
case UWM_TRAY:
{
switch(lParam)
{
case WM_LBUTTONUP:
{
ShowWindow( hwnd, SW_NORMAL);
SetForegroundWindow(hwnd);
break;
}
case WM_RBUTTONUP:
{
HMENU menuHandle, popupHandle;
HINSTANCE hinst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
POINT pt;
// need mouse coordinates relative to screen
GetCursorPos(&pt);
// load the menu and retrieve its popup
menuHandle = LoadMenu(hinst, MAKEINTRESOURCE(IDM_TRAY));
popupHandle = GetSubMenu(menuHandle, 0);
if(m_ui->m_state != UIState_Stopped)
{
MENUITEMINFO mii;
memset(&mii, 0x00, sizeof(MENUITEMINFO));
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_TYPE|MIIM_ID ;
mii.fType = MFT_STRING;
mii.dwTypeData = "Stop";
mii.cch = strlen("Stop");
mii.wID = IDC_STOP;
SetMenuItemInfo(popupHandle,
4,
TRUE,
&mii);
}
if(m_ui->m_state == UIState_Paused)
{
MENUITEMINFO mii;
memset(&mii, 0x00, sizeof(MENUITEMINFO));
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STATE;
mii.fState = MFS_CHECKED;
SetMenuItemInfo(popupHandle,
5,
TRUE,
&mii);
}
// display the popup
TrackPopupMenu( popupHandle,
TPM_RIGHTBUTTON,
pt.x,
pt.y,
0,
hwnd,
NULL);
break;
}
}
break;
}
case WM_DROPFILES:
{
HDROP hDrop = (HDROP) wParam;
int32 count;
char szFile[MAX_PATH + 1];
count = DragQueryFile( hDrop,
-1L,
szFile,
sizeof(szFile));
for(int32 i = 0; i < count; i++)
{
DragQueryFile( hDrop,
i,
szFile,
sizeof(szFile));
m_ui->m_plm->AddItem(szFile,0);
}
EnableWindow(m_ui->m_hwndPlay, TRUE);
break;
}
case WM_HSCROLL:
{
HWND hwndSlider = GetDlgItem(hwnd, IDC_SLIDER);
LRESULT position;
switch (LOWORD(wParam))
{
case TB_LINEUP:
case TB_LINEDOWN:
case TB_PAGEDOWN:
case TB_PAGEUP:
case TB_THUMBPOSITION:
{
position = SendMessage( hwndSlider,
TBM_GETPOS,
0,
0);
char timeString[256] = "00:00:00";
int32 seconds = m_ui->m_totalSeconds;
int32 hours = seconds / 3600;
int32 minutes = seconds / 60 - hours * 60;
seconds = seconds - minutes * 60 - hours * 3600;
sprintf(timeString,"%02d:%02d:%02d",hours,
minutes,
seconds);
SetWindowText(m_ui->m_hwndTotal, timeString);
m_ui->m_target->AcceptEvent(new ChangePositionEvent(position));
m_ui->m_scrolling = false;
break;
}
case TB_THUMBTRACK:
{
position = SendMessage( hwndSlider, TBM_GETPOS,0,0);
char timeString[256] = "00:00:00";
int32 seconds = (int32)ceil(m_ui->m_secondsPerFrame * position);
int32 hours = seconds / 3600;
int32 minutes = seconds / 60 - hours * 60;
seconds = seconds - minutes * 60 - hours * 3600;
sprintf(timeString,"%02d:%02d:%02d",hours,
minutes,
seconds);
SetWindowText(m_ui->m_hwndTotal, timeString);
m_ui->m_scrolling = true;
break;
}
}
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
result = TRUE;
break;
}
case WM_DESTROY:
{
m_ui->RemoveTrayIcon();
break;
}
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -