怎样将一个单文档程序 置顶

xiaogang_1 2009-06-16 05:31:25
我写了一个程序,是基于单文档的,但是我需要把这个程序置顶(像qq一样在所有窗口的最上层),
我在网上找了好多代码,可都是将对话框置顶,没有找到将单文档置顶的代码,难道单文档程序不能置顶吗?

请各位大侠指点一下了!
...全文
154 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyzoufeng 2009-08-04
  • 打赏
  • 举报
回复
重载CFrameWnd::PreCreateWindow,添加cs.dwExStyle |= WS_EX_TOPMOST;搞定!
xiaogang_1 2009-06-25
  • 打赏
  • 举报
回复
CWnd* pMain = AfxGetMainWnd();//获取主窗口指针
::SetWindowPos(pMain->m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE);
这样就完全正确了
allix123 2009-06-17
  • 打赏
  • 举报
回复
setwindowpos
DavidHsing 2009-06-17
  • 打赏
  • 举报
回复
简单:

::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
FerryLv 2009-06-16
  • 打赏
  • 举报
回复
以下两句可以:

CWnd* pMain = AfxGetMainWnd();//获取主窗口指针
SetWindowPos(pMain->m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE);
xiaogang_1 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 skyxie 的回复:]
引用 10 楼 xiaogang_1 的回复:

BOOL CMainFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class

dwStyle  |= WS_EX_TOPMOST;

return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}


晕,你的C…
[/Quote]


大侠,我看了,我的函数的确是用wizard重载的
确是 调的CWnd::Create而不是CFrameWnd::Create ?
而且我调试运行,CMainFrame::Create函数根本就不调用.

另外,谢谢大侠如此细心看我的代码,
长老QY 2009-06-16
  • 打赏
  • 举报
回复
使窗口顶层显示,可以调用CWnd::SetWindowPos函数,设置对话框窗口的层次为最顶层。
SetWindowPos函数的定义如下:
BOOL SetWindowPos(const CWnd* pWndInsertAfter, int x, int y, int cx, int cy,UINT nFlags);
其中:
pWndInsertAfter:窗口的Z轴次序。值为wndTopMost时表示窗口位于最顶层。
nFlags:窗口的大小和位置选项。值为SWP_NOMOVE时表示窗口保持当前的位置(忽略参数x和y);值为SWP_NOSIZE时表示窗口保持当前的大小(忽略参数cx和cy)。

//设置窗口的层次
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
skyxie 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xiaogang_1 的回复:]
BOOL CMainFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class

dwStyle |= WS_EX_TOPMOST;

return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}[/Quote]

晕,你的CMainFrame::Create是你自己添加的吗? 居然调的CWnd::Create而不是CFrameWnd::Create ?

使用wizard 重载, 应该是这个样子的

BOOL CMainFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle , const RECT& rect , CWnd* pParentWnd , LPCTSTR lpszMenuName , DWORD dwExStyle , CCreateContext* pContext)
{
dwExStyle |= WS_EX_TOPMOST;
return CFrameWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, lpszMenuName, dwExStyle, pContext);
}
wangrenjie_jeny 2009-06-16
  • 打赏
  • 举报
回复
mark!
xiaogang_1 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 skyxie 的回复:]
CMainFrame::Create

调用 CFrameWnd::Create  之前就  dwStyle |= WS_EX_TOPMOST 了?
[/Quote]

我是这么写的
BOOL CMainFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class

dwStyle |= WS_EX_TOPMOST;

return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
xiaogang_1 2009-06-16
  • 打赏
  • 举报
回复
我发现这个 CMainFrame::Create函数根本就没有调用,是怎么回事呢??
skyxie 2009-06-16
  • 打赏
  • 举报
回复
CMainFrame::Create

调用 CFrameWnd::Create 之前就 dwStyle |= WS_EX_TOPMOST 了?
xiaogang_1 2009-06-16
  • 打赏
  • 举报
回复
我在CMainFrame::Create里加入
dwStyle |= WS_EX_TOPMOST; 不起作用

又在 CMyView::Create里加入

dwStyle |= WS_EX_TOPMOST; 也不起作用

各位大侠 !!!! 能不能给具体代码啊
bragi523 2009-06-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xiaogang_1 的回复:]
三楼的大侠,具体怎么写啊,不会啊,再指点一下了
[/Quote]
virtual BOOL Create(
LPCTSTR lpszClassName,
LPCTSTR lpszWindowName,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID,
CCreateContext* pContext = NULL
);
第三个参数WS_EX_TOPMOST

::setwindowpos也是可以的,要加前面的::
xiaogang_1 2009-06-16
  • 打赏
  • 举报
回复
1楼和2楼的大侠,谢谢帮忙,但是SetWindowPos()只对对话框起作用,对单文档不起作用啊,
xiaogang_1 2009-06-16
  • 打赏
  • 举报
回复
三楼的大侠,具体怎么写啊,不会啊,再指点一下了
skyxie 2009-06-16
  • 打赏
  • 举报
回复
调用 CFrameWnd::Create 的时候 dwExStyle 传入 WS_EX_TOPMOST 就可以了~
neohope 2009-06-16
  • 打赏
  • 举报
回复
BOOL SetWindowPos(
HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);

hWnd是你窗口的句柄
bragi523 2009-06-16
  • 打赏
  • 举报
回复
::setwindowpos(::topmost.....);
不可以吗?

15,979

社区成员

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

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