titbits from the world less travelled

Archive for the 'windows' Category

Adding a horizontal scrollbar to CListBox in windows

Description of Listbox:

Provides the functionality of a Windows list box.
Copy

class CListBox : public CWnd

Remarks

A list box displays a list of items, such as filenames, that the user can view and select.

In a single-selection list box, the user can select only one item. In a multiple-selection list box, a range of items can be selected. When the user selects an item, it is highlighted and the list box sends a notification message to the parent window.

You can create a list box either from a dialog template or directly in your code. To create it directly, construct the CListBox object, then call the Create member function to create the Windows list-box control and attach it to the CListBox object. To use a list box in a dialog template, declare a list-box variable in your dialog box class, then use DDX_Control in your dialog box class’s DoDataExchange function to connect the member variable to the control. (this is done for you automatically when you add a control variable to your dialog box class.)

Its possible to go to the resource file and add your settings directly. See below:

LISTBOX LBS_SORT | LBS_MULTIPLESEL | LBS_NOINTEGRALHEIGHT | LBS_DISABLENOSCROLL | WS_VSCROLL | WS_HSCROLL | WS_TABSTOP

But sometimes, this wont do the work. Even though you get the vertical scrollbar automatically, somehow the horizontal scrollbar refuses to show up. We need to use TEXTMETRIC and CDC to do that. We read the length of the input and adjust the width according to that:

TEXTMETRIC vartm; //contains information about the font

The CDC object provides member functions for working with a device context, such as a display or printer, as well as members for working with a display context associated with the client area of a window.

Do all drawing through the member functions of a CDC object. The class provides member functions for device-context operations, working with drawing tools, type-safe graphics device interface (GDI) object selection, and working with colors and palettes. It also provides member functions for getting and setting drawing attributes, mapping, working with the viewport, working with the window extent, converting coordinates, working with regions, clipping, drawing lines, and drawing simple shapes, ellipses, and polygons. Member functions are also provided for drawing text, working with fonts, using printer escapes, scrolling, and playing metafiles.

//varListbox is the variable name of CListBox
//you would need to call this inside the InitDialog of listbox, so that these changes are reflected in listbox

CDC* varDC = varListbox.GetDC();

if( varDC) //check for NULL
{
varDC ->GetTextMetrics(&varTM);
size_t sizeOfHz = varTM.tmAveCharWidth * 500; //you can replace 500 with any number you want
m_varListbox.SetHorizontalExtent(sizeOfHz );
//release CDC
}

posted by admin in c++, windows and have No Comments

Retrive the class names of all windows[MSDN]

Below search method will be useful when you need to find a window with no caption.

FindWindow will not work correctly in such a instance.

You can make use of the below method to do that.

//get the handle to the top window

HWND h = ::GetTopWindow(0);
while ( h)
{
TCHAR    buf[100];
::GetClassName( h, (LPTSTR)&buf, 100 );
if ( _tcscmp( buf, _T(“Youclassnametosearch”) ) == 0 )
return true;

//get the handle to the next window

h = ::GetNextWindow( h , GW_HWNDNEXT);

}
return false;

posted by admin in c++, windows and have No Comments

SubVersion error: 1053: not able to create a service

When trying to run a subversion or svn service, we get this error stating the service is not running, this is because of the way you create the service:

Below are the right and wrong way to create it:

WRONG WAY:

F:\Program Files\Subversion\bin>sc create svnserver binpath= “F:\Program Files\S
ubversion\bin\svnserve.exe –service -r F:\Dev Files\Repository” displayname= “S
ubversion” depend= Tcpip start= auto

RIGHT WAY:

F:\Program Files\Subversion\bin>sc create svnserver binpath= “\”F:\Program Files
\Subversion\bin\svnserve.exe\” –service -r \”F:\Dev Files\Repository\”" display
name= “Subversion” depend= Tcpip start= auto
[SC] CreateService SUCCESS

Notice the apostrophe’s when a file name is declared.

posted by admin in windows and have No Comments

Windows developer tools you need to have

Some tools which I really find helpful are:

a. Multimon – Support for multiple monitors

b. Process Monitor

c. Process Explorer

d. Desktops – Desktops allows you to organize your applications on up to four virtual desktops.

e. OleView – For COM purposes

f. Spy++ – For windows GUI handling

g. Resource hunter – Reading resource files. It will be helpful when your working on multiple languages.

posted by admin in c++, windows and have No Comments

Using CListBox in windows especially in pop up windows

I had an opportunity to use CListbox. The scenario was that I had to pre populate the list box and display it in a pop up window.

Its very to use listbox when using it in a parent window but when you want to display it in a standalone pop up window, you would get an error.

This is because the data is present only after the window comes into picture completely and is gone when the window disappears. In order to rectify this you would need to over rider the Initdialog method as below:

BOOL OnInitDialog()
{
//check if the parent window dialog is also initialized
if(CDialog::OnInitDialog())
{
//enter the values into the listbox here
}
}

By the above code you can enter into a listbox before its displayed.

Declare the below methods in the header file for getting the list selection changes:
afx_msg void OnLbnSelchangeList1();
afx_msg void OnLbnDblclkList1();

Add them to the message map in the class as below:

BEGIN_MESSAGE_MAP(YourClassname, CWnd)
ON_LBN_SELCHANGE(IDC_LIST1, &YourClassname::OnLbnSelchangeList1)
ON_LBN_DBLCLK(IDC_LIST1, &YourClassname::OnLbnDblclkList1)
END_MESSAGE_MAP()

Over ride them with the functionalities you need as below:
void YourClassname::OnLbnSelchangeList1()
{

}

void YourClassname::OnLbnDblclkList1()
{
// TODO: Add your control notification handler code here
}

Use CArray to get the values:

CArray aryListBoxSel;
aryListBoxSel.SetSize(nCurrentSelected);
m_ListBox1.GetSelItems(nCurrentSelected, aryListBoxSel.GetData());
CString sTitle;
int indexVal = 0;

for(int i=0;i<=aryListBoxSel.GetUpperBound();i++)
{
indexVal = aryListBoxSel.GetAt(i);
m_ListBox1.GetText(indexVal, sTitle);
}
---------------------------------------------------------------------------------------------------
Incase CArray gives a error in vc6 you can do the following:

int Count = m_ListBox1.GetSelCount();
if (Count > 0)
{
int *pSels = new int[Count];
m_ListBox1.GetSelItems(Count,pSels);
for (int i = 0; i < Count;i++)
{
//pSels[i] this is the index of the item that was selected
CString Temp;
m_ListBox1.GetText(pSels[i],Temp);
}
delete [] pSels;
}

posted by admin in c++, msdn, visual studio, windows and have No Comments

error C2065: ‘CArray’ : undeclared identifier in visual studio

You would be getting this error if you tried to use CArray in VC 6. You would need to use some other way to get the values selected in listbox. In my next post I would be showing how to use two different ways to access a CListBox.

posted by admin in c++, msdn, visual studio, windows and have No Comments

Detecting memory leaks in VC++

One of the simplest ways to detect a leak in your code would be to use debugCRT.

Simple usage:
a. Create you project in visual studio
b. run the project in debug mode, then only you will find the memory leak dumps
int main()
{
_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_ALLOC_MEM_DF);
//below call will break where the leak is in the code provided you give the error number as shown in the //dump.
_CrtSetBreakAlloc(108);

// your code here

//Below windows API will dump the leaks
_CrtDumpMemoryLeaks();
return 0;
}

posted by admin in c++, msdn, visual studio, windows and have No Comments

creating a new registry key in windows using VC++

Below is the code to create a registry key in VC++. I used visual studio 2008 for this(VC9)

RegCreateKeyEx is the command to create the key. More documentation can be found online@ msdn.

#include “stdafx.h”
#include “Windows.h”

int _tmain(int argc, _TCHAR* argv[])
{
HKEY hOutKey;
RegCreateKeyEx(HKEY_LOCAL_MACHINE,  L”SOFTWARE\\ABC\\Registration\\addkey”,
0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hOutKey, NULL);

return 0;
}

posted by admin in c++, windows and have No Comments

comparing two systemtime variables using filetime in vc++

lets take a look at how to compare two systemtime variables but using Filetime to do so. We make use of the utility method CompareFiletime to do this.

1) store the systemtime in a variable
2) convert it into filetime. Use this method
SystemTimeToFileTime(&systemtime1, &filetime1)

SystemTimeToFileTime(&systemtime2, &filetime2)
3) Compare the filetimes using the method
CompareFileTime(&filetime1, &filetime2) > 0

4) finally convert the filetime to systemtime using the method
FileTimeToSystemTime(..)

posted by admin in c++, msdn, windows and have No Comments

Reading a 64bit key from a 32bit application

The easiest way to do this would be to use the option KEY_ENUMERATE_SUB_KEYS when you open the registry key. This option would read both the 32bit and the 64bit registry keys.

posted by admin in c++, windows and have No Comments