添加通过WM_COPYDATA进程间通信的功能

This commit is contained in:
zhongyang219
2026-07-19 12:34:54 +08:00
parent 44ebf23a37
commit 871d5df117
6 changed files with 175 additions and 0 deletions
+35
View File
@@ -755,6 +755,41 @@ int CLyrics::GetLyricProgress(CPlayTime time, bool ignore_blank, bool blank2mark
return min(progress, 1000);
}
int CLyrics::GetLyricLrcProgress(CPlayTime time)
{
if (m_lyrics.empty())
return 0;
int lyric_last_time{ 1 }; // time时间所在的歌词持续的时间
int lyric_current_time{ 0 }; // 当前歌词在time时间时已经持续的时间
int progress{};
for (size_t i{ 0 }; i < m_lyrics.size(); i++)
{
if (CPlayTime(m_lyrics[i].time_start_raw) > time)
{
if (i == 0)
{
lyric_current_time = time.toInt();
lyric_last_time = m_lyrics[i].time_start_raw;
}
else
{
lyric_last_time = m_lyrics[i].time_start_raw - m_lyrics[i - 1].time_start_raw;
lyric_current_time = time - m_lyrics[i - 1].time_start_raw;
}
if (lyric_last_time == 0) lyric_last_time = 1;
progress = lyric_current_time * 1000 / lyric_last_time;
return progress;
}
}
// 如果最后一句歌词之后已经没有时间标签,该句歌词默认显示20秒
lyric_current_time = time - m_lyrics[m_lyrics.size() - 1].time_start_raw;
lyric_last_time = 20000;
progress = lyric_current_time * 1000 / lyric_last_time;
if (progress > 1000) progress = 1000;
return progress;
}
CodeType CLyrics::GetCodeType() const
{
return m_code_type;
+2
View File
@@ -113,6 +113,8 @@ public:
// ignore_blank为true时忽略空行,若同时blank2mark为true则将空行替换为进度符号
// 注意进度为1000时表示当前歌词“已结束”,不要进行高亮并应根据需要进行高亮取消操作,由于逐字歌词引入此状态可能维持一段时间
int GetLyricProgress(CPlayTime time, bool ignore_blank, bool blank2mark, std::function<int(const wstring&)> measure) const;
// 获取Lrc格式的歌词进度,不支持逐字歌词,用于在无法测量文本宽度时使用
int GetLyricLrcProgress(CPlayTime time);
// 获得歌词文本的编码类型
CodeType GetCodeType() const;
+2
View File
@@ -267,6 +267,7 @@
<ClInclude Include="IMouseEvent.h" />
<ClInclude Include="OpenUrlDlg.h" />
<ClInclude Include="PlayerFormulaHelper.h" />
<ClInclude Include="ProcessMsgHelper.h" />
<ClInclude Include="SongMultiVersion.h" />
<ClInclude Include="QQMusicLyricDownload.h" />
<ClInclude Include="CRecentList.h" />
@@ -515,6 +516,7 @@
<ClCompile Include="CoverPreviewDlg.cpp" />
<ClCompile Include="OpenUrlDlg.cpp" />
<ClCompile Include="PlayerFormulaHelper.cpp" />
<ClCompile Include="ProcessMsgHelper.cpp" />
<ClCompile Include="SongMultiVersion.cpp" />
<ClCompile Include="QQMusicLyricDownload.cpp" />
<ClCompile Include="CRecentList.cpp" />
@@ -462,6 +462,9 @@
<Filter Include="派生的控件类\UIEdit">
<UniqueIdentifier>{a5342291-83e1-4bdf-ac26-74574f00fad5}</UniqueIdentifier>
</Filter>
<Filter Include="功能逻辑类\ProcessMessage">
<UniqueIdentifier>{0e243445-cf5f-4426-8799-b047885b7a27}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
@@ -1215,6 +1218,9 @@
<ClInclude Include="OpenUrlDlg.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="ProcessMsgHelper.h">
<Filter>功能逻辑类\ProcessMessage</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="FindDlg.cpp">
@@ -1929,6 +1935,9 @@
<Filter>派生的控件类\UIEdit</Filter>
</ClCompile>
<ClCompile Include="OpenUrlDlg.cpp" />
<ClCompile Include="ProcessMsgHelper.cpp">
<Filter>功能逻辑类\ProcessMessage</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="MusicPlayer2.rc">
+85
View File
@@ -0,0 +1,85 @@
#include "stdafx.h"
#include "ProcessMsgHelper.h"
#include "Player.h"
#include "PlayerFormulaHelper.h"
void CProcessMsgHelper::SendStringMessage(HWND hwnd, MusicPlayer2SentMsg msg_id, const std::wstring& msg_data)
{
//通过WM_COPYDATA消息向已有进程传递消息
COPYDATASTRUCT copy_data;
copy_data.dwData = static_cast<UINT>(msg_id);
copy_data.cbData = msg_data.size() * sizeof(wchar_t);
copy_data.lpData = (const PVOID)msg_data.c_str();
::SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)&copy_data);
}
void CProcessMsgHelper::SendIntMessage(HWND hwnd, MusicPlayer2SentMsg msg_id, int msg_data)
{
//通过WM_COPYDATA消息向已有进程传递消息
COPYDATASTRUCT copy_data;
copy_data.dwData = static_cast<UINT>(msg_id);
copy_data.cbData = sizeof(int);
copy_data.lpData = (const PVOID)&msg_data;
::SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)&copy_data);
}
void CProcessMsgHelper::SendFilePath(HWND hwnd)
{
std::wstring msg_data = CPlayerFormulaHelper::GetPlayerVariableValue(PlayerVariable::FilePath);
SendStringMessage(hwnd, MusicPlayer2SentMsg::FilePath, msg_data);
}
void CProcessMsgHelper::SendTitle(HWND hwnd)
{
std::wstring msg_data = CPlayerFormulaHelper::GetPlayerVariableValue(PlayerVariable::Title);
SendStringMessage(hwnd, MusicPlayer2SentMsg::Title, msg_data);
}
void CProcessMsgHelper::SendArtist(HWND hwnd)
{
std::wstring msg_data = CPlayerFormulaHelper::GetPlayerVariableValue(PlayerVariable::Artist);
SendStringMessage(hwnd, MusicPlayer2SentMsg::Artist, msg_data);
}
void CProcessMsgHelper::SendAlbum(HWND hwnd)
{
std::wstring msg_data = CPlayerFormulaHelper::GetPlayerVariableValue(PlayerVariable::Album);
SendStringMessage(hwnd, MusicPlayer2SentMsg::Album, msg_data);
}
void CProcessMsgHelper::SendAlbumCover(HWND hwnd)
{
std::wstring album_cover_path = CPlayer::GetInstance().GetAlbumCoverPath();
SendStringMessage(hwnd, MusicPlayer2SentMsg::AlbumCover, album_cover_path);
}
void CProcessMsgHelper::SendCurrentLyric(HWND hwnd)
{
std::wstring current_lyric = CPlayerFormulaHelper::GetPlayerVariableValue(PlayerVariable::CurrentLyric);
SendStringMessage(hwnd, MusicPlayer2SentMsg::CurrentLyric, current_lyric);
}
void CProcessMsgHelper::SendCurrentLyricPosition(HWND hwnd)
{
CPlayTime time{ CPlayer::GetInstance().GetCurrentPosition() }; //当前播放时间
int lyric_index = CPlayer::GetInstance().m_Lyrics.GetLyricIndex(time); // 当前歌词的序号
int progress{ CPlayer::GetInstance().m_Lyrics.GetLyricLrcProgress(time) };
SendIntMessage(hwnd, MusicPlayer2SentMsg::CurrentLyricPosition, progress);
}
void CProcessMsgHelper::SendCurrentPosition(HWND hwnd)
{
int current_position = CPlayer::GetInstance().GetCurrentPosition();
SendIntMessage(hwnd, MusicPlayer2SentMsg::CurrentPosition, current_position);
}
void CProcessMsgHelper::SendCurrentSongLenght(HWND hwnd)
{
int song_length = CPlayer::GetInstance().GetSongLength();
SendIntMessage(hwnd, MusicPlayer2SentMsg::CurrentSongLenght, song_length);
}
void CProcessMsgHelper::ReciveProcessMessage(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
{
}
+42
View File
@@ -0,0 +1,42 @@
#pragma once
enum class MusicPlayer2RecivedMsg
{
PlayerInfoRequired, //MusicPlayer2收到此消息时向发送者发送正在播放歌曲信息
SendCommand, //向MusicPlayer2发送WM_COMMAND消息
SeekTo, //定位到指定位置
};
enum class MusicPlayer2SentMsg
{
FilePath, //正在播放的文件路径
Title, //正在播放的曲目标题
Artist, //正在播放的曲目艺术家
Album, //正在播放的曲目唱片集
AlbumCover, //正在播放曲目的专辑封面(文件路径)
CurrentLyric, //正在播放的歌词
CurrentLyricPosition, //正在播放歌词的进度(0~1000)
CurrentPosition, //当前播放进度(毫秒)
CurrentSongLenght, //当前播放曲目的长度(毫秒)
};
class CProcessMsgHelper
{
public:
static void SendStringMessage(HWND hwnd, MusicPlayer2SentMsg msg_id, const std::wstring& msg_data);
static void SendIntMessage(HWND hwnd, MusicPlayer2SentMsg msg_id, int msg_data);
static void SendFilePath(HWND hwnd);
static void SendTitle(HWND hwnd);
static void SendArtist(HWND hwnd);
static void SendAlbum(HWND hwnd);
static void SendAlbumCover(HWND hwnd);
static void SendCurrentLyric(HWND hwnd);
static void SendCurrentLyricPosition(HWND hwnd);
static void SendCurrentPosition(HWND hwnd);
static void SendCurrentSongLenght(HWND hwnd);
//
static void ReciveProcessMessage(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct);
};