How to Use Win32 GUI in C++

Win32, more commonly known today as the Windows API, is the set of tools provided by Microsoft to develop programs for the Windows operating system. Programs written with Win32 use the Windows graphical user interface (GUI) to display information and receive user input. While learning Win32 programming for C++ can take a long time --- even displaying a window can take up to 70 lines of code --- you can create a simple "Hello World!" message box in a few minutes.

Instructions

    • 1

      Open your C++ integrated development environment (IDE) and create a new .cpp file. Begin your code by including the Win32 API header file. Your first line of code should look like this:

      #include <windows.h>

    • 2

      Create the WinMain entry point. This is the Win32 counterpart to the traditional main() function used in DOS and Linux. WinMain() takes four arguments, including a handle to the program's .exe file in your computer's memory; a null handle to the program's previous instance; the string of user-supplied command line arguments; and an integer that can be used to determine the startup state when creating an application that uses a window. The WinMain() declaration looks like this:

      int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

    • 3

      Write the body of the program so that it will display a message box. The Win32 MessageBox() function also takes four arguments: a handle to the owner window, the message contents, the titlebar text and the type of message box it is. Since this box has no owner window, set the handle to NULL. Write it to display any message you wish and give it an information icon with an "OK" button. The body of the WinMain() function should look like this:

      {

      MessageBox(NULL, "Hello, World!", "Hi.", MB_ICONINFORMATION | MB_OK);

      return 0;

      }

    • 4

      Save your work and compile the program as a Win32 application. The way to do this will vary from compiler to compiler --- consult your documentation if you're unsure. Run your program to see a simple message box containing your text.

Related Searches:

References

Resources

Comments

Related Ads

Featured