1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
/*
Main.c
To run this code you should tell Visual Studio to set up a GUI Project. In the
options presented to you through the various visual interfaces of Visual Studio
you should tell it to set up an 'Empty Project'. If there are options to specify
C or C++ compilation, you can choose C compilation if you like. An 'Empty Project'
is one where Visual Studio won't auto-generate any wizard code for you. After
Visual Studio is done creating your 'Empty Project', you'll need to tell it to
create a new blank *.c file for you and to add it to the project. The menu
commands for doing that should be under the File menu somewhere.
After doing that paste this code into the new blank Main.c file and attempt to
compile. The only possibility of failure that I can see with Visual Studio 2003
is that I don't know when Microsoft set up its Visual Studio Environment to
default to wide character (UNICODE) builds. It wasn't so set in Visual Studio 6
circa 1998 or so, but it was so set around 2008 with Visual Studio 2008 (VC9). I
don't presently have a 2003 setup to test this on.
If you do receive any errors about character strings that's what's happening. In
that case you need to go into 'Project Settings' and specify the 'multi-byte
character set or leave the character set as 'Not Set'. I purposly used chars in
this code instead of the wchar_ts I usually use because I figurred you would be
more at home with them. I'd guess most folks compile with UNICODE nowadays, as do
I.
*/
#include <windows.h>
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
if(msg==WM_DESTROY) // This is the Window Procedure. The concept of the
{ // Window Procedure is the most important concept to
PostQuitMessage(0); // grasp in C/C++ WinApi coding. You never call this
return 0; // function with your code; rather, Windows calls it
} // to inform code here of events occurring. The events
// are reported here as messages.
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
char szClassName[]="Form1"; // This will be both the name of the app and the name of
WNDCLASSEX wc; // the main window's 'Window Class'. The concept of the
MSG messages; // Window Class and its associated Window Procedure are
HWND hWnd; // the most important concepts in Windows Programming.
memset(&wc,0,sizeof(wc)); // zero out WNDCLASSEX wc
wc.lpszClassName = szClassName; // Feed "Form1" into WNDCLASSEX::lpszClassName
wc.lpfnWndProc = fnWndProc; // Feed pointer ( function address ) to Window Procedure To wc
wc.cbSize = sizeof(WNDCLASSEX); // Set Size
wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW; // Set Background HBRUSH
wc.hInstance = hInstance; // Set HANDLE To Instance (its a virtual process memory thing)
RegisterClassEx(&wc); // Let Operating System know about "Form1" Class
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,175,320,200,HWND_DESKTOP,0,hInstance,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|