Windows.h In Dev C%2b%2b

Posted on  by 

Introduction

Have you ever wanted to spice up your C++ console application with the odd bit of GUI magic? Perhaps pop up a dialog to ask a user for input, select a file, or display an error message? This two-part tutorial shows how you can do all that using the Windows API without having to go to the lengths of writing a full-blown Windows application.

To follow this tutorial, you will need a C++ compiler that you can use from the command line. I assume GCC (see here for how to install it), but you could also use the Microsoft command line compiler – I give brief details of how to do this at the end of the tutorial. You will also need a command line prompt, and a text editor of some sort – Notepad will do at a pinch.

This syntax prevents Visual C from linking modules that you aren't going to need in your application. Moving on we come to the include line #include windows.h. This includes all the headers you need in this application. Sometimes you may want to include, which will give you a few useful macros to use in Windows development. Dev C Programs I have been messing around with making a windows application in Dev-C I wanted to make it in a single source file, rather than a project to see if it worked. It did, other than the fact that I got the windows app, AND a DOS prompt behind it. Windows H Dev C - igoname Bloodshed Dev-C is a full-featured Integrated Development Environment (IDE) for the C/C programming language. It uses Mingw port of GCC (GNU Compiler Collection) as it's compiler. Dev-C can also be used in combination with Cygwin or any other GCC based compiler. On dev c it compiles fine. 5.Is the windows.h file in the directory from which the preprocessor reads from? The file must be in the includes folder of your Visual Studio folder because that's where the #includes/header files and such are read from. Check and see if windows.h is there along with all of its other sub-headers, dlls and lib files. Header Files in C. Header files contain definitions of Functions and Variables, which is imported or used into any C program by using the pre-processor #include statement.Header file have an extension '.h' which contains C function declaration and macro definition. H Digunakan untuk menampilan perintah: System Digunakan untuk memberi warna. Berikut contoh programnya yang menggunakan header file windows: #include #include h #include main system ('color 27'); //angka 2 memberi warna background hijau dan angka 7 mewarnai tulisan jadi putih. Windows.h is a Windows -specific header file for the C and C programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems.

A First Dialog

The easiest dialogs to use from console applications are the message boxes provided by the Windows API. Here’s about the simplest program you can write to create one:

Create this text using your editor, and save it as ex1.cpp. Now compile it with GCC (the ‘>’ in these examples indicates the command line prompt):

This will compile the code, and create an executable called ex1.exe. Now run it:

Windows.h In Dev C 2b 2b 1b

This should produce an impressive dialog:

Now take a look again at the code you wrote:

This pulls in the declarations of most (but not all) of the commonly-used functions in the Windows C API. Almost all Windows programs that do anything with the API will need to include this header.

This calls the MessageBox function which is part of the Windows API. I do not propose in this tutorial to explain all the parameters of such functions in detail, but here they are the handle of the owning window (using zero makes the desktop the owner), the message and the caption you want displayed, and some options, which have not been specified.

Now, let’s turn this into a semi-useful program:

Save this as ex2.cpp and compile (this is the last time I’ll be giving specific instructions to do this):

Now run it like this:

Which should produce:

This can be quite a handy program to use in Windows batch (.CMD) files, where it can provide status information at various points in the processing.

Getting Input

Message boxes can also be used to get input from the user. Let’s create a version of the previous program which prompts the user for a yes/no response:

This uses the options of the MessageBox function to specify that you would like a Yes and a No button, and a question mark icon. As you have specified the MB_YESNO option, the function will now return one of two special values – IDYES or IDNO, depending on which button the user hits, which get tested and results in the corresponding input on standard output, using the normal C++ stream output facilities. The dialog it displays looks like this:

This is also a handy program (here called ‘ex3’ ) to use in a batch file:

There are other button and icon combinations you can use – see the MSDN documentation on MessageBox for details. However, very simple Yes/No and OK/Cancel responses is about all that MessageBox is capable off.

Getting File Names

Suppose you want to write a C++ program that performs line-numbering. You need some way of getting the name of the file you want to list. You could prompt the user using the command line, use command line parameters, or you could use something like this:

Using the Windows API to pop up a standard file selection dialog from a program is pretty easy. Before doing it, let’s have a look at the pure command line version of the program:

There is nothing very exceptional here. I suggest you compile and run this program so you understand what it does, and then we’ll look at replacing the GetFileName function with one that pops up the file selection dialog.

The file selection dialog is one of a group of dialogs which were added to Windows quite late on in its history. Before than, all common tasks, like file selection, had to be completely coded by each application. This was a vast waste of time and effort, and everyone gave a sigh of relief when the Common Dialogs were introduced.

Windows.h In Dev C 2b 2b 1

The open-for-read dialog is produced by the Windows API function GetOpenFileName and the associated OPENFILENAME struct. This is an immensely complicated structure that is fully described in the MSDN documentation. If you check out this documentation, you will see that things have got even more complicated, as (since Vista) MS would like you to use some COM interface to get file names, but I’m sticking with the older API.

The OPENFILENAME struct has a shed-load of fields, but luckily most of them can profitably be set to zero and ignored. The only ones you absolutely have to worry about are the input buffer and buffer size and the size of the struct itself. We’ll also set the caption field to be the prompt. The replacement for the GetFileName function in the code above then looks like this:

Here the buffer size is set to something suitably large, and used to size the buffer, which is zero initialised. An instance of the OPENFILENAME struct is then created and also zero-initialised. Then the fields you are interested in get set up – the buffer, the buffer size and the caption (title) string. Then the API function GetOpenFileName is called to display the dialog with the address of the struct you have just set up as its parameter. When the function returns, any selected file name will be in the buffer, which is then returned from the function.

If you now compile this new program (let’s say you called it ex5.cpp) you may be surprised to get this error message:

This is a message from the GCC linker saying that it knows nothing about the function GetOpenFileName. This happens because (as I mentioned earlier) the common dialogs were added late to Windows and are not in the libraries that most compilers search by default. In fact, the import library for common dialogs under GCC is called libcomdlg32.a and you have to tell GCC to use it like this:

Windows.h

The file open dialog has many more features. For example, you can tell it which directory to start in, which kind of files to list and many other things. These are all described in the MSDN documentation, and using them is left as an exercise for you! There are also a number of other common dialogs which support things like font and colour selection, and which work in a similar manner to GetOpenFileName.

Using The Microsoft C++ Compiler

To use the Microsoft command line compiler, you will have to start up a command prompt using the VS20xx Prompt shortcut that should get installed along with Visual C++. This will set up all the required environment variables for you. Then to compile the message box programs, use a command line like this;

This will generate an executable called ex1.exe. Note that you have to specify the USER32 library, which contains the MessageBox function, which GCC will link with by default.

To build the file open dialog code, you need to specify the common dialogs library:

Conclusion

This first tutorial has looked at the built-in dialogs that Windows provides for your applications. In the next tutorial I will look at what is entailed in creating a custom dialog.

windows.h is a Windows-specific header file for the C and C++ programming languages which contains declarations for all of the functions in the Windows API, all the common macros used by Windows programmers, and all the data types used by the various functions and subsystems. It defines a very large number of Windows specific functions that can be used in C. The Win32 API can be added to a C programming project by including the <windows.h> header file and linking to the appropriate libraries. To use functions in xxxx.dll, the program must be linked to xxxx.lib (or libxxxx.dll.a in MinGW). Some headers are not associated with a .dll but with a static library (e.g. scrnsave.h needs scrnsave.lib).

Child header files[edit]

There are a number of child header files that are automatically included with windows.h. Many of these files cannot simply be included by themselves (they are not self-contained), because of dependencies.

windows.h may include any of the following header files:

  • excpt.h – Exception handling
  • stdarg.h – variable-argument functions (standard C header)
  • windef.h – various macros and types
  • winnt.h – various macros and types (for Windows NT)
  • basetsd.h – various types
  • guiddef.h – the GUID type
  • ctype.h – character classification (standard C header)
  • string.h – strings and buffers (standard C header)
  • winbase.h – kernel32.dll: kernel services; advapi32.dll:kernel services(e.g. CreateProcessAsUser function), access control(e.g. AdjustTokenGroups function).
  • winerror.h – Windows error codes
  • wingdi.h – GDI (Graphics Device Interface)
  • winuser.h – user32.dll: user services
  • winnls.h – NLS (Native Language Support)
  • wincon.h – console services
  • winver.h – version information
  • winreg.h – Windows registry
  • winnetwk.h – WNet (Windows Networking)
  • winsvc.h – Windows services and the SCM (Service Control Manager)
  • imm.h – IME (Input Method Editor)

Extra includes[edit]

Windows.h In Dev C 2b 2b 4

  • cderr.h – CommDlgExtendedError function error codes
  • commdlg.h – Common Dialog Boxes
  • dde.h – DDE (Dynamic Data Exchange)
  • ddeml.h – DDE Management Library
  • dlgs.h – various constants for Common Dialog Boxes
  • lzexpand.h – LZ (Lempel-Ziv) compression/decompression
  • mmsystem.h – Windows Multimedia
  • nb30.h – NetBIOS
  • rpc.h – RPC (Remote procedure call)
  • shellapi.h – Windows Shell API
  • wincrypt.h – Cryptographic API
  • winperf.h – Performance monitoring
  • winresrc.h – used in resources
  • winsock.h – Winsock (Windows Sockets), version 1.1
  • winspool.h – Print Spooler
  • winbgim.h – Standard graphics library

OLE and COM[edit]

  • ole2.h – OLE (Object Linking and Embedding)
  • objbase.h – COM (Component Object Model)
  • oleauto.h – OLE Automation
  • olectlid.h – various GUID definitions

Windows.h In Dev C 2b 2b 3

Macros[edit]

Several macros affect the behavior of windows.h.

  • UNICODE – when defined, this causes TCHAR to be a synonym of WCHAR instead of CHAR, and all type-generic API functions and messages that work with text will be defined to the -W versions instead of the -A versions. (It is similar to the windows C runtime's _UNICODE macro.)
  • RC_INVOKED – defined when the resource compiler (RC.EXE) is in use instead of a C compiler.
  • WINVER – used to enable features only available in newer operating systems. Define it to 0x0501 for Windows XP, and 0x0600 for Windows Vista.
  • WIN32_LEAN_AND_MEAN – used to reduce the size of the header files and speed up compilation. Excludes things like cryptography, DDE, RPC, the Windows Shell and Winsock.

See also[edit]

Wikibooks has a book on the topic of: Windows Programming
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Windows.h&oldid=979632131'

Coments are closed