titbits from the world less travelled

Archive for May, 2010

Displaying a background image in Android

Goto your res folder as shown in image below add a background image.

After adding a background image, goto the main.xml and add the below text:

<ImageView
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
android:src=”@drawable/background” />

Note: Do not add the extn to the image name, it wont recognize the file and give an error.

Next goto your main class and add the following:

super(context);
// Set the background
this.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.background));

Another way to do this would be:

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.background);

posted by admin in android and have No Comments

lets honor our troops – please check it out

Really good slide show: http://www.cnn.com/SPECIALS/war.casualties/index.html

Tags:
posted by admin in Uncategorized 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

How to create a file in windows using VC++ and HIDE IT

simplest way to do that would be:

typeCharStr fileName = L”C:\\hiddenFile.txt”;
if(CreateFile( fileName.c_str(),
FILE_READ_DATA,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_HIDDEN,
NULL ))
printf(“CreateFile failed (%d)\n”, GetLastError());

The above is written in C++ using windows API.

posted by admin in Uncategorized and have No Comments

SVN: cheatsheets

SVN stands for sub version.

SVN is a source control tool. This tool is used for tracking and maintaining a repository of your code and each and every minute changes made to it through the use of versions. You can use GUIs to access your SVN like tortoiseSVN and so on.
But if you need to use a command line for SVN, you can use collabnet and others. I use collabnet. Goto their website and download the installer for CLIENT. Once installed make sure the bin folder of collabnet is in the PATH of windows to access the .exe SVN.
Now once done goto command prompt and type: SVN
If you dont get a unrecognized command error, your good to go.
Now follow the steps as below:
Checkout your local code.
svn checkout svn://133.46.0.1/ –username=darshan
For this example I am updating a checked out directory of mine known as windowsAPI.
I use the below command from the same directory of the checked out code:
svn update windowsAPI
To update successive directories:
svn update WindowsSystemAPI
You also can write a batch file and schedule the tasks in windows to update your local directories every time to be up to date.
posted by admin in Uncategorized 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

Tree – Insert an element into a tree in c++

Simple program to insert an element into a tree, I wrote in c++:

#include
#include
#include

// Define the variables used to store the data
// Use struct, or also you can use arrays to store values into a tree.
// use a struct if you dont know how many elements you would be
// storing into the tree.

struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;

// value to assign the memory needed
NODE getnode()
{
NODE x;
x = (NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(“out of memory\n”);
exit(0);
}
return x;
}

NODE insert(int item, NODE root)
{
char direction[10];
printf(“Enter the directions”);
scanf(“%s”, direction);

NODE temp;
NODE cur;
NODE prev;

temp = getnode();
temp->info = item;
temp->llink = temp->rlink = NULL;

if(root == NULL) return temp;

prev = NULL;
cur = root;

int i=0;
for(i=0;i< strlen(direction) && cur != NULL;i++)
{
prev = cur;
if(direction[i] == 'l')
cur =cur->llink;
else
cur = cur->rlink;
}
if(cur != NULL || i!=strlen(direction))
{
printf(“Insertion Fail”);
//free the memory you allocated. freenode(temp);
return root;
}
if(direction[i-1] == ‘l’)
prev->llink = temp;
else
prev->rlink = temp;

return root;

}

int main()
{
NODE temp;
int item;

NODE root;
insert(1, root);
char c;
do {
c=getchar();
putchar (c);
} while (c != ‘.’);
return 0;

return 0;
}

posted by admin in c++ and have No Comments

[Linker error] undefined reference to `WinMain@16′

If you have seen this error while compiling a c++ program on windows using bloodshed dev c++, then you would need to define a main method to resolve this:

int main()
{
return 0;
}

This would solve your issues.

posted by admin in c++ and have No Comments