显示活动对话框表单甚至主程序窗口都不活动

时间:2014-10-23 10:51:38

标签: winapi visual-c++ mfc

我需要在托盘中显示主窗体对话框最小化的程序的输入对话框形式。目前我在桌面上显示了输入对话框,但是当这个表单第一次显示时它处于非活动状态。要激活此对话框,请执行以下操作:

BOOL CDialogExInput::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    this->SetWindowPos(&this->wndTop,1000,1000,1000,1000,SWP_NOSIZE|SWP_NOMOVE);
    return true;
}

但是看起来这没有影响,因为即使对话框表单大小也没有改变。也许这不是正确的地方SetWindowPos

UPT:

试图集中我的问题。让我们开始使用简单的基于对话框的应用程序。让我们运行它并停用(在任何其他窗口上单击鼠标)。我需要在计时器事件中激活我的对话框应用程序表单(在键盘输入焦点)。 SetWindowPosSetFocus没有帮助。

// focusDlg.cpp : implementation file
//

#include "stdafx.h"
#include "focus.h"
#include "focusDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define ID_TIMER_1 100
// CfocusDlg dialog




CfocusDlg::CfocusDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CfocusDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CfocusDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CfocusDlg, CDialogEx)
    ON_WM_TIMER()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDOK, &CfocusDlg::OnBnClickedOk)
END_MESSAGE_MAP()


// CfocusDlg message handlers

BOOL CfocusDlg::OnInitDialog()
{
    CDialogEx::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
    SetTimer(ID_TIMER_1,3000,NULL);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CfocusDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialogEx::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CfocusDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}

void CfocusDlg::OnTimer(UINT)
{
    //MessageBox(L"Time");
    this->SetWindowPos(&this->wndTop,1000,1000,1000,1000,SWP_NOSIZE|SWP_NOMOVE);// | SWP_SHOWWINDOW );
    this->SetFocus();
}



void CfocusDlg::OnBnClickedOk()
{
    // TODO: Add your control notification handler code here
    CDialogEx::OnOK();
}

1 个答案:

答案 0 :(得分:0)

OnInitDialog()正是您可以修改对话窗口的正确位置;当框架调用此函数时,对话框窗口已经存在但尚未可见。

如果您希望移动窗口并调整其大小,则在调用SWP_NOSIZE期间,您不能指定SWP_NOMOVESetWindowPos标记。