对话框FAQ(来自网络)

一、在对话框中使用菜单

方法1

先在资源编辑器里制作好菜单,假设IDIDR_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;

}

方法2project->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::ModifyMenuCMenu::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();

方法2PreTranslateMessage 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);

 

二、改变对话框形状

可通过CreatePolygonRgnCreateRectRgnCreateRoundRectRgn 等函数来改变

CRgn m_rgn;

CRect rcDlg;

GetClientRect(rcDlg);

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

SetWindowRgn((HRGN)m_rgn,FALSE);

 

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

方法1OnLButtonDown中:

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

方法2OnLButtonDown中:

            SendMessage(WM_SYSCOMMAND,0xF012,0);

方法3OnNcHitTest

                  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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值