Thursday, July 24, 2008

CStatic control loses transparency in Visual C++

Problem

In my current Visual C++ 2008 project, I’m attempting to display dynamic text on a custom control while maintaining a transparent background. For this purpose, I’ve subclassed a CStatic control, creating a derived class named StaticCtrl.

Initially, when the control is rendered, it correctly appears with a transparent background as intended. However, the issue arises when I update the control’s content—specifically, when I set new text. At that point, the control loses its transparency and reverts to the default opaque appearance typical of a standard CStatic control. This behavior undermines the visual consistency I’m aiming for.

To address this, I’ve overridden the OnPaint method in my StaticCtrl class. This is the only customization I’ve made—no other message handlers or style modifications are involved. My goal was to handle the painting manually to preserve transparency while updating the displayed content. Below is the implementation of the OnPaint method:

void StaticCtrl::OnPaint()
{
    CPaintDC dc(this); // device context for painting
   
    // Where to draw text
    CRect clientRect;
    GetClientRect(clientRect);
   
    // Get the caption
    CString strTitle;
    GetWindowText(strTitle);
   
    // Get the font
    CFont *pFont, *pOldFont;
    pFont = GetFont();
    pOldFont = dc.SelectObject(pFont);
   
    DWORD dwStyle = GetStyle(), dwText = 0;
   
    // Map "Static Styles" to "Text Styles"
    #define MAP_STYLE(src, dest)
        if(dwStyle & (src)) dwText |= (dest)
    #define NMAP_STYLE(src, dest)
        if(!(dwStyle & (src))) dwText |= (dest)
   
    MAP_STYLE(SS_RIGHT, DT_RIGHT);
    MAP_STYLE(SS_CENTER, DT_CENTER);
    MAP_STYLE(SS_CENTERIMAGE, DT_VCENTER | DT_SINGLELINE);
    MAP_STYLE(SS_NOPREFIX, DT_NOPREFIX);
    MAP_STYLE(SS_WORDELLIPSIS, DT_WORD_ELLIPSIS);
    MAP_STYLE(SS_ENDELLIPSIS, DT_END_ELLIPSIS);
    MAP_STYLE(SS_PATHELLIPSIS, DT_PATH_ELLIPSIS);
    NMAP_STYLE(SS_LEFTNOWORDWRAP | SS_CENTERIMAGE |
               SS_WORDELLIPSIS | SS_ENDELLIPSIS |
               SS_PATHELLIPSIS, DT_WORDBREAK );
   
    // Set transparent background
    dc.SetBkMode(TRANSPARENT);
   
    // Draw the text
    if(GetDlgCtrlID() == IDC_STATIC_CANCEL)
    dc.SetTextColor(RGB(255, 0, 0));
   
    if(GetDlgCtrlID() == IDC_CONNECTIONTXT ||
       GetDlgCtrlID() == IDC_RATETXT)
    dc.SetTextColor(RGB(250, 100, 0));
   
    if(GetDlgCtrlID() == IDC_PERCENT ||
       GetDlgCtrlID() == IDC_REMAINING ||
       GetDlgCtrlID() == IDC_TIMETXT)
       dc.SetTextColor(RGB(250, 100, 0));

    dc.DrawText(strTitle, clientRect, dwText);
   
    // Select old font
    dc.SelectObject(pOldFont);
}

Solution

When customizing the background color of a CDialog, you might notice that certain controls—especially CStatic text labels—continue to display their default background color, typically COLOR_BTNFACE. This mismatch can disrupt the visual consistency of your dialog, especially if you're aiming for a custom theme or aesthetic.

To resolve this, you can make the background of these controls transparent so they inherit the dialog’s background color. This is particularly useful for static text controls that should blend seamlessly with the rest of the interface.

In a subclassed dialog (e.g., CMyDialog derived from CDialog), you can override the OnCtlColor() message handler to apply transparency to all CStatic controls. Here’s how you can implement it:

HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    CBrush m_brHollow = m_brHollow.CreateStockObject(HOLLOW_BRUSH);

    if (nCtlColor == CTLCOLOR_EDIT) {
        pDC->SetTextColor(RGB(0, 0, 0));  // text color
        pDC->SetBkMode(TRANSPARENT);      // background mode
        hbr = m_brHollow;                 // return NULL brush
    }
   
    return hbr;
}

When you customize your dialog background to white, the OnCtlColor() implementation I shared earlier works perfectly for CStatic controls. They correctly inherit the dialog’s background and appear transparent.

However, things behave differently with a CSliderCtrl. Using the same approach, you’ll notice that the slider’s background turns completely black instead of blending with the dialog’s white background. This happens because returning a NULL_BRUSH in OnCtlColor() doesn’t interact well with certain controls like sliders, which then default to a solid black fill.

A simple workaround is to return a handle to a white brush rather than a NULL_BRUSH. This ensures that the slider control background matches the dialog’s white background consistently. The following snippet demonstrates the fix, assuming CMyDialog is derived from CDialog and the dialog background has already been painted white by overriding CDialog::OnEraseBkgnd():

HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    CBrush m_brBgColor;

    // dialog background WHITE
    m_brBgColor.CreateSolidBrush(RGB(255, 255, 255));
   
    if (nCtlColor == CTLCOLOR_EDIT) {
        pDC->SetTextColor(RGB(0, 0, 0)); // text color
        pDC->SetBkMode(TRANSPARENT);     // background mode
        hbr = m_brBgColor;               // return WHITE brush
    }
   
    return hbr;
}

This adjustment ensures that both CStatic and CSliderCtrl controls render correctly against a white dialog background, maintaining a consistent and clean appearance across your UI. Refer this.



Friday, August 17, 2007

WikiScanner can track anonymous Wikipedia edits

2007-08-17

WikiScanner or Wikipedia Scanner provides an online searchable tool that takes a database dump from Wikipedia and crawls through anonymous edits which were made from source IP addresses that can be traced back to organizations who may have a vested interest in the content of the respective Wikipedia pages. It uses a IP2Location database to connect IP addresses with organizations.

In most cases these were edits where the organizations would have removed content with criticism. The homepage lists the following organizations as editor's picks on its homepage.

  1. Government
    1. U.S. Senate Sergeant At Arms
    2. U.S. House Of Representatives
    3. Environmental Protection Agency
    4. National Institute of Health
    5. Democratic Party
    6. Republican Party
    7. NATO
  2. Education
    1. California Institute of Technology
    2. Bob Jones University
  3. Policy
    1. Electronic Frontier Foundation
    2. The Rand Corporation
    3. National Rifle Association
    4. American Civil Liberties Union
  4. Corporate
    1. Diebold Inc
    2. Amgen Inc
    3. Pfizer Inc
    4. Wal-Mart Stores Inc
    5. ExxonMobil
    6. Raytheon
  5. News
    1. Washington Post
    2. Washington Times
    3. Fox News Channel
    4. New York Times Company
    5. Al-Jazeera
  6. Locations
    1. Ft. George G Meade, Maryland
    2. Havana, Ciudad De La Habana
  7. Misc
    1. darpa.mil
    2. Central Intelligence Agency
    3. Church of Scientology
    4. Vatican
    5. Christian Science Publishing Society
    6. Church of Latter Day Saints

The creator, Virgil Griffith, wrote in the FAQ page that he created WikiScanner "to create a fireworks display of public relations disasters in which everyone brings their own fireworks, and enjoys". He however suggests that a tool like WikiScanner is not necessary and anonymous speech should be preserved.

The Wired wrote See Who's Editing Wikipedia - Diebold, the CIA, a Campaign to break the story. 

The online tool is available here.

Compact disc hits 25th birthday

2007-08-17

The world's first compact disc (CD) was produced by Philips and Sony 25 years ago, revolutionizing the music industry and becoming a dominant format. CDs have sold over 200 billion units worldwide and are still popular despite digital downloads' growth. The CD's development involved overcoming technical challenges, and it was initially aimed at holding an hour of audio but extended to 74 minutes for a complete symphony. Although CD sales have declined in recent years, they remain a significant format, cherished by some music enthusiasts. Here is an article from BBC News on the compact disc turning 25.

Monday, August 13, 2007

Google Shutting Down Google Video

2007-08-13

Google is shutting down its premium video service, leaving users who purchased or rented content unable to access their videos in the future. They won't receive refunds but are offered a $5 credit on Google Checkout, expiring in 60 days. The move comes after Google's acquisition of YouTube on October 9, 2006, making Google Video less relevant. Analysts criticize the decision, as users lose access to content they bought despite functioning players. Google originally launched this video hosting service on January 25, 2005. The BBC reports it here.

Monday, July 9, 2007

Yahoo.com is Asia's Most Popular Website

Yahoo.com is not only the most popular web site in the US but also the top choice in Asia, leading in markets like Japan, India, Taiwan, Hong Kong, Malaysia, and Singapore. Microsoft and Google follow as the second and third most popular sites in the region, respectively. US web companies are investing more in Asia to tap into the region's rapidly growing economies like China and India. However, internet usage in Asia lags behind the global average, with South Korea having the highest rate at 65% and India and China at the bottom with 3% and 9%, respectively. Yahoo operates in China and Japan through partnerships with local companies. The Financial Express writes Yahoo.com is Asia’s favorite website here.

Wednesday, May 23, 2007

Google buying FeedBurner for $100 million

In this context, it is essential to recognize that the aggregation of personal preferences and browsing history of ordinary users is consolidating within the databases of a single company. This aggregation enables the correlation and analysis of vast amounts of private information. While this information is already scattered across the internet, the concern arises from its gradual centralization under the control of a single private entity. The potential consequences for our personal and private data in relation to our browsing experiences are a matter of concern.

One notable outcome of this centralization is the potential for the company to offer more integrated, personalized, and localized services to its users. This aspect is particularly intriguing in the current landscape of internet search dominated by companies like Google.

Wired writes Google To Drop $100 Million On Feedburner here. BetaNews reports Google to Acquire Feedburner for $100 Million here.

Friday, May 18, 2007

Can Google and Linux Topple Microsoft?

The Federal Aviation Administration's CIO, David Bowen, is considering replacing Microsoft Windows and Office with Linux and Google Apps Premium for cost and compatibility reasons. Google Apps offers collaborative tools, but not all functionalities of Microsoft Office. Concerns exist over data security with third-party servers. Microsoft argues that costs are not solely dependent on software and addresses compatibility issues. The FAA has not made a final decision yet. Here is an article from Route-Fifty.