天天看点

对话框FAQ(来自网络)

一、在对话框中使用菜单

方法1:

先在资源编辑器里制作好菜单,假设ID为IDR_MENU1,然后打开对话框的属性窗口,在Dialog属性里的General中选择你的Menu菜单的ID就行了

方法2:

CMenu menu;           menu.LoadMenu(IDR_MENU1);       SetMenu(&menu);

方法3:

HMENU hMenu = LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MENU1));

::SetMenu(m_hWnd,hMenu);

方法4:动态创建:

CMenu menu,subMenu;

VERIFY(menu.CreateMenu());

VERIFY(subMenu.CreatePopupMenu());

subMenu.AppendMenu(MF_STRING,ID_11,(LPCTSTR)"11");

subMenu.InsertMenu(0,MF_BYCOMMAND,ID_12,(LPCTSTR)"12");

menu.AppendMenu(MF_POPUP,(UINT)subMenu.m_hMenu,"&MyMenu");

SetMenu(&menu);

二、改变对话框背景色

方法1:.h中:CBrush m_Brush;                  .cpp中:在OnInitDialog()中:   m_Brush.CreateSolidBrush(RGB(0,128,128)); OnCtlColor中: return m_Brush;

方法2:在CXXApp::InitInstance()中dlg.DoModal()前 SetDialogBkColor(RGB(0,255,0),RGB(255,0,0));

三、为对话框中的控件加上提示信息

方法1:在XXdlg.h中:CToolTipCtrl m_TipCtrl;

在XXDlg.cpp中:在OnInitDialog()中:

CButton *pButton = NULL;

pButton = (CButton *)GetDlgItem(IDC_BUTTON1);

m_TipCtrl.Create(this);

m_TipCtrl.AddTool(pButton,"This is a button");

CEdit *pEdit = NULL;

pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);

m_TipCtrl.AddTool(pEdit,"This is an edit box");

在PreTranslateMessage中:

switch (pMsg->message)

{

case WM_LBUTTONDOWN:

case WM_LBUTTONUP:

case WM_MOUSEMOVE:

           m_TipCtrl.RelayEvent(pMsg);

           break;

}

方法2:project->Add to Project->component and control->VC++ component->Tooltip support

         在OnInitDialog中加入,m_tooltip.AddTool(GetDlgItem(IDC_),”ToolTip”);

四、如何使对话框上的关闭按钮失效

方法1:重载WM_CLOSE,注销掉CDialog::OnClose()

方法2:通过CWnd::GetSystemMenu得到句柄,再通过CMenu::ModifyMenu或CMenu::EnableMenuItem()函数改变系统菜单的属性,在OnInitDialog中:      CMenu *pMenu = this->GetSystemMenu(FALSE);

pMenu->ModifyMenu(SC_CLOSE,MF_BYCOMMAND | MF_GRAYED);

或者:

CMenu *pMenu = this->GetSystemMenu(FALSE);

pMenu->EnableMenuItem(SC_CLOSE,MF_BYCOMMAND | MF_GRAYED);

五、如何改变对话框内控件的字体

CEdit *pEdit = NULL;

pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);

m_TipCtrl.AddTool(pEdit,"This is an edit box");

m_Font.CreateFont(14,0,0,0,100,TRUE,TRUE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,

           CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"隶书");

pEdit->SetFont(&m_Font,FALSE);

六、如何屏蔽回车键与ESC键

方法1:重载OnOK(),注释掉CDialog::OnOK();

方法2:PreTranslateMessage: if (pMsg->message==WM_KEYDOWN)

{

           switch (pMsg->wParam)

{

case VK_RETURN:  case VK_ESCAPE:  return TRUE;

}

}

七、在对话框上显示Bmp图片

a、   class CAboutDlg内:CBitmap *m_pBmp;

b、   构造函数中:m_pBmp = NULL;

c、   OnInitDialog中:        HBITMAP hBmp = (HBITMAP)::LoadImage(0,"f://1.bmp",IMAGE_BITMAP,0,0,

LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);

                                       _ASSERT(hBmp);

                                      m_pBmp = CBitmap::FromHandle(hBmp);

d、   OnPaint中:         BITMAP bm;

     CDC memDC;

     VERIFY(m_pBmp->GetObject(sizeof(bm),(LPVOID)&bm));

     memDC.CreateCompatibleDC(&dc);

     CBitmap *pOldBmp = (CBitmap *)memDC.SelectObject(m_pBmp);

     BitBlt(dc.m_hDC,0,0,bm.bmWidth,bm.bmHeight,memDC.m_hDC,0,0,SRCCOPY);

     memDC.SelectObject(pOldBmp);

八、如何在不使用通用文件打开对话框的情况下显示一个文件列表

TCHAR szPath[MAX_PATH] = {"C:"};

//在ListBox中显示

DlgDirList(szPath,IDC_LIST2,NULL,DDL_DIRECTORY);

//在combo box中显示

this->DlgDirListComboBox(szPath,IDC_COMBO1,NULL,DDL_READWRITE | DDL_READONLY | DDL_HIDDEN | DDL_SYSTEM | DDL_ARCHIVE | DDL_DIRECTORY | DDL_DRIVES);

九、如何让对话框在什么样不显示

DWORD dwStyle = ::GetWindowLong(AfxGetMainWnd()->m_hWnd,GWL_EXSTYLE);

dwStyle = WS_EX_TOOLWINDOW;

AfxGetMainWnd()->ShowWindow(SW_HIDE);

::SetWindowLong(AfxGetMainWnd()->m_hWnd,GWL_EXSTYLE,dwStyle);

十、如何让Static控件响应WM_MOUSEMOVE消息

                    CRect rect;

                   m_Static.GetWindowRect(&rect);

                    ScreenToClient(&rect);

                   if (rect.PtInRect(point))      {        MessageBox("In");    }

十一、为对话框中加入工具条

m_ToolBar.Create(this,WS_CHILD | WS_VISIBLE |CBRS_TOP|CBRS_TOOLTIPS |CBRS_FLYBY | CBRS_BORDER_BOTTOM);

m_ToolBar.LoadToolBar(IDR_TOOLBAR1);

CRect rcClientStart,rcClientNow;

GetClientRect(rcClientStart);

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNow);

CPoint ptOffset(rcClientNow.left-rcClientStart.left,rcClientNow.top-rcClientStart.top);

CRect rcChild;

CWnd *pWndChild = GetWindow(GW_CHILD);

while (pWndChild)

{

    pWndChild->GetWindowRect(rcChild);

    ScreenToClient(rcChild);

    rcChild.OffsetRect(ptOffset);

    pWndChild->MoveWindow(rcChild,TRUE);

    pWndChild = pWndChild->GetNextWindow();

}

CRect rcWindow;

GetWindowRect(rcWindow);

rcWindow.right+=rcClientStart.Width()-rcClientNow.Width();

rcWindow.bottom+=rcClientStart.Height()-rcClientNow.Height();

MoveWindow(rcWindow,TRUE);

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);

十二、改变对话框形状

可通过CreatePolygonRgn、CreateRectRgn、CreateRoundRectRgn 等函数来改变

CRgn m_rgn;

CRect rcDlg;

GetClientRect(rcDlg);

m_rgn.CreateEllipticRgn(0,0,rcDlg.Width(),rcDlg.Height());

SetWindowRgn((HRGN)m_rgn,FALSE);

十三、如何实现非客户区移动

方法1:OnLButtonDown中:

            PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));

方法2:OnLButtonDown中:

            SendMessage(WM_SYSCOMMAND,0xF012,0);

方法3:OnNcHitTest中

                  UINT nHitTest = CDialog::OnNcHitTest(CSize(point.x,point.y));

      return (nHitTest==HTCLIENT) ? HTCAPTION:nHitTest;

十四、  在对话框中加入状态条

在.h中加入CStatusBar m_StatusBar;

在.cpp中加入static UINT BASED_CODE indicators[] =

{

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM

};

在OnInitDialog中加入:

m_StatusBar.CreateEx(this,SBT_TOOLTIPS);

m_StatusBar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT));

CRect rect;

GetClientRect(&rect);

m_StatusBar.SetPaneInfo(0,ID_INDICATOR_CAPS,SBPS_NORMAL,rect.Width()/2);

m_StatusBar.SetPaneInfo(1,ID_INDICATOR_NUM,SBPS_NORMAL,rect.Width()/2);

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,ID_INDICATOR_NUM);

m_StatusBar.GetStatusBarCtrl().SetBkColor(RGB(0,180,180));

十五、改变鼠标指针

OnSetCursor中:

SetCursor(AfxGetApp()->LoadStandardCursor(IDC_UPARROW));

return 0;   

十六、加入上下文帮助

在OnInitDialog中

ModifyStyleEx(0,WS_EX_CONTEXTHELP);

在OnHelpInfo中:

short state = GetKeyState(VK_F1);

if (state<0)

{

     return CDialog::OnHelpInfo(pHelpInfo);

}

else

{

     if (pHelpInfo->dwContextId)

     {

         WinHelp(pHelpInfo->dwContextId,HELP_CONTEXTPOPUP);

     }

     return TRUE;

}

继续阅读