示例#1
0
        /// <summary>
        /// Send a command to MPC-HC
        /// </summary>
        /// <param name="nCmd">Command to send.</param>
        /// <param name="strCmd">Some commands require a parameter.</param>
        private void SendData(MPCAPI_SENDCOMMAND nCmd, string strCmd)
        {
            WinAPI.COPYDATASTRUCT nCDS;
            if (nCmd == MPCAPI_SENDCOMMAND.CMD_OSDSHOWMESSAGE)
            {
                MPC_OSDDATA osdData = new MPC_OSDDATA
                {
                    nMsgPos     = (int)m_OSDMSGPos,
                    nDurationMS = m_OSDMSGDur,
                    strMsg      = strCmd
                };
                nCDS = new WinAPI.COPYDATASTRUCT
                {
                    dwData = (IntPtr)(int)nCmd,
                    cbData = Marshal.SizeOf(osdData)
                };
                nCDS.lpData = Marshal.AllocCoTaskMem(nCDS.cbData);
                Marshal.StructureToPtr(osdData, nCDS.lpData, false);
            }
            else
            {
                nCDS = new WinAPI.COPYDATASTRUCT
                {
                    dwData = (IntPtr)(int)nCmd,
                    cbData = (strCmd.Length + 1) * 2,
                    lpData = Marshal.StringToCoTaskMemUni(strCmd)
                };
            }

            IntPtr cdsPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(nCDS));

            Marshal.StructureToPtr(nCDS, cdsPtr, false);

            WinAPI.SendMessage(m_hWndMPC, WinAPI.WM_COPYDATA, m_hWnd, cdsPtr);

            Marshal.FreeCoTaskMem(cdsPtr);
            Marshal.FreeCoTaskMem(nCDS.lpData);
        }
示例#2
0
        private unsafe IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WinAPI.WM_COPYDATA)
            {
                WinAPI.COPYDATASTRUCT cds = (WinAPI.COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.COPYDATASTRUCT));

                if (cds.cbData > 0)
                {
                    MPCAPI_COMMAND nCmd   = (MPCAPI_COMMAND)cds.dwData;
                    string         mpcMSG = new String((char *)cds.lpData, 0, cds.cbData / 2 - 1);

                    switch (nCmd)
                    {
                    case MPCAPI_COMMAND.CMD_CONNECT:
                        isHooked  = true;
                        m_hWndMPC = new IntPtr(int.Parse(mpcMSG));
                        break;

                    case MPCAPI_COMMAND.CMD_CURRENTPOSITION:
                    case MPCAPI_COMMAND.CMD_NOTIFYSEEK:
                        m_currentFilePosition = double.Parse(mpcMSG);
                        if (!m_currentFileWatched && m_watchedWhen == MPC_WATCHED.DURING_TICKS &&
                            m_currentFileTick > m_currentFileLength * m_watchedWhenPerc)
                        {
                            OnFileWatched(this, new FileWatchedArgs(m_currentFilePath));
                            m_currentFileWatched = true;
                            m_PlayTimer.Stop();
                        }
                        break;

                    case MPCAPI_COMMAND.CMD_NOWPLAYING:
                        string[] playing = mpcMSG.Split('|');
                        m_currentFileTitle    = !string.IsNullOrWhiteSpace(playing[0]) ? playing[0] : System.IO.Path.GetFileName(playing[3]);
                        m_currentFilePath     = playing[3];
                        m_currentFileLength   = double.Parse(playing[4]);
                        m_currentFileWatched  = false;
                        m_currentFilePosition = m_currentFileTick = 0;
                        LoadConfig();
                        SetTitle();
                        break;

                    case MPCAPI_COMMAND.CMD_PLAYMODE:
                        m_currentPlayState = (MPC_PLAYSTATE)int.Parse(mpcMSG);
                        SetTitle();
                        switch (m_currentPlayState)
                        {
                        case MPC_PLAYSTATE.PS_PLAY:
                            if (!m_currentFileWatched)
                            {
                                m_PlayTimer.IsEnabled = true;
                            }
                            break;

                        case MPC_PLAYSTATE.PS_PAUSE:
                        case MPC_PLAYSTATE.PS_STOP:
                            m_PlayTimer.IsEnabled = false;
                            break;
                        }
                        break;

                    case MPCAPI_COMMAND.CMD_STATE:
                        MPC_LOADSTATE mpcLS = (MPC_LOADSTATE)int.Parse(mpcMSG);
                        switch (mpcLS)
                        {
                        case MPC_LOADSTATE.MLS_CLOSED:
                            IsMPCAlive();
                            break;

                        case MPC_LOADSTATE.MLS_CLOSING:
                            if (m_watchedWhen == MPC_WATCHED.AFTER_FINISHED && !m_currentFileWatched &&
                                m_currentFileTick > m_currentFileLength * m_watchedWhenPerc)
                            {
                                OnFileWatched(this, new FileWatchedArgs(m_currentFilePath));
                                m_currentFileWatched = true;
                                m_PlayTimer.Stop();
                            }
                            break;
                        }
                        break;
                    }
                }

                handled = true;
            }
            else
            {
                handled = false;
            }

            return(IntPtr.Zero);
        }