Skip to main content

Posts

Showing posts from November, 2008

Windows Socket Faq's

1. What is Windows Sockets? Answer: The Windows Sockets specification defines a network programming interface for Microsoft Windows which is based on the "socket" paradigm popularized in the Berkeley Software Distribution (BSD) from the University of California at Berkeley. It encompasses both familiar Berkeley socket style routines and a set of Windows-specific extensions designed to allow the programmer to take advantage of the message-driven nature of Windows. The Windows Sockets Specification is intended to provide a single API to which application developers can program and multiple network software vendors can conform. Furthermore, in the context of a particular version of Microsoft Windows, it defines a binary interface (ABI) such that an application written to the Windows Sockets API can work with a conformant protocol implementation from any network software vendor. This specification thus defines the library calls and associated semantics to which an application

MFC - Microsoft Foundation Classes, GUI Development

Are you ready to start programming? No you are not. You don't want me to teach you a stupid 'hello world' application, do you? If you want to make the most of Visual C++ you have to use Microsoft Foundation Classes (MFC). These classes are great because they wrap all of those handles we talked about in the first lesson with easy to use classes. The most important to you right now is the class CWnd . This wraps the functions which needed a window handle ( HWND ). Remember that PostMessage function I mentioned? PostMessage(your_HWND, WM_PAINT, 0,0); Well now we can take our windows class and call it's member function. MyCWnd.PostMessage(WM_PAINT, 0,0); This does the same thing, but now we don't have to keep track of the window handles. But don't be fooled, they are still there. We can still use them too. They are now just member variables of the class. The handle to the window a CWnd is associated with in the member variable m_hWnd . We can

SDI and MDI Applications, Doc/View Architecture

We are getting to some advanced stuff now. In this lesson I am not going to go in depth at all. I will just give you a flavor of the structure of a SDI (single document interface) and a MDI (multiple document interface) application. In the last lesson we will build a SDI application and you can see the nitty gritty there. The SDI application is typically used when you intend to work with only one data set at a time. For instance, the program notepad.exe is a SDI application. Netscape is also an SDI application. At any one time, there is only one document open at a time. Word for Windows and the VC++ developer studio are MDI applications. In these you can have several documents opened at once. This is particularly useful when you want to cut and paste between documents. Another use for MDI applications is to have one document, but several different views open that view the data differently. A graphing application comes to mind where in one window you have a spreadsheet-like data list,

Dialog Based Applications

We won't build a dialog application just yet, but I will tell you enough here so that you get the picture of what's going on in dialog applications. Dialog apps are the simplest apps in my opinion. In the IDE, go to File, New, Projects, MFC AppWizard(exe), and type in a project name. Hit next. Select Dialog Application as the type of application and then hit finish. Next go to the File View. You will see the source files created automagically. You should be able to compile and run the application as it is. What is going on in all these files? Everything boils down to the CWinApp derived class and the CDialog derived class (which is derived from CWnd ). Look in the source file named after your project. You should see a the InitInstance() function there. Inside of that function you can see that a dialog class is constructed, it is set as the 'main window' of the application, and it is displayed with the DoModal() function. Once you exit your dialog app, the DoModal(

The workspace, the VC++ IDE, Visual Studio Editor

Windows programming is tricky stuff. Don't let anyone fool you. But microsoft has blessed us with their IDE called Developer's Studio. It will handle all the compiling and linking, provides help, fills in tons of code for you, and gives you that 'visual' designing environment to make cute little dialogs. There a few things you must learn to use to get anywhere. First and most importantly, USE THE ONLINE HELP. There are so many windows functions, you will find yourself in the help 50% of the time or more for the first few months. If you have visual c 6.0 you need the MSDN for help. Get it. If you have 4.2 or 5.0 you can use the built in help. Next, the single most important key combination is Ctrl-W. This brings up what is called the 'Class Wizard'. This guy will insert a bunch of code in your project for you. It handles all the code for connecting functions up to the messages windows posts. You will also find yourself in the resource view a bit. It is used to

Sample VC++ Project - Making a Data Viewer

The moment you have been waiting for, we finally will make a useful application. If you have just skipped the last 6 lessons, then you will probably be able to follow along, but you may not really understand what you are doing. (But since you are the type of person that skips ahead, you are probably used to this.) I have decided to make this example a data viewing application that takes a text file of data, reads it in, and then displays it. If that isn't enough, we are then going to use the timer to animate the data. Let's first assume that we are doing an experiment tracking the random motion of a drunken bug on a table. Every second we measure its distance from two adjacent sides of the table. These are what we will call the bug's x,y coordinates. Our data file looks like this: 390.789 362.245 386.032 366.429 386.559 369.289 385.557 370.483 384.841 372.370 385.785 371.975 389.348 371.005 377.266

C++ Essentials

If you want to use Microsoft Visual C++, it helps a ton if you really know C++. Everything is about classes. If you are used to plain C, you won't really see the big deal with classes until you use them for a while. Let's review what you need to know about classes to get started with VC++. A class is a structure for the most part. Let's work with an example instead of me just telling you rules. Let's make a class to represent a line. In the .h file you would define the class as follows: class CLine { int m_nX1; int m_nY1; int m_nX2; int m_nY2; public: // constructors CLine(); CLine(int x1, int y1, int x2, int y2); // destructor ~CLine(); // set the line data void SetPoints(int x1, int y1, int x2, int y2); // draw the line void Draw(); } A quick word about naming conventions. Class names usually start with 'C' and the member variables usually are prefixed b

Behind the scenes, Handles and Messages

Though you think you want to dive right into the code, you really don't. Windows programming is overwhelming at first. Let's take a quick look at how Windows works. The backbone of all of your programming will be responding to and sending messages. What are messages? Messages are simply a 32bit number designating some event. Example: You move the mouse, a message (defined as WM_MOUSEMOVE) is 'posted' to the active window. You press a key, a message (WM_KEYDOWN) is 'posted' to the active window. You resize the window, a message (WM_SIZE) is 'posted' to the active window. Get the picture? Now where do these messages go? They get queued up and a window eventually takes them out of the queue and reacts to them. For instance when a window gets the WM_MOVE message it changes the coordinates of the window and redraws it on the screen. Let's move on to Handles. Windows is very much object oriented. You have several window objects (like the desktop, the p