« November 2005 »
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
You are not logged in. Log in

Programming Tips
Thursday, 10 November 2005
How to get the height and width of a bitmap?
Mood:  energetic
Topic: VC++
It's real easy to retrieve the height and width of a bitmap. Trust me, take a look.

CBitmap cbBmp;
cbBmp.LoadBitmap(IDB_BITMAPID);
BITMAP bmpInfo;
//clear bmpInfo
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
cbBmp.GetBitmap(&bmpInfo);

There is another way of going about this. Take a look:

CBitmap cbBmp;
cbBmp.LoadBitmap(IDB_BITMAPID);
BITMAP bmpInfo;
//clear bmpInfo
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
cbBmp.GetObject(sizeof(bmpInfo), &bmpInfo)

//now get the height and width...
int bmpHeight = bmpInfo.bmHeight;
int bmpWidth = bmpInfo.bmWidth;


See I told you it's easy.

Posted by Nibu babu thomas at 3:48 PM
Updated: Thursday, 10 November 2005 4:48 PM
Quick way of changing background color of edit controls
Mood:  cheeky
Topic: VC++
Windows provides us a way to override the look and feel of a component at runtime throught this method. 
For more information take a look at the MSDN documenation on 
OnCtlColor().

The nCtlColor parameter contains the category of the control being painted, for eg:

CTLCOLOR_BTN -- Button control 
CTLCOLOR_DLG -- Dialog box 
CTLCOLOR_EDIT -- Edit control 
CTLCOLOR_LISTBOX -- List-box control 
CTLCOLOR_MSGBOX -- Message box 
CTLCOLOR_SCROLLBAR -- Scroll-bar control 
CTLCOLOR_STATIC -- Static control 

Well if you want to check for the painting of a particular control then use 
pWnd->GetDlgCtrlID(), which returns the id of the control being painted.

The device of the context of the control being painted is in the pointer pDC.

This is how we go about it:

HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  //call base class implementation first otherwise it may undo what we have done.
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  //Get the ctrl id and check whether we are painting the edit control or not. 
  if (pWnd->GetDlgCtrlID() == IDC_OF_EDITBOX)
  {
    // Set the text color to red.
    pDC->SetTextColor(RGB(255, 0, 0));

    //set to trasparent mode
    pDC->SetBkMode(TRANSPARENT);

    //unless we do this there won't be any effect.
    hbr = m_brush;
  }
  return hbr;
}

Posted by Nibu babu thomas at 11:05 AM
Updated: Thursday, 10 November 2005 4:47 PM
Tuesday, 8 November 2005
How to register a Hotkey for your application?
Mood:  sharp
Topic: VC++
Now what's a Hotkey. An easy definition would be a key that remains hot throughout the lifetime of an 
application. Whenever you press the hotkey it does it's work no matter where the focus is where the 
input is directed to, etc. 

So here's how we go about it.

//Make this a member variable of your class.
UINT uniqueIdentifier;

//creates a unique identifier for your hotkey so that there is no hotkey id clashes
uniqueIdentifier = ::GlobalAddAtom("Somename");
::RegisterHotKey(
	m_hWnd/*Your window handle*/,
	uniqueIdentifier/*Unique identifier to uniquely identify this hotkey*/,
	MOD_ALT|MOD_CONTROL /*Modifier keys*/,
	VK_F10/*Virtual key code of any key*/
	);

//Add these message map entries
BEGIN_MESSAGE_MAP(..., ...)
     ON_MESSAGE(WM_HOTKEY, HotKeyHandler)
     ON_WM_DESTROY()
END_MESSAGE_MAP()

//toggle visibility
LRESULT CYourDialog::HotKeyHandler(WPARAM wParam, LPARAM lParam)
{
     static BOOL visible = IsWindowVisible();
     if(visible)
           ShowWindow(SW_HIDE);
     else
           ShowWindow(SW_SHOWNORMAL);
     visible = !visible;
}

void CYourDialog::OnDestroy()
{
     //if you register you will have to unregister too.
     UnregisterHotKey(m_hWnd, uniqueIdentifier);
}

Yeah this is how we go about it.
A cleaner approach would be create a wrapper class around this hotkey. So that registration and 
unregistration can take place smoothly.

Posted by Nibu babu thomas at 5:28 PM
Updated: Thursday, 10 November 2005 4:46 PM
How to know the state of a key?
Mood:  energetic
Topic: VC++
During a function call if you want to determine the state of a key use

::GetAsyncKeyState(VK_SHIFT)

Wait a minute there is one more:

::GetKeyState(VK_SHIFT)

This function retrieves the status of the
specified virtual key. The status specifies whether 
the key is up, down, or toggled (on, off?alternating 
each time the key is pressed)

To retrieve state information for all the virtual keys, use

::GetKeyboardState(PBYTE lpKeyState)

lpKeyState:   Pointer to the 256-byte array that receives the status data for each virtual key.

Posted by Nibu babu thomas at 3:56 PM
Updated: Thursday, 10 November 2005 4:44 PM
Loading images on the fly
Mood:  cool
Topic: VC++
Anywhere any time. Happy programming.


HBITMAP hBmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle()/*application instance handle*/,
		"C:\\Nibu.bmp"/*file name*/,
		IMAGE_BITMAP/*or IMAGE_ICON, IMAGE_CURSOR*/,
		0/*original width*/,
		0/*original height*/,
		LR_LOADFROMFILE
		);
//create bitmap object
CBitmap *bmp = CBitmap::FromHandle(hBmp);
//That's it. Very easy ;)

Posted by Nibu babu thomas at 1:43 PM
Updated: Thursday, 10 November 2005 4:44 PM

Newer | Latest | Older