ShowWindow的问题

倚栏|听风 2018-02-07 01:32:17
自建了一个窗口类,里面只有一个进度条,然后给了一个函数Test创建进度条以及开启个测试线程,想让进度条每隔2秒显示或者隐藏,为什么进度条总是不能隐藏?(PS:我想知道原因,而不是怎么实现这个功能)

TestWnd.h
#pragma once
#include "afxwin.h"
#include "afxcmn.h"
#include "atltypes.h"

#define UM_TEST WM_USER + 1

class CTestWnd :
public CWnd
{
public:
CTestWnd(void);
~CTestWnd(void);
private:
CProgressCtrl m_progressCtrl;
public:
void Test(void);
private:
afx_msg LRESULT OnTest(WPARAM wParam, LPARAM lParam);
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
private:
CRect m_rtProgress;
public:
afx_msg void OnSize(UINT nType, int cx, int cy);
};
.

TestWnd.cpp
#include "StdAfx.h"
#include "TestWnd.h"

BEGIN_MESSAGE_MAP(CTestWnd, CWnd)
ON_WM_ACTIVATE()
ON_MESSAGE(UM_TEST, CTestWnd::OnTest)
ON_WM_SIZE()
END_MESSAGE_MAP()

UINT MyTestThread(LPVOID pParam)
{
while (1)
{
Sleep(2000);
SendMessage(static_cast<HWND>(pParam), UM_TEST, NULL, NULL);
}

return 0;
}

CTestWnd::CTestWnd(void)
{
}

CTestWnd::~CTestWnd(void)
{
}

void CTestWnd::Test(void)
{
AfxBeginThread(MyTestThread, static_cast<LPVOID>(m_hWnd));
m_progressCtrl.Create(NULL, m_rtProgress, this, 0);
}


void CTestWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CWnd::OnActivate(nState, pWndOther, bMinimized);

// TODO: 在此处添加消息处理程序代码
}

LRESULT CTestWnd::OnTest(WPARAM wParam, LPARAM lParam)
{
if (m_progressCtrl.IsWindowVisible())
{
m_progressCtrl.ShowWindow(SW_HIDE);
}
else
{
m_progressCtrl.ShowWindow(SW_SHOW);
}

return 0;
}

void CTestWnd::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);

// TODO: 在此处添加消息处理程序代码
m_rtProgress.SetRect(cy / 2, 0, cx, cy / 2 + 30);
}
...全文
1477 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-02-09
  • 打赏
  • 举报
回复
参考11楼。
schlafenhamster 2018-02-09
  • 打赏
  • 举报
回复
case(WM_MYTSTMSG): { if(m_PrsCtr) { int nCmd = m_PrsCtr.IsWindowVisible()? SW_HIDE:SW_SHOW; m_PrsCtr.Invalidate(); m_PrsCtr.ShowWindow(nCmd); } break; }
倚栏|听风 2018-02-09
  • 打赏
  • 举报
回复
引用 23 楼 tajon1226 的回复:
线程里不要sendmessage,要用postmessage
有什么理由吗?而且,我这个用Postmessage也没用
走好每一步 2018-02-09
  • 打赏
  • 举报
回复
线程里不要sendmessage,要用postmessage
赵4老师 2018-02-09
  • 打赏
  • 举报
回复
引用 21 楼 qq_33462307 的回复:
[quote=引用 20 楼 zhao4zhong1 的回复:] 参考11楼。
那个好长啊,我看的晕晕乎乎的,没找到重点,真是醉了,您直接给个答案,我试过直接用对话框进行处理,发现没问题,我现在是想是不是和我创建的窗口是子窗口不是pop_up风格有关[/quote] 是现在不怕麻烦,硬啃难啃的资料一遍,彻底弄懂类似问题,将来一劳永逸好呢? 还是现在怕麻烦想偷懒走捷径,囫囵吞枣、人云亦云、隔靴搔痒,将来对此类问题保持一辈子浑浑噩噩的状态好呢? 这是个问题。
倚栏|听风 2018-02-09
  • 打赏
  • 举报
回复
引用 20 楼 zhao4zhong1 的回复:
参考11楼。
那个好长啊,我看的晕晕乎乎的,没找到重点,真是醉了,您直接给个答案,我试过直接用对话框进行处理,发现没问题,我现在是想是不是和我创建的窗口是子窗口不是pop_up风格有关
倚栏|听风 2018-02-08
  • 打赏
  • 举报
回复
引用 15 楼 schlafenhamster 的回复:
AfxBeginThread(MyTestThread, static_cast<LPVOID>(m_hWnd)); 放 OnActivate 试试
我问的是为什么在另外一个线程自定义消息发送,回调函数中调用ShowWindow为什么不隐藏,而不是怎么做能达到隐藏的效果。。。
倚栏|听风 2018-02-08
  • 打赏
  • 举报
回复
引用 14 楼 pxw_ps 的回复:
同样的问题遇到过,明明执行了showwindow,然而没什么用,后来解决了,记到工作日记里了,离职的时候电脑里的东西都不让拷贝。。。
解决了你还不说说是啥原因,说你是不是来砸场子的还是来看笑话的
schlafenhamster 2018-02-08
  • 打赏
  • 举报
回复
AfxBeginThread(MyTestThread, static_cast<LPVOID>(m_hWnd)); 放 OnActivate 试试
pxw_ps 2018-02-08
  • 打赏
  • 举报
回复
同样的问题遇到过,明明执行了showwindow,然而没什么用,后来解决了,记到工作日记里了,离职的时候电脑里的东西都不让拷贝。。。
倚栏|听风 2018-02-08
  • 打赏
  • 举报
回复
引用 18 楼 schlafenhamster 的回复:
你那样 是不是 AfxBeginThread 太多了 ?
就是个测试,我只会调用一次把副线程启动就行了,我想不明白,单步执行已经执行了hide为什么不能隐藏,如果这时我把窗口最小化一下再恢复,进度条就消失了,这是啥情况
schlafenhamster 2018-02-08
  • 打赏
  • 举报
回复
你那样 是不是 AfxBeginThread 太多了 ?
Lafite_MoMo 2018-02-07
  • 打赏
  • 举报
回复
你这是跨线程访问控件,在子线程中修改主线程创建的控件,虽然你是通过消息发送过去的。但是然并卵。。。。
赵4老师 2018-02-07
  • 打赏
  • 举报
回复
Multiple Threads in the User Interface http://msdn.microsoft.com/zh-cn/library/ms810439.aspx
zgl7903 2018-02-07
  • 打赏
  • 举报
回复
我的测试没有问题 复制了试试看

#define WM_MYTSTMSG (WM_USER + 100)

class CMyWnd : public CWnd
{
public:
  CMyWnd()
  {
  }

  virtual ~CMyWnd()
  {
  }

  CProgressCtrl m_PrsCtr;

public:
  virtual BOOL CreateEx(DWORD dwExStyle, LPCTSTR lpszWindowName, DWORD dwStyle, 
    const RECT& rect, CWnd* pParentWnd, UINT nID=0, LPVOID lpParam = NULL)
  {
    BOOL bRet = FALSE;
    LPCTSTR lpszClassName = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS|CS_SAVEBITS,
      ::LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), ::LoadIcon(NULL, IDI_HAND));
    bRet = CWnd::CreateEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, lpParam);

    if(bRet)
    {
      m_PrsCtr.Create(WS_CHILD|WS_VISIBLE, CRect(0,0, 100, 20), this, 1000);
    }

    SetTimer(10, 100, NULL);

    return bRet;
  }

  virtual BOOL DestroyWindow()
  {
    KillTimer(10);
    m_PrsCtr.DestroyWindow();
    return CWnd::DestroyWindow();
  }


protected:
  virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  {
    switch(message)
    {
    case(WM_TIMER):
      {
        if(wParam == 10)
        {
          if(m_PrsCtr.m_hWnd)
            m_PrsCtr.StepIt();
        }

        break;
      }
    case(WM_MYTSTMSG):
      {
        if(m_PrsCtr)
        {
          int nCmd = m_PrsCtr.IsWindowVisible()? SW_HIDE:SW_SHOW;
          m_PrsCtr.ShowWindow(nCmd);
        }
        break;
      }
    }

    return CWnd::WindowProc(message, wParam, lParam);
  }

};
//测试

BOOL CDlg6Dlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  
  // Set the icon for this dialog.  The framework does this automatically
  //  when the application's main window is not a dialog
  SetIcon(m_hIcon, TRUE);			// Set big icon
  SetIcon(m_hIcon, FALSE);		// Set small icon
  
  // TODO: Add extra initialization here
  
  m_MyWnd.CreateEx(0, _T("Test"), WS_POPUP|WS_CAPTION|WS_VISIBLE, CRect(0, 0, 400, 400), this);
  SetTimer(10, 2000, NULL);
  
  return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDlg6Dlg::OnTimer(UINT nIDEvent) 
{
  // TODO: Add your message handler code here and/or call default
  if(nIDEvent == 10)
  {
    m_MyWnd.PostMessage(WM_MYTSTMSG, 0, 0);
  }
  
  CDialog::OnTimer(nIDEvent);
}


BOOL CDlg6Dlg::DestroyWindow() 
{
  // TODO: Add your specialized code here and/or call the base class
  m_MyWnd.DestroyWindow();
  
  return CDialog::DestroyWindow();
}

赵4老师 2018-02-07
  • 打赏
  • 举报
回复
引用 9 楼 qq_33462307 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] ShowWindow The ShowWindow function sets the specified window's show state. BOOL ShowWindow( HWND hWnd, // handle to window int nCmdShow // show state of window ); Parameters hWnd Handle to the window. nCmdShow Specifies how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides aSTARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values: Value Meaning SW_FORCEMINIMIZE Windows NT 5.0 and later: Minimizes a window, even if the thread that owns the window is hung. This flag should only be used when minimizing windows from a different thread. SW_HIDE Hides the window and activates another window. SW_MAXIMIZE Maximizes the specified window. SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates the window and displays it as a minimized window. SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active. SW_SHOWNA Displays the window in its current state. The active window remains active. SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active. SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. Return Values If the window was previously visible, the return value is nonzero. If the window was previously hidden, the return value is zero. Remarks The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter. As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in theSTARTUPINFO structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show the window. On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application. This behavior is designed for the following situations: Applications create their main window by calling CreateWindow with the WS_VISIBLE flag set. Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow with the SW_SHOW flag set to make it visible. Windows CE: The nCmdShow parameter does not support the following values: SW_MAXIMIZE SW_MINIMIZE SW_RESTORE SW_SHOWDEFAULT SW_SHOWMAXIMIZED SW_SHOWMINIMIZED SW_SHOWMINNOACTIVE QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. Import Library: Use user32.lib. See Also Windows Overview, Window Functions,CreateProcess, CreateWindow, ShowOwnedPopups,STARTUPINFO, WinMain
赵老师,你这Copy MSND 差评,我读了一遍,没有有用信息。。。[/quote] 那就请你再读一千遍。 书读千遍,其义自现。
倚栏|听风 2018-02-07
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
ShowWindow The ShowWindow function sets the specified window's show state. BOOL ShowWindow( HWND hWnd, // handle to window int nCmdShow // show state of window ); Parameters hWnd Handle to the window. nCmdShow Specifies how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides aSTARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values: Value Meaning SW_FORCEMINIMIZE Windows NT 5.0 and later: Minimizes a window, even if the thread that owns the window is hung. This flag should only be used when minimizing windows from a different thread. SW_HIDE Hides the window and activates another window. SW_MAXIMIZE Maximizes the specified window. SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates the window and displays it as a minimized window. SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active. SW_SHOWNA Displays the window in its current state. The active window remains active. SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active. SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. Return Values If the window was previously visible, the return value is nonzero. If the window was previously hidden, the return value is zero. Remarks The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter. As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in theSTARTUPINFO structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show the window. On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application. This behavior is designed for the following situations: Applications create their main window by calling CreateWindow with the WS_VISIBLE flag set. Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow with the SW_SHOW flag set to make it visible. Windows CE: The nCmdShow parameter does not support the following values: SW_MAXIMIZE SW_MINIMIZE SW_RESTORE SW_SHOWDEFAULT SW_SHOWMAXIMIZED SW_SHOWMINIMIZED SW_SHOWMINNOACTIVE QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. Import Library: Use user32.lib. See Also Windows Overview, Window Functions,CreateProcess, CreateWindow, ShowOwnedPopups,STARTUPINFO, WinMain
赵老师,你这Copy MSND 差评,我读了一遍,没有有用信息。。。
倚栏|听风 2018-02-07
  • 打赏
  • 举报
回复
引用 7 楼 lz0905 的回复:
子线程阻塞了主线程,你将sendmessage改成postmessage试试
没用
Lafite_MoMo 2018-02-07
  • 打赏
  • 举报
回复
子线程阻塞了主线程,你将sendmessage改成postmessage试试
赵4老师 2018-02-07
  • 打赏
  • 举报
回复
ShowWindow The ShowWindow function sets the specified window's show state. BOOL ShowWindow( HWND hWnd, // handle to window int nCmdShow // show state of window ); Parameters hWnd Handle to the window. nCmdShow Specifies how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides aSTARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values: Value Meaning SW_FORCEMINIMIZE Windows NT 5.0 and later: Minimizes a window, even if the thread that owns the window is hung. This flag should only be used when minimizing windows from a different thread. SW_HIDE Hides the window and activates another window. SW_MAXIMIZE Maximizes the specified window. SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in theSTARTUPINFO structure passed to theCreateProcess function by the program that started the application. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates the window and displays it as a minimized window. SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active. SW_SHOWNA Displays the window in its current state. The active window remains active. SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active. SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. Return Values If the window was previously visible, the return value is nonzero. If the window was previously hidden, the return value is zero. Remarks The first time an application calls ShowWindow, it should use the WinMain function's nCmdShow parameter as its nCmdShow parameter. Subsequent calls to ShowWindow must use one of the values in the given list, instead of the one specified by the WinMain function's nCmdShow parameter. As noted in the discussion of the nCmdShow parameter, the nCmdShow value is ignored in the first call to ShowWindow if the program that launched the application specifies startup information in theSTARTUPINFO structure. In this case, ShowWindow uses the information specified in the STARTUPINFO structure to show the window. On subsequent calls, the application must call ShowWindow with nCmdShow set to SW_SHOWDEFAULT to use the startup information provided by the program that launched the application. This behavior is designed for the following situations: Applications create their main window by calling CreateWindow with the WS_VISIBLE flag set. Applications create their main window by calling CreateWindow with the WS_VISIBLE flag cleared, and later call ShowWindow with the SW_SHOW flag set to make it visible. Windows CE: The nCmdShow parameter does not support the following values: SW_MAXIMIZE SW_MINIMIZE SW_RESTORE SW_SHOWDEFAULT SW_SHOWMAXIMIZED SW_SHOWMINIMIZED SW_SHOWMINNOACTIVE QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. Import Library: Use user32.lib. See Also Windows Overview, Window Functions,CreateProcess, CreateWindow, ShowOwnedPopups,STARTUPINFO, WinMain
加载更多回复(5)
采用MFC编程时,子对话框常常要向主窗口传递数据,获取主窗口的对象指针便显得非常重要了。 void CMyView::OnModel() //点击菜单,创建对话框的几种变量设置,总共是4种方法,模态的一种,非模态的三种 { // TODO: Add your command handler code here //CDialog modDlg(MAKEINTRESOURCE(IDD_MODEL),this); //modDlg.DoModal(); static bool num=0;//第一次执行第一段,以后再打开都执行第二段。 static CModDlg Dlgless(this);//将MyView指针传进去 if (0==num) { CModDlg mod(this);//将MyView指针传进去 //mod.SetWindowText("局部变量——模态对话框");不允许动态设置模态对话框标题?这句编绎会出错 mod.DoModal(); Dlgless.Create(IDD_MODEL);//重复执行会出错,要放到构造函数中好 Dlgless.ShowWindow(SW_SHOW); Dlgless.SetWindowText("局部静态变量——非模对话框"); m_dlgless.Create(IDD_MODEL,this);//重复执行会出错,要放到构造函数中好 m_dlgless.ShowWindow(SW_SHOW); m_dlgless.SetWindowText("成员非指针变量——非模对话框"); m_pDlgless=new CModDlg(this);//重复执行会出错,要放到构造函数中好, m_pDlgless->Create(IDD_MODEL);//重复执行会出错,要放到构造函数中好, m_pDlgless->ShowWindow(SW_SHOW); m_pDlgless->SetWindowText("成员指针变量——非模对话框"); num=1; } else { Dlgless.ShowWindow(SW_SHOW); m_dlgless.ShowWindow(SW_SHOW); m_pDlgless->ShowWindow(SW_SHOW); } }

15,979

社区成员

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

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