最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

winapi - Several Thumbnails on Taskbar for runtime created windows - Stack Overflow

programmeradmin5浏览0评论

I try to make Windows show several thumbnails for forms created at runtime in the group with the main form. So, when the mouse hovers over the icon of the main form, I want to see the thumbnail for the main form and for the runtime created formular as well. I am not really fit when it comes to windows-programming, but did try my best. Unfortunately the thumbnail of the main form is now overwritten with the runtime form thumbnails and I cannot show the two in a group. What I am doing wrong? I attach here some code of the minimal config needed to reproduce the problem. First the WINAPI portion:

#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
//---------------------------------------------------------------------------
USEFORM("Unit1.cpp", Form1);
USEFORM("Unit2.cpp", Form2);
//---------------------------------------------------------------------------
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    try
    {
        Application->Initialize();
        Application->MainFormOnTaskBar = true;
        Application->CreateForm(__classid(TForm1), &Form1);
        Application->CreateForm(__classid(TForm2), &Form2);
        Application->Run();
    }
    catch (Exception &exception)
    {
        Application->ShowException(&exception);
    }
    catch (...)
    {
        try
        {
            throw Exception("");
        }
        catch (Exception &exception)
        {
            Application->ShowException(&exception);
        }
    }
    return 0;
}

And here is the code for Form1:

#include <vcl.h>
#include <windows.h>
#include <shobjidl.h>  // Required for ITaskbarList3
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
UINT TaskBarBtnCreatedId;
ITaskbarList3* pTaskbar=NULL;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
CoInitialize(NULL);
Application->OnMessage=MainWndProc;
TaskBarBtnCreatedId=RegisterWindowMessageA("TaskbarButtonCreated");
ChangeWindowMessageFilterEx(Form1->Handle, TaskBarBtnCreatedId, MSGFLT_ALLOW, NULL);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MainWndProc(tagMSG  &uMsg, bool& Handled){

    if(uMsg.message==TaskBarBtnCreatedId){
        if(pTaskbar==NULL){
            HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, (void**)&pTaskbar);
            pTaskbar->HrInit(); //not sure if need, behaviour doesn't change
            }
        }

    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form2->Show();
}

And here the code of the child form:

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{


}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormShow(TObject *Sender)
{
HWND hWnd = Handle;
HWND hMainWnd = Application->MainForm->Handle;

if(pTaskbar){
    HRESULT res=pTaskbar->RegisterTab(hWnd, hMainWnd);   // Register child form as a tab under main form
    if(res!=S_OK)
        ShowMessage("Taskbar cannot register tab");

    pTaskbar->SetTabOrder(hWnd, NULL);   // Set child tab order under the main window
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
if (pTaskbar) {
    pTaskbar->UnregisterTab(Handle); // Remove tab from taskbar on close
    }
}

After creating the child form, the child overwrites the main thumbnail. After closing the child form, the thumbnail returns to the main form.

This is how the group of thumbnails should look like in an real world example, however this code only produces a single tumbnail, never a group of thumnails:

发布评论

评论列表(0)

  1. 暂无评论