如何在自定义图标的消息框上使用'MAINICON'?

xiedewei 2009-02-06 06:12:59
默认情况工程目录都有一个与工程同名的资源文件*.res,里面有一个图标资源MAINICON,
我自己又加进去一个图标101。
下面的代码可以让图标101显示在消息框上,可是如何才能在消息框上使用MAINICON呢??
请各位高人赐教。
procedure TForm1.Button1Click(Sender: TObject);
function MessageEx(lText, lCaption: PChar; lStyle: DWORD; IconId: Integer): Integer;
var
Msg: TMsgBoxParams;
begin
Msg.cbSize := Sizeof(Msg);
Msg.hwndOwner := Application.Handle;
Msg.hInstance := HInstance;
Msg.lpszText := lText;
Msg.lpszCaption := lCaption;
Msg.dwStyle := lStyle;
Msg.lpszIcon := MakeIntResource(IconId);
{如果改成Msg.lpszIcon := 'MAINICON';不能显示图标MAINICON}
Msg.dwContextHelpId := 0;
Msg.lpfnMsgBoxCallback := nil;
Msg.dwLanguageId := LANG_NEUTRAL;
Result := Integer(MessageBoxIndirect(Msg));
end;
begin
MessageEx('MessageEx', 'MessageEx', MB_OK or MB_USERICON, 101);//可以显示图标101
end;
...全文
378 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiedewei 2009-02-09
  • 打赏
  • 举报
回复
第一段代码应改为:
procedure TForm1.Button1Click(Sender: TObject);

function MessageEx(lText, lCaption: PWideChar; lStyle: DWORD; IconID: PWideChar): Integer;
var
Msg: TMsgBoxParamsW;
begin
Msg.cbSize := Sizeof(Msg);
Msg.hwndOwner := Application.Handle;
Msg.hInstance := HInstance;
Msg.lpszText := lText;
Msg.lpszCaption := lCaption;
Msg.dwStyle := lStyle;
Msg.lpszIcon := IconID;
Msg.dwContextHelpId := 0;
Msg.lpfnMsgBoxCallback := nil;
Msg.dwLanguageId := LANG_NEUTRAL;
Result := Integer(MessageBoxIndirectW(Msg));
end;

begin
MessageEx('Hello MessageEx!','MessageEx', MB_OK or MB_USERICON, 'MAINICON');{PWideChar(101)}
end;

数字ID,字符串ID都没问题了!
xiedewei 2009-02-09
  • 打赏
  • 举报
回复
哈哈终于搞定了!
通过十六进制比对资源文件(*.res)我发现'MAINICON'的十六进制串是这样的:'AD 00 41 00 49 00 4E 00 49 00 43 00 4F 00 4E 00',豁然开朗了:
测试Msg.lpszIcon := 'M'+#0+'A'+#0+'I'+#0+'N'+#0+'I'+#0+'C'+#0+'O'+#0+'N'+#0;成功!
或者说应该直接使用宽字符的MessageBoxIndirectW:
procedure TForm1.Button1Click(Sender: TObject);
var
Msg: TMsgBoxParamsW;
begin
Msg.cbSize := Sizeof(Msg);
Msg.hwndOwner := Application.Handle;
Msg.hInstance := HInstance;
Msg.lpszText := 'MY MAINICON !';
Msg.lpszCaption := 'MAINICON';
Msg.dwStyle := MB_OK or MB_USERICON;
Msg.lpszIcon := 'MAINICON';
Msg.dwContextHelpId := 0;
Msg.lpfnMsgBoxCallback := nil;
Msg.dwLanguageId := LANG_NEUTRAL;
MessageBoxIndirectW(Msg);{成功显示MAINICON}
end;
xiedewei 2009-02-09
  • 打赏
  • 举报
回复
有没有高手能够实现的 我可以加分
lihuasoft 2009-02-08
  • 打赏
  • 举报
回复
我在一楼的回答有误。因从来没有用过MessageBoxIndirect,孤陋寡闻
原来TMsgBoxParams.lpszIcon就是资源文件里的资源名字。但我不会。再顶
yinjingxin3000 2009-02-08
  • 打赏
  • 举报
回复
哦,对不起,虽然什么有点“跑篇”了,但是验证了下面的想法。
不能能显示图标时因为没有找到资源。ms 实现 MessageBoxIndirect函数时,将图标资源的标识符视为一个整数,因此,不支持以字符标识表示的资源。
yinjingxin3000 2009-02-08
  • 打赏
  • 举报
回复
试了一下,这样测试正常

procedure TForm1.Button1Click(Sender: TObject);
function MessageEx(lText, lCaption: PChar; lStyle: DWORD; IconId: Integer): Integer;
var
Msg: TMsgBoxParams;
begin
Msg.cbSize := Sizeof(Msg);
Msg.hwndOwner := Application.Handle;
// Msg.hInstance := HInstance;
Msg.hInstance := 0;//如果使用系统定义的标准图标,这个句柄必须为0
Msg.lpszText := lText;
Msg.lpszCaption := lCaption;
Msg.dwStyle := lStyle;
Msg.lpszIcon := MakeIntResource(IconId);
{如果改成Msg.lpszIcon := 'MAINICON';不能显示图标MAINICON}
Msg.dwContextHelpId := 0;
Msg.lpfnMsgBoxCallback := nil;
Msg.dwLanguageId := LANG_NEUTRAL;
Result := Integer(MessageBoxIndirect(Msg));
end;
begin
MessageEx('MessageEx', 'MessageEx', MB_OK or MB_USERICON, 101);//可以显示图标101
MessageEx('MessageEx', 'MessageEx', MB_OK or MB_USERICON, 32512);//应用程序图标

end;
yinjingxin3000 2009-02-08
  • 打赏
  • 举报
回复
IDI_APPLICATION = MakeIntResource(32512);

MessageEx('MessageEx', 'MessageEx', MB_OK or MB_USERICON,32512 );//可以显示图标101

shishi
xiedewei 2009-02-07
  • 打赏
  • 举报
回复
这段代码可以直接使用的,请有时间的朋友帮帮忙啦
xiedewei 2009-02-07
  • 打赏
  • 举报
回复
TMsgBoxParams、MessageBoxIndirect都是在Windows.pas定义的,
MessageBoxIndirect是API函数,TMsgBoxParams.lpszIcon的类型是PAnsiChar。
都不是我自定义的。
请楼上兄弟测试一下,多谢
xiedewei 2009-02-07
  • 打赏
  • 举报
回复
楼上通过测试的?
hidelphi 2009-02-07
  • 打赏
  • 举报
回复
Msg.lpszIcon := Application.Icon.Handle
xiedewei 2009-02-07
  • 打赏
  • 举报
回复
测试了MessageEx的朋友顶个贴啊
xiedewei 2009-02-07
  • 打赏
  • 举报
回复
真的可以吗 我试了怎么不行呀?
我测试结果:
Msg.lpszIcon := Application.Icon.Handle;//报错类型不匹配
MessageEx('MessageEx', 'MessageEx', MB_OK or MB_USERICON, application.icon.handle);//显示空白
不得闲你是怎么写的?
xieguoking 2009-02-07
  • 打赏
  • 举报
回复
增加些人气。。。。
不得闲 2009-02-07
  • 打赏
  • 举报
回复
2楼说了
application.icon.handle就可以了
lihuasoft 2009-02-06
  • 打赏
  • 举报
回复
不知道你的TMsgBoxParams.lpszIcon是什么类型的(代码中未给出定义部分)

如果是TIcon类型的,可以这样:Msg.lpszIcon := Application.Icon;
如果是HICON类型的,可以这样:Msg.lpszIcon := Application.Icon.Handle;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧