echo '' ;

C Library – windows.h

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:

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

C Library – windows.h predefined functions

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: Sleep() function first letter always Uppercase, if you declare in the small case the compiler might generate error “Undefined reference to 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 Sleep() the argument should be unsigned long. here ‘ArunEworld' is a charter pointer.

Refer to the C Examples – Time Delay


Next

C – Basic
C – Operator
C – Decision making, Branching, Looping
C – Functions
C – Storage Class
C – Extern
C – Array
C – Pointer
C – Memory Allocation
C – Structure
C – Union
C – Structure and Union
C – Macros
C – Preprocessor and Macros
C – File Management
C – Coding Standard
C – Compilers
C – Compiling Process
C File Management
C Library
C Example

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading