Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8224981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:15:29+00:00 2026-06-07T15:15:29+00:00

I have simple win32 application (not dialog box), and treeview in this application. All

  • 0

I have simple win32 application (not dialog box), and treeview in this application.
All works, but if I have selected item in the treeview, and I minimize and restore application, selected item changes blue higlighting color to grayed color.
How to correct this without using custom draw?
If I have treeview in the dialog box, selected item has always blue color.

CODE:

HWND g_hTree;

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    MSG Msg;

    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    return (int) Msg.wParam;
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TREEVIEWTEST));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_TREEVIEWTEST);
    wcex.lpszClassName  = L"TreeViewTestClass";
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   InitCommonControls();

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(L"TreeViewTestClass", L"Tree View Test", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

void AddItems();

#define TVS_STYLES  (TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | TVS_SHOWSELALWAYS | TVS_INFOTIP | TVS_TRACKSELECT | WS_VSCROLL | WS_TABSTOP)
#define ID_TREEVIEW 505

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_CREATE:
        {
            g_hTree = CreateWindowEx(0, WC_TREEVIEWW, L"Test_Tree_View", WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_STYLES, 10, 10, 240, 480, hWnd, (HMENU)ID_TREEVIEW, NULL, NULL);
            AddItems();
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code here...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

void AddItems()
{
    TVITEM tvi;
    HTREEITEM htSelected;
    TVINSERTSTRUCT tvinsert;   // struct to config out tree control
    HTREEITEM htParent;           // Tree item handle
    HTREEITEM htBefore;           // .......
    HTREEITEM htRoot;  

    tvinsert.hParent=NULL;          // top most level no need handle
    tvinsert.hInsertAfter=TVI_ROOT; // work as root level
    tvinsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
    tvinsert.item.pszText=L"Parent1";
    tvinsert.item.iImage=0;
    tvinsert.item.iSelectedImage=1;

    htParent = TreeView_InsertItem(g_hTree, &tvinsert);

    htRoot = htParent;
    htBefore = htParent;

    tvinsert.hParent=htParent;         // handle of the above data
    tvinsert.hInsertAfter=TVI_LAST;  // below parent
    tvinsert.item.pszText=L"Child 1";

    htParent = TreeView_InsertItem(g_hTree, &tvinsert);

    tvinsert.hParent=htParent;
    tvinsert.item.pszText=L"Child Of Child 1";

    htParent = TreeView_InsertItem(g_hTree, &tvinsert);

    tvinsert.hParent=htBefore;         // handle of the above data
    tvinsert.hInsertAfter=TVI_LAST;  // below parent
    tvinsert.item.pszText=L"Child 2";

    htParent = TreeView_InsertItem(g_hTree, &tvinsert);

    tvinsert.hParent=NULL;          // top most level no need handle
    tvinsert.hInsertAfter=TVI_LAST; // work as root level
    tvinsert.item.pszText=L"Parent2";

    htParent = TreeView_InsertItem(g_hTree, &tvinsert);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:15:30+00:00Added an answer on June 7, 2026 at 3:15 pm

    It seems your tree-view control is losing focus when the window is minimized. You can try handling WM_ACTIVATE and using SetFocus to manually set focus to the tree-view control.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application which calls OpenFileDialog.ShowDialog. While this dialog is open it
I have a simple win32 application that uses the createProcess method to call Qt
I have created a simple win 32 application..in which it has a textbox and
I have compiled a simple win32 app successfully with bc++ (2 lines excerpt only):
Simple problem. I'm working on a Delphi 2007/WIN32 application which now uses MS Access
I have a native Win32 client application, and a .NET web application. I'd like
I'm creating a small win32 application. Currently I have 6 text labels coded in
For my cross-platform application I have started to use Boost, but I can't understand
I am trying to create a single executable of a simple Win32 application that
I have a C++ application that uses the Win32 API for Windows, and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.