The windows.h
header file is a fundamental header file for the Microsoft Windows operating system. It includes declarations for a wide range of functions and data types used in Windows programming. Here’s a basic example demonstrating the usage of windows.h
for creating a simple Windows GUI application:
Contents
C Library – windows.h File
#include <windows.h> // Function prototype for the window procedure LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); int main() { // Get the handle to the instance of this application HINSTANCE hInstance = GetModuleHandle(NULL); // Create the main window HWND hwnd = CreateWindowEx( 0, // Optional window styles L"WindowClass", // Window class L"My First Window", // Window title WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, // Parent window NULL, // Menu hInstance, // Instance handle NULL // Additional application data ); // Display the window ShowWindow(hwnd, SW_SHOWNORMAL); // Enter the message loop MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } // The window procedure LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; }
This above example creates a simple window using the Windows API. The WindowProc
function is the window procedure, which handles messages for the main window. The CreateWindowEx
function creates the main window, and the ShowWindow
function displays it. The program then enters a message loop (while (GetMessage...)
) where it waits for and processes messages until the user closes the window.
Remember that this is just a basic example, and real-world Windows applications will involve more complexities and considerations. Additionally, GUI programming in Windows often involves using additional libraries, such as MFC (Microsoft Foundation Classes) or newer frameworks like WinUI.
Predefined functions of C Library – windows.h
The windows.h
header in Windows programming provides declarations for various functions, data types, and constants used in Windows API. Here are some commonly used predefined functions available in windows.h
:
C Library – windows.h predefined functions List
Function Category | Function | Note |
---|---|---|
Window Management Functions: | CreateWindowEx() | Creates an extended window. |
ShowWindow() | Sets the specified window’s show state. | |
UpdateWindow() | Updates the client area of the specified window. | |
DestroyWindow() | Destroys the specified window. | |
DefWindowProc() | The default window procedure for window messages not processed by your window procedure. | |
Message Handling Functions: | GetMessage() | Retrieves a message from the calling thread’s message queue. |
TranslateMessage() | Translates virtual-key messages into character messages. | |
DispatchMessage() | Dispatches a message to a window procedure. | |
PostQuitMessage() | Posts a quit message to the message queue. | |
Thread Functions: | CreateThread() | Creates a new thread for parallel execution. |
GetCurrentThreadId() | Retrieves the thread identifier of the calling thread. | |
Synchronization Functions: | CreateMutex() | Creates or opens a named or unnamed mutex object. |
CreateEvent() | Creates or opens a named or unnamed event object. | |
WaitForSingleObject() | Waits until the specified object is in the signaled state. | |
ReleaseMutex() | Releases ownership of the specified mutex object. | |
File and File I/O Functions: | CreateFile() | Creates or opens a file or I/O device. |
ReadFile, WriteFile() | Reads from or writes to a file or I/O device. | |
CloseHandle() | Closes an open object handle. | |
Memory Management Functions: | VirtualAlloc() | Reserves or commits a region of memory within the virtual address space of a specified process. |
VirtualFree() | Releases, decommits, or releases and decommits a region of memory. | |
Time Functions: | GetSystemTime() | Retrieves the current system date and time. |
Sleep() | Suspends the execution of the current thread for a specified interval. | |
Miscellaneous Functions: | MessageBox() | Displays a modal dialog box that contains a system icon, a set of buttons, and a brief application-specific message. |
GetLastError() | Retrieves the calling thread’s last-error code value. | |
LoadLibrary, GetProcAddress() | Loads a dynamic-link library (DLL) into the calling process’s address space. |
Note
These are just a few examples, and there are many more functions provided by Windows.h for various purposes. When working with Windows programming, documentation is an essential resource to understand and use these functions effectively. The next section only discussed Sleep() function.
sleep() in C Library – windows.h
if you have the following questions like
- How to use the delay function in C?
- How to use the C program delay function in Windows?
Here is your solution,
If you wanna use the delay feature, you can use the Sleep()
function in the Windows platform, based on the compiler the Sleep()
will call in different library files(Some times Sleep function will be winbase.h
, Sometimes different). Don’t worry about that, if you include the windows.h
the header file, that will be taken care of. why because everything all the sleep function necessary headers are already included in windows.h
the file.
- Rule-1: You should add the header file
#include <windows.h>
- Rule-2:
function first letter always Uppercase, if you declare in the small case the compiler might generate error “Undefined reference to sleep”.Sleep()
- Rule-3:
Sleep()
function argument (Milliseconds) should be unsigned long type. If your call [Example :Sleep("ArunEworld")
]Sleep()
function with char type or other types, the compiler will generate an Error.
Please note that the sleep
function may not be very precise, and the actual delay could be slightly longer due to system-related factors. If you need more precise timing, you might want to explore other methods or libraries for that purpose.
Sleep() Example-1
Code
#include <stdio.h> #include <unistd.h> // for sleep function int main() { printf("ArunEworld: This is before the delay.\n"); // Sleep for 3 seconds sleep(3); printf("ArunEworld: This is after the delay.\n"); return 0; }
Explanation
In this above example, the program will print the first message, then pause for 3 seconds using sleep(3)
, and finally print the second message after the delay.
Remember to include the <unistd.h>
header for the sleep
function to work.
Sleep() Example-2
The below code will be printed ArunEworld website every 1 second
#include <stdio.h> #include <Windows.h> int main() { while(1) { //print the aruneworld website address printf("www.ArunEworld.com.\r\n"); //make a delay every in millisecond Sleep(1000); } return 0; }
Slep() Example-3
#include <stdio.h> #include <Windows.h> int main() { while(1) { //print the aruneworld website address printf("www.ArunEworld.com.\r\n"); //make a delay every in millisecond Sleep("ArunEworld"); } return 0; }
The above code will show the Error like "[Note] expected 'DWORD' but argument is of type 'char *'"
. Why because
the argument should be unsigned long. here ‘Sleep()
ArunEworld'
is a charter pointer.
Refer to the C Examples – Time Delay