Tuesday, September 14, 2010

Fibonacci & Factorial Windows Calculator

So, an idea came to me as I was sitting in DSA class thinking about the introduction to windows programming I got during the first week of GAM666. So I thought it might be an interesting mini-project if I were to combine the two ideas being discussed and create a calculator in visual studio that would use different algorithms from DSA and display them in a more visually appealing fashion.

To start off I simply drafted a quick resource dialog box in visual studio so I could figure out what I was working with and how it should look:



Once the easy part was out of the way, I decided to take steps at a time first getting buttons to properly update the label field, which is where I ran into my first issue. I was able to set text into the edit control easily enough, it was appending and maintaining the number that was causing issues, until I found this support article, that helped me immensely. The following code is how I solved this issue:

            index = GetWindowTextLength(editBox);
            if (index != 0)
            {
                SendMessage(editBox, EM_SETSEL, (WPARAM)index, (LPARAM)index);
                SendMessage(editBox, EM_REPLACESEL, 0, (LPARAM)((LPSTR) L"0"));
            }
            else
                SetDlgItemInt(hwnd, ID_NUMFLD, 0, TRUE);

Once the number is put in and the user clicks on either the fibonacci or the factorial button, I use GetDlgItemInt to retrieve the UINT from the edit control.

No comments:

Post a Comment