Windows programming
Now, be prepared - this learning curve will be steep -
probably the hardest learning curve ever because you are dealing
with the Microsoft WIN32 API. An API is a set of calls you can
use to interact with someone elses program. In this case we use the
WIN32 API to interact with Windows.
You are going to also have to make excellent use of the help (MSDN)
supplied with Visual Studio. It will become your best friend.
so, are you ready? It's time to enter the world of Windows programming
<insert dramatic door opening sound>
The first thing we are going to learn is DialogBoxes. They are simple
dialog apps that can get you up and running quickly. We won't have to
deal with some of the more painful side of windows when we use DialogBoxes,
and you'll be able to write some pretty cool stuff. Then later we'll move
on to more advanced items. But if you get a good understanding of DialogBoxes,
the rest should come easier. (it will all still be painful!)
Create a new project, this time we want a Windows Application instead of a
Console Application. You also want to be sure to check the box that says
"empty project" so windows won't add a bunch of crap you don't need.
The first file you add should be "winmain.cpp", this is going to be the
entry point to your program.
We will need the following include files
#include <windows.h> //all the windows stuff
#include <stdio.h> //standard c in and out stuff
next up is the entry point for your program When doing a console app it is either void main( void ); or void
main( int argc, char *pArgv[] );. For windows it's a bit more complex:
int WINAPI WinMain(
//the instance of your app (each app has
//a unique instance assigned by windows)
HINSTANCE hInstance,
//no longer used, is here only for
//backwards compatibility
HINSTANCE hPrevInstance,
//the arguments coming in on the command line
//(like if a file was dragged over the exe)
LPSTR lpCmdLine,
//not used
int iCmdShow
)
{
//program code goes here
};
The first thing we are going to learn is the message box function.
This will help a lot in your debugging, because there is no console
window for printf to output to. Below this function I will describe what each variable does:
int MessageBox(
HWND hWnd,
LPCTSTR lpText,
LPCTSTR lpCaption,
UINT uType
);
HWND hWnd: each window has a handle, this is the handle of the window that the
message box should belong to.
LPCTSTR lpText: don't be fooled by this scary thing
(LPCTSTR), all it is, is a pointer to a char array (char *).
This is the text that your message box will display
LPCTSTR lpCaption: this is the caption for your message box
UINT uType: this is the type of message box, it can be a combination of many items. If you go to your help,
and type MessageBox in the index and then hit enter. It will bring you up some
choices, choose the one that says MessageBox function() Windows Management.
Look over this file and it will show you combinations you can use for the type.
A common combination is MB_OK | MB_ICONQUESTION. This will put an ok button
on the message box, with an icon showing a question mark. Notice I put multiple
combinations together with the pipe symbol ( | ). Lets say you wanted an
exclamation point and an ok or cancel button, you would use MB_OKCANCEL | MB_ICONEXCLAMATION.
If you found the help file, you can play around with the types.
Since we haven't created a window yet, we will pass in NULL for the handle to the window.
NULL is defined (in windows.h) has 0. In windows.h they put #define NULL ( 0 ). Here is a sample message box
MessageBox( NULL, "Hello World!", "my first
message box", MB_OK | MB_ICONASTERISK );
Now remember, MessageBox is just a function call. It's nothing scary, and you've made plenty of function calls
before (fread, fwrite, etc.) So go ahead and play around with the message box
__________________________________________________________
#include <windows.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
MessageBox( NULL, "Help", "Message box",
MB_OK|MB_ICONEXCLAMATION );
return 0;
}
|