Convert a console project into a Win32 GUI project or the other way around.

Jeffrey Richter 在他的经典著作中提供了两种简单的方法来解决开发者在创建Win32项目时误设为Console项目的问题。通过修改项目设置中的链接选项,可以轻松更正项目类型,从而避免不必要的麻烦。

Jeffrey Richter mentioned in his classical Programming application for Windows, 4th edition that if a developer made a mistake by creating a Win32 project when what he really meant was a Console project, he has at least the following 2 options to fix it. (Jeffery Richter mentioned 4 options actually. But the first 2 are not interesting at all.)

  • Click on the Link tab of the Project Settings dialog box and change the /SUBSYSTEM:WINDOWS switch to /SUBSYSTEM :CONSOLE. This is an easy way to fix the problem; few people are aware that this is all they have to do.
  • Click on the Link tab of the Project Settings dialog box and delete the /SUBSYSTEM:WINDOWS switch entirely. This is my favorite choice because it gives you the most flexibility. Now, the linker will simply do the right thing based on which function you implement in your source code. I have no idea why this isn't the default when you create a new Win32 Application or Win32 Console Application project with Visual C++'s Developer Studio.

Reader may come to the conclusion that the project type, or consequently the application type, is determined by the entry point function name you choose. If you use main(), you'll get a console project and get an console application in the end, if you choose WinMain(), you'll get a Win32 project and a Win32 application in the end.

 

Not necessarily. The above statement is only true when the /ENTRY option is not used for the linker. With this option, you can specify any function name as the Entry Point Function of your application. If you do so, whether your application will be a console application or a Win32 application will be solely determined by the /SUBSYSTEM option.

 

This means, it's completely OK to have a GUI application that starts with a main() function. It's also completely OK to have a console application that starts with a WinMain() function. Name doesn't matter!

### 将用户输入的年龄字符串转换为整数 在 C# 中,可以使用 `Convert.ToInt32` 方法将用户从控制台输入的字符串转换为整数。以下是实现这一功能的完整代码示例: ```csharp Console.Write("请输入一个年龄:"); string ageInput = Console.ReadLine(); // 获取用户输入的字符串 int age = Convert.ToInt32(ageInput); // 将字符串转换为整数 Console.WriteLine("您输入的年龄是:" + age); // 输出转换后的整数 ``` 上述代码中,`Console.ReadLine()` 用于获取用户输入的字符串[^3]。然后,通过调用 `Convert.ToInt32` 方法将字符串类型的年龄转换为整数类型[^3]。 需要注意的是,如果用户输入的内容无法转换为整数(例如输入了非数字字符),程序会抛出异常。为了提高程序的健壮性,可以使用 `int.TryParse` 方法来安全地进行转换: ```csharp Console.Write("请输入一个年龄:"); string ageInput = Console.ReadLine(); // 获取用户输入的字符串 int age; bool isParsed = int.TryParse(ageInput, out age); // 尝试将字符串转换为整数 if (isParsed) { Console.WriteLine("您输入的年龄是:" + age); // 如果转换成功,输出年龄 } else { Console.WriteLine("输入无效,请输入一个有效的整数。"); // 如果转换失败,提示用户 } ``` 在此代码中,`int.TryParse` 方法尝试将字符串转换为整数,并返回一个布尔值以指示转换是否成功[^1]。 ### 注意事项 - 使用 `Convert.ToInt32` 时,如果输入的字符串不是有效的整数格式,程序会抛出 `FormatException` 异常。 - 使用 `int.TryParse` 是一种更安全的方式,因为它不会抛出异常,而是通过返回值告知转换结果。 ### 示例输出 假设用户输入 `25`,程序将输出: ``` 请输入一个年龄:25 您输入的年龄是:25 ``` 如果用户输入了无效内容(如 `abc`),则使用 `int.TryParse` 的版本会输出: ``` 请输入一个年龄:abc 输入无效,请输入一个有效的整数。 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值