dcc32编译报错E2010:tagLOGFONTW与Pointer类型不兼容求助
Delphi dcc32编译错误E2010解决
错误信息
MultiTabEditor.pas(425): E2010 Incompatible types: 'tagLOGFONTW' and 'Pointer'
需求说明
选中字体列表框中的字体后,让另一个列表框显示该字体的可用样式。
问题代码
枚举字体样式的回调函数
function EnumFontStylesProc(var LogFont: TLogFont; var TextMetric: TTextMetric; FontType: Integer; Data: Pointer): Integer; stdcall; var StylesList: TStrings; StyleName: string; begin // Cast the Data pointer back to our TStrings object (ListBoxStyles.Items) StylesList := TStrings(Data); // Determine the style name based on the font metrics if (LogFont.lfWeight >= FW_BOLD) and (LogFont.lfItalic <> 0) then StyleName := 'Bold Italic' else if (LogFont.lfWeight >= FW_BOLD) then StyleName := 'Bold' else if (LogFont.lfItalic <> 0) then StyleName := 'Italic' else StyleName := 'Regular'; // Add the determined style name to the list box if it's not already present if StylesList.IndexOf(StyleName) = -1 then StylesList.Add(StyleName); Result:= 1; // Return 1 to continue the enumeration process end;
字体列表框点击事件
procedure TTabbedEditor.FontListBoxClick(Sender: TObject); var SelectedFontName: string; DC: HDC; LogFont: TLogFont; begin // Font Styles load Options. FontStyleListBox.Items.Clear; // Clear previous styles if FontListBox.ItemIndex = -1 then Exit; SelectedFontName := FontListBox.Items[FontListBox.ItemIndex]; // Get a device context (DC) for the screen DC := GetDC(0); try // Initialize the LOGFONT structure ZeroMemory(@LogFont, SizeOf(LogFont)); StrLCopy(LogFont.lfFaceName, PChar(SelectedFontName), SizeOf(LogFont.lfFaceName) - 1); LogFont.lfCharSet := DEFAULT_CHARSET; // Enumerate styles for the selected font family, passing ListBox1.Items as Data EnumFontFamiliesEx(DC, @LogFont, @EnumFontStylesProc, LPARAM(FontStyleListBox.Items), 0); // Error Here finally ReleaseDC(0, DC); end; // Select the first style (usually 'Regular') if FontStyleListBox.Items.Count > 0 then FontStyleListBox.ItemIndex := 0; // Optional: Apply the font immediately to the Memo (requires next step) FontStyleListBoxClick(nil) end;
错误原因与解决方法
核心错误原因
编译错误出现在EnumFontFamiliesEx的调用行,原因是第二个参数传递类型不匹配:
Delphi中EnumFontFamiliesEx的WinAPI声明里,第二个参数要求是const LOGFONT(值类型的TLogFont),但你传递了@LogFont(指向TLogFont的指针),导致编译器提示tagLOGFONTW(即TLogFont)与Pointer类型不兼容。
修复步骤
- 修改
EnumFontFamiliesEx的第二个参数,去掉@符号,直接传递LogFont变量:
EnumFontFamiliesEx(DC, LogFont, @EnumFontStylesProc, LPARAM(FontStyleListBox.Items), 0);
- 修复代码末尾的语法错误:
FontStyleListBoxClick(nil)后面缺少分号,改为:
FontStyleListBoxClick(nil);
额外优化建议
- 回调函数中判断字体样式时,可以简化逻辑:
if (LogFont.lfWeight >= FW_BOLD) then begin if LogFont.lfItalic <> 0 then StyleName := 'Bold Italic' else StyleName := 'Bold'; end else begin if LogFont.lfItalic <> 0 then StyleName := 'Italic' else StyleName := 'Regular'; end;
- 当前代码通过
IndexOf避免重复添加样式,该逻辑可以保留以确保列表唯一性。
内容的提问来源于stack exchange,提问作者Wayne Barron
相关产品推荐
相关产品推荐

