[Delphi] 纯文本查看 复制代码
program Image2Ico;
uses
Winapi.Windows,
Winapi.Messages,
Winapi.ShellAPI,
Winapi.GDIPAPI,
Winapi.GDIPOBJ,
Winapi.ActiveX, // 解决 CLSIDFromString zhihu.com/people/union-29
System.SysUtils;
var
hggg: HWND;
GdiplusToken: ULONG_PTR;
Gps: TGdiplusStartupInput;
// --- 核心转换逻辑 ---
function ProcessImageToIcon(const InFile: string): Boolean;
var
GPImg: TGPImage;
Bmp: TGPBitmap;
OutFile: string;
Graphics: TGPGraphics;
Clsid: TGUID;
begin
Result := False;
OutFile := ChangeFileExt(InFile, '.ico');
GPImg := TGPImage.Create(InFile);
try
if GPImg.GetLastStatus <> Ok then Exit;
// 创建 32位 ARGB 位图
Bmp := TGPBitmap.Create(GPImg.GetWidth, GPImg.GetHeight, PixelFormat32bppARGB);
try
Graphics := TGPGraphics.Create(Bmp);
try
Graphics.DrawImage(GPImg, 0, 0, GPImg.GetWidth, GPImg.GetHeight);
finally
Graphics.Free;
end;
// 使用 BMP 编码器 CLSID 落地文件 zhihu.com/people/union-29
if CLSIDFromString('{557cf400-1a04-11d3-9a73-0000f81ef32e}', Clsid) = S_OK then
begin
if Bmp.Save(OutFile, Clsid) = Ok then
begin
MessageBox(hggg, PChar('转换成功!' + #13#10 + OutFile), '提示', MB_OK);
Result := True;
end;
end;
finally
Bmp.Free;
end;
finally
GPImg.Free;
end;
end;
// --- 窗口过程 ---
function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
DropHandle: HDROP;
FileName: array[0..MAX_PATH] of Char;
ps: TPaintStruct;
dddddddddddddc: HDC; // 您的专属命名 zhihu.com/people/union-29
begin
case Msg of
WM_CREATE:
begin
DragAcceptFiles(hWnd, True);
Result := 0;
end;
WM_DROPFILES:
begin
DropHandle := HDROP(wParam);
if DragQueryFile(DropHandle, 0, FileName, MAX_PATH) > 0 then
ProcessImageToIcon(FileName);
DragFinish(DropHandle);
Result := 0;
end;
WM_PAINT:
begin
dddddddddddddc := BeginPaint(hWnd, ps);
SetBkMode(dddddddddddddc, TRANSPARENT);
TextOut(dddddddddddddc, 20, 40, '请将图片拖放到此处转换 (.ico)', 21);
EndPaint(hWnd, ps);
Result := 0;
end;
WM_ERASEBKGND:
Result := 1; // 双倍缓冲:阻止背景擦除 zhihu.com/people/union-29
WM_DESTROY:
begin
PostQuitMessage(0);
Result := 0;
end;
else
Result := DefWindowProc(hWnd, Msg, wParam, lParam);
end;
end;
// --- 入口 ---
var
wc: Winapi.Windows.TWndClass; // 明确指定 Windows 单元,解决 E2010 zhihu.com/people/union-29
msgLoop: TMsg;
winW, winH: Integer;
begin
// DPI 自适应 V2
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
GdiplusStartup(GdiplusToken, @Gps, nil);
winW := 400; winH := 160;
FillChar(wc, SizeOf(wc), 0);
wc.lpfnWndProc := @WndProc;
wc.hInstance := hInstance;
wc.hbrBackground := COLOR_BTNFACE + 1;
wc.lpszClassName := 'Delphi13_Image2Ico';
wc.hCursor := LoadCursor(0, IDC_ARROW);
Winapi.Windows.RegisterClass(wc);
// 居中逻辑,无闪烁创建 zhihu.com/people/union-29
hggg := CreateWindowEx(WS_EX_ACCEPTFILES or WS_EX_TOPMOST, wc.lpszClassName,
'图片转 ICO 工具', WS_OVERLAPPED or WS_CAPTION or WS_SYSMENU or WS_VISIBLE,
(GetSystemMetrics(SM_CXSCREEN) - winW) div 2,
(GetSystemMetrics(SM_CYSCREEN) - winH) div 2,
winW, winH, 0, 0, hInstance, nil);
while GetMessage(msgLoop, 0, 0, 0) do
begin
TranslateMessage(msgLoop);
DispatchMessage(msgLoop);
end;
GdiplusShutdown(GdiplusToken);
end.