taohe 发表于 2006-2-12 17:06:23

〔新年礼物〕在用mcc编译exe时,指定icon

论坛上不时有人问起如何修改mcc产生的exe文件的光标。缺省情况下,mcc产生的exe并不包含任何资源信息(Resource),在Windows Explorer里看起来,该exe的光标就像是个普通的命令行程序。

首先想到的一个方法就是利用工具软件比如ResourceHacker来修改光标,但是在缺省情况下,mcc产生的exe文件并不包含任何资源信息。所以这条路行不通。

后来有想到那么就给该exe加一个任意一个资源文件,然后再使用ResourceHacker修改,实践证明这样是可行的。具体作起来需要:1. 修改相应的compopts.bat文件,在连接器linker的命令行后面加上我们将要使用的res文件名。你可以从任何VC工程获得一个res用到这里。2.然后照样用mcc命令编译。结果,产生的exe已经有资源了,虽然这时光标不是我们想要的,但我们已经可以用ResourceHacker等来修改光标了。这个方法虽然看起来解决了问题,可惜不完美,而且第一次由mcc产生的exe的光标无法定制。我最开始的时候想到的是这个方法,但迟迟没有贴出来,主要是因为我想找到更好的方法后再和大家分享。

过年期间抽空重新思考了一下,找到了更为简单和完善的方法。不用修改compopts.bat,而且可以在执行mcc命令时指定所用的光标文件。具体方法如下:
第一步:首先创建一个简单资源源文件(rc文件),这是一个文本文件,在其中加上如下一行:

appicon ICON "myicon.ico"

其中myicon.ico则是需要使用的光标文件。

第二步:在mcc的命令行加上刚刚创建的rc文件。比如刚才产生的rc文件取名为myapp.rc,我们需要编译的matlab应用程序是myapp01.m,那么可以用如下命令编译:

mcc -m myapp01.m myapp.rc

哇!结果你将会发现,产生的myapp01.exe在Windows Explorer中的光标就是我们指定的。

好了,利用这个方法,以及令外一些在matlab程序中改变Figure窗口图标的方法,我们完全可以实现对编译后的exe文件光标的完全定制。(关于修改Figure窗口的光标的方法,在论坛上和拙作《深入浅出matlab7.x混合编程》中都有提到)

顺祝大家新年好!

nostalgica 发表于 2006-2-21 17:38:25

Re:〔新年礼物〕在用mcc编译exe时,指定icon

强烈支持!

晨曦1007 发表于 2007-6-26 09:49:58

写的真好,谢谢!

raigu 发表于 2009-1-6 10:30:20

谢谢!!学习了:handshake

cwit 发表于 2009-1-6 22:33:10

这个方法不错,很简单。
以前都是采用一个有图标的exe文件,来引导这个黑窗口文件。
多谢多谢!

鼻涕佛 发表于 2009-1-8 15:20:24

可以借助nsis

http://www.chinavib.com/forum/thread-5382-1-1.html
1. Compile your GUI into an exe. We'll call it "foo.exe".
2. Download and install NSIS - the installation program created and
used by the fine folks at Nullsoft (think Winamp):

http://www.nullsoft.com/free/nsis/

3. Make an NSIS script text file in the same directory as your exe,
let's call it "file.nsi". Put the following text in there:

SilentInstall silent

; The name of the NSIS install program you're creating
Name "NotSeen"

; The file that NSIS writes
OutFile "nocmdwindow.exe"

Section "Ignore"
; Change this exe file to the name of the exe you created
nsExec::Exec "cmd /C foo.exe"
SectionEnd

4. Save the file and compile it into the new exe ("nocmdwindow.exe")
by right clicking on it and selecting Compile. Or you can open the
"Make NSIS GUI" window and select "Load Script..." from the File menu.

5. Now run "nocmdwindow.exe". As promised, no command window. And
so simple to implement. Plus, if you ever want to see the command
window for diagnostic purposes, you can always run the original exe
file ("foo.exe"), and it will run normally with its glorious DOS
window.

You also have a really easy to use and powerful Windows installer
program that's free. The no command window trick works with NSIS by
calling a dll (nsExec.dll) and running the program through it,
suppressing all text output, but leaving popup windows alone.

摇摇 发表于 2011-12-28 21:27:44

注册论坛来就是为了来读taohe老师的帖子,谢谢分享。
页: [1]
查看完整版本: 〔新年礼物〕在用mcc编译exe时,指定icon