param( [switch]$ValidateOnly ) Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing if ($ValidateOnly) { exit 0 } $form = New-Object System.Windows.Forms.Form $form.Text = '设置 PinAI Key' $form.Size = New-Object System.Drawing.Size(540, 250) $form.StartPosition = 'CenterScreen' $form.FormBorderStyle = 'FixedDialog' $form.MaximizeBox = $false $form.MinimizeBox = $false $form.ShowInTaskbar = $true $title = New-Object System.Windows.Forms.Label $title.Text = '粘贴你的 PinAI API Key' $title.Font = New-Object System.Drawing.Font('Microsoft YaHei UI', 12, [System.Drawing.FontStyle]::Bold) $title.AutoSize = $true $title.Location = New-Object System.Drawing.Point(28, 24) $form.Controls.Add($title) $hint = New-Object System.Windows.Forms.Label $hint.Text = 'Key 会以圆点隐藏,并只保存到当前 Windows 用户环境变量。' $hint.AutoSize = $true $hint.Location = New-Object System.Drawing.Point(30, 58) $form.Controls.Add($hint) $keyBox = New-Object System.Windows.Forms.TextBox $keyBox.Location = New-Object System.Drawing.Point(30, 90) $keyBox.Size = New-Object System.Drawing.Size(465, 30) $keyBox.UseSystemPasswordChar = $true $keyBox.Font = New-Object System.Drawing.Font('Segoe UI', 11) $form.Controls.Add($keyBox) $saveButton = New-Object System.Windows.Forms.Button $saveButton.Text = '保存 Key' $saveButton.Size = New-Object System.Drawing.Size(105, 36) $saveButton.Location = New-Object System.Drawing.Point(275, 145) $saveButton.DialogResult = [System.Windows.Forms.DialogResult]::None $form.Controls.Add($saveButton) $cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Text = '取消' $cancelButton.Size = New-Object System.Drawing.Size(105, 36) $cancelButton.Location = New-Object System.Drawing.Point(390, 145) $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.Controls.Add($cancelButton) $form.AcceptButton = $saveButton $form.CancelButton = $cancelButton $form.Add_Shown({ $keyBox.Focus() }) $saveButton.Add_Click({ $key = $keyBox.Text.Trim() if ([string]::IsNullOrWhiteSpace($key)) { [System.Windows.Forms.MessageBox]::Show( '请先粘贴 PinAI API Key。', '未填写 Key', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning ) | Out-Null return } try { [Environment]::SetEnvironmentVariable('PINAI_API_KEY', $key, 'User') [Environment]::SetEnvironmentVariable('PINAI_API_KEY', $key, 'Process') } catch { [System.Windows.Forms.MessageBox]::Show( '保存失败。请确认当前 Windows 用户有写入环境变量的权限。', '保存失败', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error ) | Out-Null return } [System.Windows.Forms.MessageBox]::Show( 'PinAI Key 已保存。请完全退出并重新打开 Codex,然后即可生图。', '设置完成', [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information ) | Out-Null $form.Close() }) [void]$form.ShowDialog()