Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



C Programming Language (2nd Edition)    (ISBN: 0131103628)


 

 List Price: $40.00
 Our Price: $40.00
 Used Price: $24.95

 Release Date: 22 March, 1988
 Manufacturer: Prentice Hall PTR (Paperback)
 Sales Rank: 2,265

 Author: Brian W. Kernighan, Dennis Ritchie, Dennis M. Ritchie









More Info

 Create Basic Window 2003-07-03 16:53:46
 
Category: source:c:windows
Description: A basic example on creating a window using the windows API.
Platform: win
Author: detour
Viewed: 7314
Rating: 4.4/5 (49 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
win32api.c
/* win32api.c by detour@metalshell.com
 *
 * A basic example on creating a window using
 * the windows API.
 *
 * http://www.metalshell.com/
 *
 */

#include <windows.h>

/* The name of your window class, can be used multiple
   times after registered. */
const char *ClassName = "MyWindow";

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, 
			WPARAM wParam, LPARAM lParam);

// When using the windows API you use WinMain instead of main

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, 
                   LPSTR lpCmdLine, int nShowCmd) {

	// The window class struct.

	WNDCLASSEX wc;
	// Handle to our window.

	HWND hWnd;
	// Will hold messages sent to our window.

	MSG msg;

	// The size of the structure

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
	wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
	wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = ClassName;
	wc.lpszMenuName = NULL;
	wc.style = 0;

	// Register the class to be used with CreateWindow.

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Could not register window.", "Error",
			MB_ICONEXCLAMATION | MB_OK);
		return -1;
	}

	// Create the window using the "MyWindow" class.

	hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, ClassName, "Basic Window", 
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 200, 150,
		NULL, NULL, hInstance, NULL);

	// If the window was not created show error and exit.

	if(hWnd == NULL) {
		MessageBox(NULL, "Could not create window.", "Error",
			MB_ICONEXCLAMATION | MB_OK);
		return -1;
	}

	// Set the windows show state, to show it.

	ShowWindow(hWnd, nShowCmd);
	// Draw the window.

	UpdateWindow(hWnd);

	// Retrieve messages from the message queue.

	while(GetMessage(&msg, NULL, 0, 0) > 0) {
		// Convert virtual key messages into a char

		TranslateMessage(&msg);
		// Send the message to our WndProc function

		DispatchMessage(&msg);
	}

	return msg.wParam;
}

// This will handle the messages.

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, 
                         WPARAM wParam, LPARAM lParam) {

	switch(uMsg) {
		// Recieved when the user hits the X box or alt-F4

		case WM_CLOSE:
			// Tell the program to end, and dispath WM_DESTROY

			DestroyWindow(hWnd);
			break;
		case WM_DESTROY:
			// End the program.

			PostQuitMessage(0);
			break;
		default:
			// DefWindowProc processes anything we don't.

			return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}

	return 0;
}
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?