请教一个关于隐藏应用程序在任务栏图标的问题。大牛请进

starytx 2017-03-28 09:14:34
背景:vs2013,MFC对话框程序。
需求:可以动态隐藏和显示任务栏的图标,通过查找资料,现在可以实现在初始化时隐藏任务栏图标,方法就是在app的InitInstance()中先创建一个隐藏窗口,然后将主对话框的父窗口设为此窗口,然后将对话框的扩展属性WS_EX_APPWINDOW去掉(可以在对话框属性中设置“”Application Window“”为“”false“”,也可以通过代码ModifyStyleEx( WS_EX_APPWINDOW,0,); /在OninitDialog中去掉此样式),这样是可以实现隐藏,但是没办法再显示出来。网上的方法基本都是这句:ModifyStyleEx(WS_EX_TOOLWINDOW, WS_EX_APPWINDOW,); // 去掉toolwindow样式,增加APPwindow样式,这句没有效果。
具体方法如下:
	if (m_wndOwner.m_hWnd == NULL)
{
LPCTSTR pstrOwnerClass = AfxRegisterWndClass(0);
if (!m_wndOwner.CreateEx(0, pstrOwnerClass, _T(""), //创建一个隐藏的弹出样式的窗口
WS_POPUP, CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL, 0))
return FALSE;
}

CMyDlg dlg(&m_wndOwner);
m_pMainWnd = &dlg;
.....

if (m_wndOwner.m_hWnd != NULL)
m_wndOwner.DestroyWindow();

我现在的问题:就是如何动态隐藏和显示?总觉的用ModifyStyleEx修改样式之后还缺点啥。感觉修改样式只有放在初始化里才有效果,请不吝赐教。
...全文
222 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven 2017-03-28
  • 打赏
  • 举报
回复
很简单,在pFrame下面加句修改Icon的即可,如:
CFrameWnd* pFrame = new CFrameWnd;
	m_pMainWnd = pFrame;
	pFrame->Create(NULL, NULL);
	ASSERT(NULL != pFrame->GetSafeHwnd());
	HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	pFrame->SetIcon(hIcon, FALSE);
	pFrame->SetIcon(hIcon, TRUE);
starytx 2017-03-28
  • 打赏
  • 举报
回复
引用 1 楼 VisualEleven 的回复:

// App类的InitInstance()函数中添加
CFrameWnd* pFrame = new CFrameWnd;
	m_pMainWnd = pFrame;
	pFrame->Create(NULL, NULL);
	ASSERT(NULL != pFrame->GetSafeHwnd());

// 显示主对话框
CXXXDlg dlg;
dlg.DoModal();

// CXXXDlg对话框类的OnInitDialog()函数中在return之前加上
	ModifyStyleEx(WS_EX_APPWINDOW, 0);

// CXXXDlg类的某一个按钮的BN_CLICKED通知响应函数中加入:

void CXXXDlg::OnBnClickedButton1()
{
	static BOOL bStyle = FALSE;
	bStyle = !bStyle;

	ShowWindow(SW_HIDE);

	if (bStyle)
	{
		ModifyStyleEx(0, WS_EX_APPWINDOW);
	}
	else
	{
		ModifyStyleEx(WS_EX_APPWINDOW, 0);
	}
	ShowWindow(SW_SHOW);
}
我的父窗口创建那里使用我之前的代码,图标样式就正常了。完美解决!非常感谢~
starytx 2017-03-28
  • 打赏
  • 举报
回复
引用 2 楼 VisualEleven 的回复:
Managing Taskbar Buttons
The Shell creates a button on the taskbar whenever an application creates a window that isn't owned. To ensure that the window button is placed on the taskbar, create an unowned window with the WS_EX_APPWINDOW extended style. To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.
The Shell will remove a window's button from the taskbar only if the window's style supports visible taskbar buttons. If you want to dynamically change a window's style to one that does not support visible taskbar buttons, you must hide the window first (by calling ShowWindow with SW_HIDE), change the window style, and then show the window.
The window button typically contains the application icon and title. However, if the application does not contain a system menu, the window button is created without the icon.
If you want your application to get the user's attention when the window is not active, use the FlashWindow function to let the user know that a message is waiting. This function flashes the window button. Once the user clicks the window button to activate the window, your application can display the message.
厉害!原来关键在这里,要先隐藏一下修改样式后再显示一下。不过我发现这样处理后,任务栏的系统图标变成这样的了,不知道什么原因,如果不好搞先不管了,算是一点小瑕疵吧
Eleven 2017-03-28
  • 打赏
  • 举报
回复
Managing Taskbar Buttons The Shell creates a button on the taskbar whenever an application creates a window that isn't owned. To ensure that the window button is placed on the taskbar, create an unowned window with the WS_EX_APPWINDOW extended style. To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window. The Shell will remove a window's button from the taskbar only if the window's style supports visible taskbar buttons. If you want to dynamically change a window's style to one that does not support visible taskbar buttons, you must hide the window first (by calling ShowWindow with SW_HIDE), change the window style, and then show the window. The window button typically contains the application icon and title. However, if the application does not contain a system menu, the window button is created without the icon. If you want your application to get the user's attention when the window is not active, use the FlashWindow function to let the user know that a message is waiting. This function flashes the window button. Once the user clicks the window button to activate the window, your application can display the message.
Eleven 2017-03-28
  • 打赏
  • 举报
回复

// App类的InitInstance()函数中添加
CFrameWnd* pFrame = new CFrameWnd;
	m_pMainWnd = pFrame;
	pFrame->Create(NULL, NULL);
	ASSERT(NULL != pFrame->GetSafeHwnd());

// 显示主对话框
CXXXDlg dlg;
dlg.DoModal();

// CXXXDlg对话框类的OnInitDialog()函数中在return之前加上
	ModifyStyleEx(WS_EX_APPWINDOW, 0);

// CXXXDlg类的某一个按钮的BN_CLICKED通知响应函数中加入:

void CXXXDlg::OnBnClickedButton1()
{
	static BOOL bStyle = FALSE;
	bStyle = !bStyle;

	ShowWindow(SW_HIDE);

	if (bStyle)
	{
		ModifyStyleEx(0, WS_EX_APPWINDOW);
	}
	else
	{
		ModifyStyleEx(WS_EX_APPWINDOW, 0);
	}
	ShowWindow(SW_SHOW);
}

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧