162 lines
6.6 KiB
C#
162 lines
6.6 KiB
C#
using System.IO;
|
|
using Microsoft.Win32;
|
|
using System.Diagnostics;
|
|
using HeartbeatSender;
|
|
namespace DualScreenDemo
|
|
{
|
|
public static class Program
|
|
{
|
|
// 定义全局变量
|
|
internal static DBObj.SongListManager songListManager;
|
|
//internal static ArtistManager artistManager;
|
|
internal static SerialPortManager serialPortManager;
|
|
private static PrimaryForm primaryForm; // 儲存實例的參考
|
|
public static Room room = new Room();
|
|
|
|
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
Console.WriteLine("Server V.1.2.3 202507211828");
|
|
if (Utils.Env.GetBool("IsCursor", true)) Cursor.Hide();
|
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => Cursor.Show();
|
|
//Console.WriteLine("正在與中控取得聯繫...");
|
|
var sender = new heartbeatSender();
|
|
var cherker=new DataCheck.PublicSongChecker(DBObj.SongList.PublicSong());
|
|
try
|
|
{
|
|
// COM 初始化
|
|
int hr = ComInterop.CoInitializeEx(IntPtr.Zero, ComInterop.COINIT_APARTMENTTHREADED);
|
|
if (hr < 0)
|
|
{
|
|
Console.WriteLine("Failed to initialize COM library.");
|
|
return;
|
|
}
|
|
// 初始化管理器
|
|
|
|
songListManager = new DBObj.SongListManager(); // 使用单例
|
|
//artistManager = new ArtistManager();
|
|
|
|
var commandHandler = new CommandHandler(songListManager);
|
|
serialPortManager = new SerialPortManager(commandHandler);
|
|
serialPortManager.InitializeSerialPort();
|
|
|
|
// 輸出屏幕信息
|
|
Console.WriteLine($"Virtual Screen: {SystemInformation.VirtualScreen}");
|
|
foreach (var screen in Screen.AllScreens)
|
|
{
|
|
Console.WriteLine($"Screen: {screen.DeviceName} Resolution: {screen.Bounds.Width}x{screen.Bounds.Height}");
|
|
}
|
|
|
|
// 啟動服務器
|
|
var TCP = new TCPServer();
|
|
Task.Run(() => HttpServerManager.StartServer());
|
|
|
|
// 註冊事件
|
|
Application.ApplicationExit += (sender, e) => SerialPortManager.CloseSerialPortSafely();
|
|
SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;
|
|
|
|
// 創建主窗體
|
|
primaryForm = new PrimaryForm();
|
|
primaryForm.StartPosition = FormStartPosition.Manual;
|
|
primaryForm.Location = new Point(0, 0);
|
|
primaryForm.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
|
|
|
|
// 在完整初始化後檢查狀態
|
|
if (Screen.AllScreens.Length > 1)
|
|
{
|
|
var secondaryScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary);
|
|
if (secondaryScreen != null)
|
|
{
|
|
// 确保 primaryForm 和 videoPlayerForm 已经正确初始化
|
|
if (primaryForm.videoPlayerForm == null)
|
|
{
|
|
primaryForm.videoPlayerForm = new VideoPlayerForm();
|
|
}
|
|
|
|
// 设置 videoPlayerForm 的位置和大小
|
|
// primaryForm.videoPlayerForm.StartPosition = FormStartPosition.Manual;
|
|
// primaryForm.videoPlayerForm.Location = secondaryScreen.WorkingArea.Location;
|
|
// primaryForm.videoPlayerForm.Size = secondaryScreen.WorkingArea.Size;
|
|
|
|
// 显示 videoPlayerForm 在第二显示器
|
|
primaryForm.videoPlayerForm.Show();
|
|
|
|
// 初始化公共播放列表
|
|
primaryForm.videoPlayerForm.PlayNextSong();
|
|
}
|
|
}
|
|
primaryForm.Show();
|
|
Application.Run(primaryForm);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLog(ex.ToString());
|
|
//} finally {
|
|
//SystemEvents.DisplaySettingsChanged -= OnDisplaySettingsChanged;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static void OnDisplaySettingsChanged(object sender, EventArgs e)
|
|
{
|
|
// UI操作應該放在try-catch塊中
|
|
try
|
|
{
|
|
if (Screen.AllScreens.Length > 1)
|
|
{
|
|
primaryForm.Invoke(new System.Action(() =>
|
|
{
|
|
if (primaryForm.videoPlayerForm == null)
|
|
{
|
|
var filePath = @"D:\\video\\100015-周杰倫&aMei-不該-國語-vL-100-11000001.mpg";
|
|
if (File.Exists(filePath))
|
|
{
|
|
Screen secondaryScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary);
|
|
if (secondaryScreen != null)
|
|
{
|
|
primaryForm.videoPlayerForm = new VideoPlayerForm();
|
|
// primaryForm.primaryMediaPlayerForm = new PrimaryMediaPlayerForm(primaryForm, primaryForm.secondaryMediaPlayerForm);
|
|
//primaryForm.videoPlayerForm.PlayNextSong();
|
|
primaryForm.videoPlayerForm.Show();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("File not found.");
|
|
}
|
|
}
|
|
}));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLog("Error during display settings changed: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
static void WriteLog(string message)
|
|
{
|
|
// 指定日志文件的路径
|
|
string logFilePath = Path.Combine(Application.StartupPath, "txt", "mainlog.txt");
|
|
|
|
|
|
try
|
|
{
|
|
// 使用 StreamWriter 来向日志文件追加文本
|
|
using (StreamWriter writer = new StreamWriter(logFilePath, true))
|
|
{
|
|
writer.WriteLine(String.Format("[{0}] {1}", DateTime.Now, message));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// 如果写入日志文件时发生错误,这里可以处理这些异常
|
|
// 例如:打印到控制台
|
|
Console.WriteLine(String.Format("Error writing to log file: {0}", ex.Message));
|
|
}
|
|
}
|
|
|
|
}
|
|
} |