从控制台转到win32应用程序。

时间:2021-12-23 23:05:32

After some time coding in c in a simple console, I decided I wanted to try and code an actual Win32 application. However, upon selecting the option, the sheer amount of unknown code that surfaced on my IDE (Visual Studio 2013) just to open a blank window was overwhelming, as I don't understand half of it's meaning or even what to do, since, even simple printf commands yield no result... Can someone point me to a way to understand the differences between console and application? Or at least someway I can insert my current coding knowledge in an application environment?

在简单的控制台上用c编码了一些时间之后,我决定尝试编写一个实际的Win32应用程序。然而,在选择这个选项时,在我的IDE (Visual Studio 2013)上出现了大量的未知代码,只是打开了一个空白窗口,这让我无法理解,因为我不理解其中一半的含义,甚至不知道该怎么做,因为,即使是简单的printf命令也不会产生任何结果……有人能告诉我一种理解控制台和应用程序之间的区别的方法吗?或者至少我可以在应用程序环境中插入当前的编码知识?

2 个解决方案

#1


3  

Working with the Windows API can be quite the experience for a newcomer. The act of opening a window does, certainly, involve a lot of boilerplate code. The good news is a lot of that boilerplate rarely changes, and when it does it'll be for very special circumstances that you can go research. That's why Visual Studio spits the whole mess out for you by default.

使用Windows API对于一个新手来说是很有经验的。打开窗口的行为当然涉及到大量的样板代码。好消息是,大量的样本很少发生变化,当它发生的时候,你就可以进行研究了。这就是为什么Visual Studio在默认情况下会为你弄出一团糟。

There are plenty of resources to learn with, including Microsoft itself: https://msdn.microsoft.com/en-us/library/bb384843.aspx

有很多资源可以学习,包括微软自己:https://msdn.microsoft.com/en-us/library/bb384843.aspx。

To help situate you, understand that a lot of what you're looking at that's probably confusing are typedefs used on primitive types. This helps maintain backwards compatibility with programs created for older versions of Windows, and adds semantic meanings to types. A lot of the preprocessor stuff you see in header files is actually conditional compilation - it's checking what environment you're compiling in, what version of Windows, etc. and then providing the correct typedefs so that what actually compiles works on the desired Windows platform. Stuff that's greyed out in VS2013 is stuff that Intellisense sees isn't going to be compiled based on the current #defines and a lot of it may be relatively ancient. A big part of the Windows API for a long time was a strong desire to not break backwards compatibility. This was a huge advantage for Windows, because it means my program you wrote for Windows 3.1 isn't going to be hosed by Windows 95 rolling out. Or XP, 2000, Vista, 7, 8, 10, etc. It does mean a lot of stuff makes it into the API and stays there.

为了帮助你定位,要明白你所看到的很多东西可能会让你感到困惑,那就是在原始类型中使用的typedefs。这有助于向后兼容为旧版本的Windows创建的程序,并为类型添加语义含义。您在头文件中看到的许多预处理器内容实际上都是有条件编译的——它检查您正在编译的环境、什么版本的Windows等等,然后提供正确的typedefs,以便实际编译在所需的Windows平台上的工作。在VS2013中出现的内容是智能感知看到的东西不会根据当前的#定义进行编译,很多可能是比较古老的。长期以来,Windows API的很大一部分是强烈的不破坏向后兼容性的强烈愿望。这对Windows来说是一个巨大的优势,因为它意味着你为Windows 3.1编写的程序不会被windows95的滚动所占用。或者XP, 2000, Vista, 7, 8, 10,等等,它确实意味着很多东西进入了API并停留在那里。

They try to hide a lot of that in the headers, but you'll also see deprecated input variables to functions and the like (this parameter should always be NULL...etc), and you just have to read the documentation. Thank the internet.

它们试图在header中隐藏大量的内容,但是您也会看到一些不赞成的输入变量的函数和类似的(这个参数应该总是空的…等等),并且您只需要阅读文档。感谢互联网。

For example, an LPCSTR is just a const char*, but the LPCSTR signifies that it's a null-terminated constant string. In fact, you may see that's it's not a const char*, but a CONST CHAR* where CONST and CHAR are #defines themselves to make sure the correct keywords and types are used. In your case it'll probably end up being just normal const char*.

例如,LPCSTR只是一个const char*,但是LPCSTR表示它是一个以null结尾的常量字符串。实际上,您可能会看到,它不是const char*,而是const char*,其中const和char是#定义自己,以确保使用正确的关键字和类型。在你的情况下,它很可能只是普通的const char*。

My suggestion, rather than diving into about as complicated a C-API as possible, is to look at something like SDL. SDL is a much simpler C API designed to provide an interface to the operating system's windowing, graphics, and input, while hiding the dirty details of the API, be it Windows, Mac, or Linux. https://www.libsdl.org/

我的建议是,不要像使用C-API那样深入地讨论复杂的C-API,而是看一些类似SDL的东西。SDL是一个简单得多的C API,旨在为操作系统的窗口、图形和输入提供接口,同时隐藏API的脏细节,比如Windows、Mac或Linux。https://www.libsdl.org/

It uses openGL for its graphics. If you're making any kind of game you'll be using some kind of graphics API to talk to the video card. The native Windows graphics API is DirectX, but openGL is the very commonly used cross-platform API. Both APIs allow you to make use of a computer's video hardware to render graphics.

它使用openGL的图形。如果你正在做任何一种游戏,你将使用某种图形API与视频卡对话。本地Windows图形API是DirectX,但openGL是非常常用的跨平台API。两个api都允许您使用计算机的视频硬件来呈现图形。

Edit: I'll add, since I went off and voiced an opinion on libraries, which is why these types of questions are probably frowned upon, that I think it's fair to say that SDL is the de facto standard for C third-party multimedia libraries. Also commonly mentioned is SFML, which provides much the same functionality but is more object-oriented and written in C++.

编辑:我要补充一点,因为我已经对图书馆发表了意见,这就是为什么这些类型的问题可能会被反对的原因,我认为应该说SDL是C第三方多媒体库的实际标准。通常提到的是SFML,它提供了很多相同的功能,但更面向对象,并且用c++编写。

#2


0  

In Visual Studio, when you create a win32 app project, click on next, and click on empty project. This will eliminate all the stuff that Visual Studio creates for a win32 app, but then you start with no files. You'll probably want to start with some simple example C program for windows from a book or web site.

在Visual Studio中,当您创建一个win32应用程序项目时,单击next,然后单击empty项目。这将消除Visual Studio为win32应用程序创建的所有内容,但是,您将从没有文件开始。您可能想从一个简单的示例C程序开始,从一本书或web站点的窗口。

#1


3  

Working with the Windows API can be quite the experience for a newcomer. The act of opening a window does, certainly, involve a lot of boilerplate code. The good news is a lot of that boilerplate rarely changes, and when it does it'll be for very special circumstances that you can go research. That's why Visual Studio spits the whole mess out for you by default.

使用Windows API对于一个新手来说是很有经验的。打开窗口的行为当然涉及到大量的样板代码。好消息是,大量的样本很少发生变化,当它发生的时候,你就可以进行研究了。这就是为什么Visual Studio在默认情况下会为你弄出一团糟。

There are plenty of resources to learn with, including Microsoft itself: https://msdn.microsoft.com/en-us/library/bb384843.aspx

有很多资源可以学习,包括微软自己:https://msdn.microsoft.com/en-us/library/bb384843.aspx。

To help situate you, understand that a lot of what you're looking at that's probably confusing are typedefs used on primitive types. This helps maintain backwards compatibility with programs created for older versions of Windows, and adds semantic meanings to types. A lot of the preprocessor stuff you see in header files is actually conditional compilation - it's checking what environment you're compiling in, what version of Windows, etc. and then providing the correct typedefs so that what actually compiles works on the desired Windows platform. Stuff that's greyed out in VS2013 is stuff that Intellisense sees isn't going to be compiled based on the current #defines and a lot of it may be relatively ancient. A big part of the Windows API for a long time was a strong desire to not break backwards compatibility. This was a huge advantage for Windows, because it means my program you wrote for Windows 3.1 isn't going to be hosed by Windows 95 rolling out. Or XP, 2000, Vista, 7, 8, 10, etc. It does mean a lot of stuff makes it into the API and stays there.

为了帮助你定位,要明白你所看到的很多东西可能会让你感到困惑,那就是在原始类型中使用的typedefs。这有助于向后兼容为旧版本的Windows创建的程序,并为类型添加语义含义。您在头文件中看到的许多预处理器内容实际上都是有条件编译的——它检查您正在编译的环境、什么版本的Windows等等,然后提供正确的typedefs,以便实际编译在所需的Windows平台上的工作。在VS2013中出现的内容是智能感知看到的东西不会根据当前的#定义进行编译,很多可能是比较古老的。长期以来,Windows API的很大一部分是强烈的不破坏向后兼容性的强烈愿望。这对Windows来说是一个巨大的优势,因为它意味着你为Windows 3.1编写的程序不会被windows95的滚动所占用。或者XP, 2000, Vista, 7, 8, 10,等等,它确实意味着很多东西进入了API并停留在那里。

They try to hide a lot of that in the headers, but you'll also see deprecated input variables to functions and the like (this parameter should always be NULL...etc), and you just have to read the documentation. Thank the internet.

它们试图在header中隐藏大量的内容,但是您也会看到一些不赞成的输入变量的函数和类似的(这个参数应该总是空的…等等),并且您只需要阅读文档。感谢互联网。

For example, an LPCSTR is just a const char*, but the LPCSTR signifies that it's a null-terminated constant string. In fact, you may see that's it's not a const char*, but a CONST CHAR* where CONST and CHAR are #defines themselves to make sure the correct keywords and types are used. In your case it'll probably end up being just normal const char*.

例如,LPCSTR只是一个const char*,但是LPCSTR表示它是一个以null结尾的常量字符串。实际上,您可能会看到,它不是const char*,而是const char*,其中const和char是#定义自己,以确保使用正确的关键字和类型。在你的情况下,它很可能只是普通的const char*。

My suggestion, rather than diving into about as complicated a C-API as possible, is to look at something like SDL. SDL is a much simpler C API designed to provide an interface to the operating system's windowing, graphics, and input, while hiding the dirty details of the API, be it Windows, Mac, or Linux. https://www.libsdl.org/

我的建议是,不要像使用C-API那样深入地讨论复杂的C-API,而是看一些类似SDL的东西。SDL是一个简单得多的C API,旨在为操作系统的窗口、图形和输入提供接口,同时隐藏API的脏细节,比如Windows、Mac或Linux。https://www.libsdl.org/

It uses openGL for its graphics. If you're making any kind of game you'll be using some kind of graphics API to talk to the video card. The native Windows graphics API is DirectX, but openGL is the very commonly used cross-platform API. Both APIs allow you to make use of a computer's video hardware to render graphics.

它使用openGL的图形。如果你正在做任何一种游戏,你将使用某种图形API与视频卡对话。本地Windows图形API是DirectX,但openGL是非常常用的跨平台API。两个api都允许您使用计算机的视频硬件来呈现图形。

Edit: I'll add, since I went off and voiced an opinion on libraries, which is why these types of questions are probably frowned upon, that I think it's fair to say that SDL is the de facto standard for C third-party multimedia libraries. Also commonly mentioned is SFML, which provides much the same functionality but is more object-oriented and written in C++.

编辑:我要补充一点,因为我已经对图书馆发表了意见,这就是为什么这些类型的问题可能会被反对的原因,我认为应该说SDL是C第三方多媒体库的实际标准。通常提到的是SFML,它提供了很多相同的功能,但更面向对象,并且用c++编写。

#2


0  

In Visual Studio, when you create a win32 app project, click on next, and click on empty project. This will eliminate all the stuff that Visual Studio creates for a win32 app, but then you start with no files. You'll probably want to start with some simple example C program for windows from a book or web site.

在Visual Studio中,当您创建一个win32应用程序项目时,单击next,然后单击empty项目。这将消除Visual Studio为win32应用程序创建的所有内容,但是,您将从没有文件开始。您可能想从一个简单的示例C程序开始,从一本书或web站点的窗口。