Here's a sample Win32 program written in C, compatible with Visual C++ 6.0 and adhering to C89 standards. It creates a window with a button that displays a "Hello World!" message box when clicked, styled to have a retro Windows 9x look:
#include <windows.h>
#define IDC_BUTTON 1001
/* Function prototypes */
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static void CreateControls(HWND hwnd);
/* WinMain - Entry point */
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc;
HWND hwnd;
MSG msg;
/* Initialize window class */
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); /* Win9x gray background */
wc.lpszMenuName = NULL;
wc.lpszClassName = "RetroWin9xApp";
if (!RegisterClass(&wc)) {
return FALSE;
}
/* Create window with Win9x style */
hwnd = CreateWindow(
"RetroWin9xApp",
"Retro Win9x Demo",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL,
NULL,
hInstance,
NULL
);
if (!hwnd) {
return FALSE;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
/* Message loop */
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
/* Window procedure */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
CreateControls(hwnd);
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDC_BUTTON) {
MessageBox(hwnd,
"Hello World!",
"Message",
MB_OK | MB_ICONINFORMATION);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
/* Create button control with Win9x style */
static void CreateControls(HWND hwnd) {
HWND hwndButton;
hwndButton = CreateWindow(
"BUTTON", /* Predefined class */
"Click Me!", /* Button text */
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, /* Styles */
90, /* x position */
60, /* y position */
100, /* Button width */
30, /* Button height */
hwnd, /* Parent window */
(HMENU)IDC_BUTTON, /* Control ID */
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL /* No window creation data */
);
/* Set classic Win9x font */
SendMessage(hwndButton,
WM_SETFONT,
(WPARAM)GetStockObject(ANSI_VAR_FONT),
MAKELPARAM(TRUE, 0));
}
Key Features and Explanations:
-
C89 Compatibility:
- The code avoids any C99 or later features, ensuring compatibility with Visual C++ 6.0, which has limited support for newer standards.
- Variable declarations are at the top of blocks, as required by C89.
-
Win32 API:
- Uses the classic Win32 API for creating windows and controls, avoiding any modern Windows features or APIs introduced after Windows 9x/2000.
- The
CreateWindow function is used to create both the main window and the button control.
-
Retro Win9x Look:
- The background color is set to the classic Windows 9x gray using
(HBRUSH)(COLOR_BTNFACE + 1).
- The button uses the default system font (
ANSI_VAR_FONT) to mimic the look of Windows 9x applications.
- The window style (
WS_OVERLAPPEDWINDOW) and button style (BS_PUSHBUTTON) are consistent with the classic Windows 9x UI.
-
Functionality:
- A single button labeled "Click Me!" is centered in the window.
- Clicking the button displays a message box with "Hello World!" using
MessageBox.
-
Visual C++ 6.0 Compatibility:
- The code uses
GetWindowLong instead of the newer GetWindowLongPtr to ensure compatibility with older compilers.
- No modern C++ or Windows features are used, ensuring it compiles and runs on Visual C++ 6.0.
How to Compile and Run:
-
Create a New Project in Visual C++ 6.0:
- Open Visual C++ 6.0.
- Go to
File > New > Win32 Application.
- Choose a name (e.g., "RetroWin9xDemo") and select "An empty project".
- Add a new C source file (e.g.,
main.c) and paste the code above.
-
Build the Project:
- Go to
Build > Build RetroWin9xDemo.exe.
- Fix any errors (there shouldn't be any if the code is copied correctly).
-
Run the Program:
- Press
F5 to run the program in debug mode, or run the generated .exe file directly.
- You should see a window with a retro Win9x look, containing a button. Clicking the button will display a "Hello World!" message box.
Notes:
- This program will run on modern Windows systems, but it will retain the retro Windows 9x appearance due to the styling choices.
- If you want to enhance the retro feel further, you could add a classic Windows 9x-style icon or use a bitmap for the button, but those would require additional resources and code.
This code provides a good starting point for building simple Win32 applications with a nostalgic look while maintaining compatibility with older tools and standards.