How To Get The Screen Resolution In Pixels

January 7, 2009

The function GetWindowRect(), defined in WinUser.h, is able to give the current screen resolution. The declaration of GetWindowRect() is


BOOL
WINAPI
GetWindowRect(
    __in HWND hWnd,
    __out LPRECT lpRect);

We can give a handle to the current desktop window to the above function as input. A sample implementation for getting the current screen resolution is given below.

#include “wtypes.h”
#include
using namespace std;

// Get the horizontal and vertical screen sizes in pixel
void GetDesktopResolution(int& horizontal, int& vertical)
{
   RECT desktop;
   // Get a handle to the desktop window
   const HWND hDesktop = GetDesktopWindow();
   // Get the size of screen to the variable desktop
   GetWindowRect(hDesktop, &desktop);
   // The top left corner will have coordinates (0,0)
   // and the bottom right corner will have coordinates
   // (horizontal, vertical)
   horizontal = desktop.right;
   vertical = desktop.bottom;
}

int main()
{       
   int horizontal = 0;
   int vertical = 0;
   GetDesktopResolution(horizontal, vertical);
   cout << horizontal << '\n' << vertical << '\n';    return 0; } [/sourcecode]

Advertisements

How To Get All CD Drives

November 19, 2008

In the last post, I described how to open/close cd tray. Some of you might have raised your eye brows as there is no description how to find the drive letter of cd drive. Here is a sample program which prints all cd drive letters in a system.

#include
#include
#include “wtypes.h”
#include “basetsd.h”
#include “winbase.h”
using namespace std;

int main()

   // Iterate through all the 26 drives possible  
   for (char Drive = ‘a’; Drive <= 'z'; ++Drive)    {       // Format drive like "c:\"       string StrDrive = "";       StrDrive.push_back(Drive);       StrDrive.append(":\\");       // Print the drive letter if it is a cd drive       if (DRIVE_CDROM == GetDriveType(StrDrive.c_str()))       {          cout << Drive << '\n';       }    }    return 0; } [/sourcecode]

Some Background Info

The GetDriveType() function can give the type of a drive. The possible values are given below.

  • DRIVE_UNKNOWN = 0 : The drive type can not be determined.
  • DRIVE_NO_ROOT_DIR = 1 :The root path is invalid; for example, there is no volume is mounted at the path.
  • DRIVE_REMOVABLE = 2 : The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.
  • DRIVE_FIXED = 3 : The drive has fixed media; for example, a hard drive or flash drive.
  • DRIVE_REMOTE = 4 : The drive is a remote (network) drive.
  • DRIVE_CDROM = 5 : The drive is a CD-ROM drive.
  • DRIVE_RAMDISK = 6 : The drive is a RAM disk.

The GetDriveType() function expects an argument of type LPCTSTR, which can specify the root path of the drive with a trailing slash (as in c:\). if the argument is NULL, it will take the root of the current directory.

GetDriveType() will be expanded to GetDriveTypeW (for UNIODE) and GetDriveTypeA (for ANSI). The macro _T() can automatically expand the argument to multibyte character string if unicode is defined.


What is const_iterator

October 20, 2008

A const_iterator is not a const iterator. That is,

const vector<int>::iterator it;

and

vector<int>::const_iterator it;

are different. The first type (const iterator) does not allow any kind of modification of the iterator as well as the contents. That is, both

++it;

and

*it = 40 are illegal.

The second type(const_iterator) allows modification of the iterator, but prevents the contents from getting modified.

So,

++it is legal whereas *it = 40 is illegal.


#include<vector>

using namespace std;

int main()

{  

   vector<int> myVector;

   myVector.push_back(10);

   myVector.push_back(20);

   vector<int>::const_iterator it = myVector.begin();

   ++it;       // Legal 

   *it = 40;   // Illegal

   return 0;

}


How To Make gcc Warn You About Unused Variables

October 9, 2008

Use the option -Wunused with gcc.

gcc MyFile.cpp -Wunused


How To Get The Size Of Memory Allocated In Heap

September 20, 2008

sizeof() function can not give the size of memory locations allocated using new, malloc, calloc and realloc. VC++ provides a macro for the above purpose and it is _msize. See the following code segment.

#include
using namespace std;
int main()
{  
   // Allocate 10 integer locations 
   // = a toatl of 40 in 32 bit platforms
   int* pInt = new int[10];
   size_t Size = _msize(pInt);
   cout << Size << '\n';       return 0; } [/sourcecode] The result will be 40.


How To Sort A Vector With Non Numeric Elements

September 19, 2008

Have you ever faced a problem to sort STL vector with non number elemnts (say structs or other classes)? The problem with sort function is that it should know about the less than (<) operator. So the problem would be solved if you defined a < operator for the element type.

The following example illustrates how to sort a vector with elements of type struct.

#include
#include
#include
 
using namespace std;

// Here is a simple struct
struct MyStruct
{
   int Num;
   // Define the operator <    bool operator <(const MyStruct& Rhs)    {       return (Num < Rhs.Num);          } };   int main() {      vector MyVector;
   // Let the size be 5.
   MyVector.resize(5);
   // Push 5 instances of MyStruct with Num ranging
   // from 5 to 1
   MyStruct TestStruct;
   int i = 0;
   for (i = 0; i < 5; ++i)    {       TestStruct.Num = 5 - i;       MyVector[i] = TestStruct;    }    // Now sort the vector    sort(MyVector.begin(), MyVector.end());    // Try to display Num for each element. It is sorted    for (i = 0; i < 5; ++i)    {       cout << MyVector[i].Num << '\n';    }    return 0; } [/sourcecode] You can use the same technic for any vector with non number elements. The only condition is that you should have a < operator defined for the element data type.


How To Sort An Array

September 18, 2008

The same sort function used to sort STL vector can be used for sorting arrays also. Surprised? Sometimes life is easier than we expect. 

#include
#include
using namespace std;

int main()
{  
   int MyArray[] = {10, 9, 8, 7, 6};
   sort(MyArray, MyArray + 5);
   for (int i = 0; i < 5; ++i)    {       cout << MyArray[i] <<'\n';    }    return 0; } [/sourcecode]