Notes On Using Visual C++ |
| Copyright © 2008-2009 by Zack Smith All rights reserved.
Is there a free version of Visual C++?Yes, there is, and in fact there is a free version of the other main Microsoft languages. They are all packaged together in as Visual Studio Express, which you can download from Microsoft's website here.Note that Visual C++ Express does not include support for Microsoft Foundation Classes (MFC), but it does support what has superceded it i.e. .NET. Is there a free version of Visual C++ for Windows CE and Windows Mobile?Yes, it's called Embedded Visual C++ 4.0. It still exists and you can download it for free from Microsoft's eVC download page.A free registration code has been made available for anyone who wants to use it at Tech Republic. Or, heck, just type this in, it's the same as the code on that page: TRT7H-KD36T-FRH8D-6QH8P-VFJHQ. Is there a free compiler for Win32 that is not from Microsoft?Yes, it's called MinGW ("minimal GNU on Windows"). I am actively using it. You need to get MSYS with it.Is there a free compiler for WinCE/WinMo that is not from Microsoft?It appears MinGW has been updated to cross-compile to the ARM. I haven't looked at this yet.Can I create a typical Windows GUI using Visual C++ Express?Yes and no. It cannot be based on MFC, because Express does not support MFC. So it must be either .NET based or Win32-based.However I have found that .NET's Form editor crashes Visual Studio pretty regularly. Therefore I use plain Win32. I've developed a simple widget set that sits on top of Win32 that I use. I have found that C# with .NET is a more natural pairing and the form editor doesn't seem to crash as much. Note that in .NET, GUI objects are accessed via handles which use the caret notation, e.g. PictureBox ^pb, rather than pointers. And they must be instantiated using gcnew rather than the typical C++ new. How to static-link libraries in Visual studio?To link a library as static, right click on the project and go into Properties, click on Configuration Properties, General, and set Configuration Type to Static Library (.lib).How to static-link the executable?Doing a static link is a good idea since it protects against missing DLLs and it avoids the hassles of manifest files. To link an executable statically, you will need to ensure that all components of it are also statically linked, including libraries. And, for all projects in your solution, you need to go into Project Properties, Configuration Properties, C/C++, Code Generation, and set Runtime Library to /MT. For good measure, go into Linker, Command Line, and add the command line option /NODEFAULTLIB:libraryname.Setting the include and link pathsSelect the Tools pulldown menu, then Options, then Projects and Solutions, then VC++ Directories, then under "Show Directories For:" select either Library files or Include files.How can I find out the number of processors programmatically?Like so:int num_processors; SYSTEM_INFO sys_info; GetSystemInfo(&sys_info); num_processors = sys_info.dwNumberOfProcessors; How much memory can one process have?In 32-bit Windows, the maximum is 2 gigabytes per task.
You can expand it to 3 gigabytes by altering the file
How to create a simple dialog box in my program?In standard non-MFC Windows it is:MessageBoxA (NULL, "message", "window title", MB_OK); Or: MessageBox (NULL, L"message", L"window title", MB_OK); In .NET it's easy to create a basic message box: MessageBox::Show ( "my text" ); How to create a simple file-open dialog box?Like so:
OPENFILENAME ofn;
short szFile[260];
ZeroMemory (&ofn, sizeof(ofn));
szFile[0]=0;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
ofn.hwndOwner = NULL; // hwnd;
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = (LPWSTR) szFile;
ofn.lpstrFileTitle = NULL;
ofn.lpstrFilter = L"All\0*.*\0Text\0*.txt\0";
ofn.lpstrInitialDir = NULL;
ofn.lpstrInitialDir = L"";
ofn.nFilterIndex = 1;
ofn.nMaxFile = sizeof(szFile);
ofn.nMaxFileTitle = 0;
if (TRUE == GetOpenFileName(&ofn)) {
// Use szFile here.
}
How to create a simple file-save dialog box?Like so:
OPENFILENAME ofn;
short szFile[260];
ZeroMemory (&ofn, sizeof(ofn));
szFile[0]=0;
ofn.Flags = OFN_OVERWRITEPROMPT;
ofn.hwndOwner = NULL; // hwnd;
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = (LPWSTR) szFile;
ofn.lpstrFileTitle = NULL;
ofn.lpstrFilter = L"All\0*.*\0Text\0*.txt\0";
ofn.lpstrInitialDir = NULL;
ofn.lpstrInitialDir = L"";
ofn.nFilterIndex = 1;
ofn.nMaxFile = sizeof(szFile);
ofn.nMaxFileTitle = 0;
if (TRUE == GetSaveFileName(&ofn)) {
// Use szFile here.
}
How to create a color-wheel popup to let users choose a color?Thus:
unsigned long chooser ()
{
CHOOSECOLOR cc;
static COLORREF custom[16];
memset (custom, 0, sizeof(COLORREF)*16);
custom[0] = 0xff0000;
memset (&cc, 0, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.lpCustColors = (LPDWORD) custom;
cc.rgbResult = current_color;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor (&cc))
return cc.rgbResult;
else
return 0xff000000;
}
What is a manifest?It's an XML file that describes what versions of DLLs that application uses, including C runtime libraries. Like it or not, you have to include it with your program, either inside or separate. The default behavior of Visual C++ is to include it inside the .exe file.Note, when you make a Release to give to the general public, you will need to include the version of the C runtime libraries that are provided with Visual C++, e.g. Microsoft.VC90.CRT. There are four files total and they are either these or something similar that came with your VC++.
If you fail to provide the needed manifest file and DLLs, your program will not start but will produce an error:
The application configuration is incorrect. Reinstalling this application may fix this problem.
Upon inspection of the Event Log (run eventvwr.exe)
you will find a SxS (meaning side-by-side) event error.
How do I receive mouse motion that is outside of a window?After a mouse-down message (WM_MOUSEDOWN) make a call to the functionSetCapture(hwnd) providing your window's handle.
To release this just call
Can any thread draw to a window?No. Only the thread that created a window can draw to it.How do I get the handle of a dialog window control?Like so:HWND handle; handle = GetDlgItem (dialog_handle, IDC_EDIT );... where IDC_EDIT is just an example of a #define used in your .rc file, and defined in your Resources.h file,
to identify some part of a dialog.
How do I create a timer event?Some applications require timers e.g. for scrolling selectionn of text in a word processor.In pre-MFC Windows you would use the SetTimer() API call, and look for a WM_TIMER event. In .NET, you can use the System::Windows::Forms::Timer class or System::Timers. What is ValidateRect() for?It clears part or all of the list of rectangles that need to be painted. Use it very sparingly.
Note!
If you accidentally put a call to
Can I use just any HDC for drawing?No. You must use the HDC you produced for of the window (HWND) that received the message that you're handling.What is the HWND parameter in MessageBoxA() for?It's important, at least for Win32 programs. If you pass in NULL rather than your application's window handle (hwnd) you will find that after the dialog box goes away, you are no longer able to draw to your window.My main window has a child window. For some reason when I call MessageBox the dialog does not appear until I press the Alt key. Why?The dialog is effectively obscured by the child window, which is above it. It's a tedious feature of Win32. One way around it is to create a hand-made dialog in a new window. Another way is to code the message loop for a custom dialog and include ShowWindow in the InitDialog function. More info.How to I make a beep at a specific frequency and duration?Use this function from Winbase.h. Beware, it's loud.unsigned int freq = 20000; // Hz unsigned int dur = 2000; // 2000 ms Beep (freq, dur); How to I set and reset the clipping region?To set it:IntersectClipRect (hdc, x0, y0, x1, y1);To reset it: SetClipRgn (hdc, NULL); What is Invalid Address Specified to RtfFreeHeap all about?You're probably trying to free a constant string.Links
|
|