Q:如何在对话框中加入工具条

在 OnInitDialog 中加入下面代码:

BOOL CYourDlg::OnInitDialog()

{

CDialog::OnInitDialog();

// Create the toolbar. To understand the meaning of the styles used, you

// can take a look at the MSDN for the Create function of the CToolBar class.

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

// I have assumed that you have named your toolbar''s resource as IDR_TOOLBAR1.

// If you have given it a different name, change the line below to accomodate

// that by changing the parameter for the LoadToolBar function.

ToolBar.LoadToolBar(IDR_TOOLBAR1);

CRect rcClientStart;

CRect rcClientNow;

GetClientRect(rcClientStart);

// To reposition and resize the control bar

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, FALSE);

pwndChild = pwndChild->GetNextWindow();

}

CRect rcWindow;

GetWindowRect(rcWindow);

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

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

MoveWindow(rcWindow, FALSE);

// And position the control bars

RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

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

}

 Q:如何改变对话框的形状?

可用下面一些函数: 
CreatePolygonRgn
CreateRectRgn
CreateRoundRectRgn 等.

CRgn m_rgn;  // Put this in your dialog''s header file. i.e. a member variable

// This Gets the size of the Dialog: This piece of code is to be placed in the

// OnInitDialog Function of your dialog.

CRect rcDialog

GetClientRect(rcDialog);

// The following code Creates the area and assigns it to your Dialog

m_rgn.CreateEllipticRgn(0, 0, rcDialog.Width(), rcDialogHeight());

SetWindowRgn(GetSafeHwnd(), (HRGN) m_rgn, TRUE);

 Q:如何实现非客户区移动?

可用下面二种方法

// Handler for WM_LBUTTONDOWN message

void CYourDialog::OnLButtonDown(UINT nFlags, CPoint point)

{

CDialog::OnLButtonDown(nFlags, point);

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

}

// Handler for WM_NCHITTEST message

LONG CYourDialog::OnNcHitTest( UINT uParam, LONG lParam )

{

int xPos = LOWORD(lParam);

int yPos = HIWORD(lParam);

UINT nHitTest = CDialog::OnNcHitTest(CSize(xPos, yPos));

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

}

 Q:如何使对话框初始为最小化状态?

在 OnInitDialog 中加入下面代码:

SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, NULL);

 Q:如何限定对话框大小范围?

在 WM_SIZING中加入下面代码:

void CYourDialog::OnSizing(UINT fwSide, LPRECT pRect)

{

if(pRect->right - pRect->left <=200)

pRect->right = pRect->left + 200;

if(pRect->bottom - pRect->top <=200)

pRect->bottom = pRect->top + 200;

CDialog::OnSizing(fwSide, pRect);

}

 Q:如何在对话框中加入状态条?

定义 CStatusBar 变量:

CStatusBar m_StatusBar;

定义状态条指定状态:

static UINT BASED_CODE indicators[] =

{

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM

};

在 OnInitDialog 中加入下面代码:

m_StatusBar.CreateEx(this,SBT_TOOLTIPS,WS_CHILD|WS_VISIBLE|CBRS_BOTTOM,AFX_IDW_STATUS_BAR);

// Set the indicators namely caps and nums lock status

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_STRETCH ,rect.Width()/2);

RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,ID_INDICATOR_NUM);

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

 Q. 如何有效地使初始窗口不显示
当我们想让窗口初始时不显示时,通常会用ShowWindow(SW_HIDE) ,但实际上还是在启动是可以看到窗口一闪而过的痕迹。所以,可以使用下面的方法来实现它:
(1.1)先在构造函数中设置布乐变量 visible值为false.

visible = false;

(1.2)重载 WM_WINDOWPOSCHANGING,并添加下面代码:

void CTest_deleteDlg::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)

{

if(!visible)

lpwndpos->flags &= ~SWP_SHOWWINDOW;

CDialog::OnWindowPosChanging(lpwndpos);

}

(1.3)然后设布尔visible变量值为true,并在要显示窗口时,再用ShowWindow(SW_SHOW)既可。

visible = true;

ShowWindow(SW_SHOW);

 Q. 对话框的全屏显示
对话框的全屏显示可以在OnInitDialog()中用 SetWindowPos 和 HWND_TOPMOST 来实现对话框的重新大小。

BOOL CFullScrDlgDlg::OnInitDialog()

{

CDialog::OnInitDialog();

//...

int cx, cy;

HDC dc = ::GetDC(NULL);

cx = GetDeviceCaps(dc,HORZRES) +

GetSystemMetrics(SM_CXBORDER);

cy = GetDeviceCaps(dc,VERTRES) +

GetSystemMetrics(SM_CYBORDER);

::ReleaseDC(0,dc);

//去除标题和边框

SetWindowLong(m_hWnd, GWL_STYLE,

GetWindowLong(m_hWnd, GWL_STYLE) &

(~(WS_CAPTION | WS_BORDER)));

// 置对话框为最顶端并扩充到整个屏幕

::SetWindowPos(m_hWnd, HWND_TOPMOST,

-(GetSystemMetrics(SM_CXBORDER)+1),

-(GetSystemMetrics(SM_CYBORDER)+1),

cx+1,cy+1, SWP_NOZORDER);

//...

return TRUE;

}

 Q. 如何在2K/xp下使窗口获取焦点
在2K/XP下我们可以用 AttachThreadInput 和SetForegroundWindow来有效的获取焦点。

//捕捉并设置当前焦点窗口为我们的窗口

AttachThreadInput(

GetWindowThreadProcessId(

::GetForegroundWindow(),NULL),

GetCurrentThreadId(),TRUE);

//置我们的为焦点窗口

SetForegroundWindow();

SetFocus();

//释放thread

AttachThreadInput(

GetWindowThreadProcessId(

::GetForegroundWindow(),NULL),

GetCurrentThreadId(),FALSE);

 Q. 使你的对话框位于最顶端
可以直接在 OnInitDialog()中用SetWindowPos来实现。

SetWindowPos(&this->wndTopMost,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);

5. 如何动态放大/缩小对话框
还是可以用SetWindowPos或MoveWindow来实现它。

void CTest_deleteDlg::OnMakeSmall()

{

SetWindowPos(NULL,0,0,200,200,SWP_NOZORDER|SWP_NOMOVE);

}

void CTest_deleteDlg::OnExpand()

{

SetWindowPos(NULL,0,0,500,300,SWP_NOZORDER|SWP_NOMOVE);

}

或:

//伸、缩在IDC_DYCREDITS和IDC_COPYRIGHT两STATIC控件间,做为分隔线

BOOL CAbout::OnInitDialog()

{

CDialog::OnInitDialog();

//"关于"对话框中对话框可收缩效果

CRect Rect1,Rect2;             //对话框收缩时大小

GetDlgItem(IDC_DYCREDITS)->GetWindowRect(Rect1);

GetDlgItem(IDC_COPYRIGHT)->GetWindowRect(Rect2);

m_nReducedHeight = Rect1.Height()+(Rect1.top -Rect2.bottom)/2; //收缩后窗体高度

dlgRect.bottom -= (Rect1.Height()+(Rect1.top -Rect2.bottom)/2);

MoveWindow(&dlgRect);                  //如果要显示对话框起始动态效果的话,不能使用该句

m_bVertical=false;                                //默认收缩对话框

}

// ---------------------------------------------------------

// 名称: OnMore

// 功能: 是否允许显示

// 变量: 无

// 返回: 无

// 编写: 徐景周,2002.4.8

// ---------------------------------------------------------

void CAbout::OnMore()

{

m_bVertical = !m_bVertical;

if(m_bVertical == FALSE) //不显示

{

SetDlgItemText(ID_MORE,_T("更多>>"));

SizeWindow(m_nReducedHeight,true);

//  m_DyCredits.EndScrolling();              //停止滚动

}

else      //显示

{

SetDlgItemText(ID_MORE,_T("<<隐藏"));

SizeWindow(m_nReducedHeight,false);

m_DyCredits.StartScrolling();   //开始滚动

}

UpdateWindow();

}

// ---------------------------------------------------------

// 名称: SizeWindow

// 功能: 伸展或收缩对话框

// 变量: ReduceHeight-收缩高度,bExtend-是否伸展

// 返回: 无

// 编写: 徐景周,2002.4.8

// ---------------------------------------------------------

void CAbout::SizeWindow(int ReduceHeight, bool bExtend)

{

CRect rc;

GetWindowRect(&rc);

if(bExtend)

{

for (int i= 0; i < ReduceHeight; i++)

{

rc.bottom--;

MoveWindow(&rc);

}

}

else

{

for (int i= 0; i < ReduceHeight; i++)

{

rc.bottom++;

MoveWindow(&rc);

}

}

}

 Q. 如何让对话框回到屏幕中来
当对话框被拖离屏幕时,可用下面代码使其回到屏幕中。

SendMessage(DM_REPOSITION);

注:它必须是顶端窗口且不是child窗口。

 Q. 如何给对话框添加或去掉最大/最小化按钮
在OnCreate()或OnInitDialog() 改变其显示风格既可。

int CTest_deleteDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CDialog::OnCreate(lpCreateStruct) == -1)

return -1;

// TODO: Add your specialized creation code here

SetWindowLong(this->m_hWnd,GWL_STYLE,

GetWindowLong(this->m_hWnd,GWL_STYLE) |

WS_MINIMIZEBOX | WS_MAXIMIZEBOX);

return 0;

}

或用:

ModifyStyle (NULL, WS_MAXIMIZEBOX);

 Q. 改变鼠标指针
可以在OnSetCursor中实现.

BOOL CTest_deleteDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)

{

// TODO: Add your message handler code here and/or call default

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

// Now we return instead of calling the base class

return 0;

// return CDialog::OnSetCursor(pWnd, nHitTest, message);

}

 Q. 改变对话框的前景和背景色
可以在InitInstance()中实现。

//红色背景、绿色前景

SetDialogBkColor(RGB(255,0,0),RGB(0,255,0));

 Q. 在任务条上不显示图标
先从CWinApp继承类中建立一个不显示的顶级窗口.

CFrameWnd *abc=new CFrameWnd();

abc->Create(0,0,WS_OVERLAPPEDWINDOW);

CNoTaskBarIconDlg dlg(abc);

m_pMainWnd = &dlg;

int nResponse = dlg.DoModal();

if (nResponse == IDOK)

{

}

else if (nResponse == IDCANCEL)

{

}

delete abc;

在 OnInitDialog中修改显示风格 WS_EX_APPWINDOW.

BOOL CNoTaskBarIconDlg::OnInitDialog()

{

CDialog::OnInitDialog();

ModifyStyleEx(WS_EX_APPWINDOW,0);

SetIcon(m_hIcon, TRUE);  // Set big icon

SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

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

}

 Q. 加入上、下文帮助
在 OnInitDialog 修改显示风格,加入上、下文HLP帮助显示.

BOOL HelpDialog::OnInitDialog()

{

ModifyStyleEx(0, WS_EX_CONTEXTHELP);

return CDialog::OnInitDialog();

}

重载OnHelpInfo(...),用显示相关帮助信息

BOOL HelpDialog::OnHelpInfo(HELPINFO* pHelpInfo)

{

short state = GetKeyState (VK_F1);

if (state < 0)   // F1 key is down, get help for the dialog

return CDialog::OnHelpInfo(pHelpInfo);

else

{    // F1 key not down, get help for specific control

if (pHelpInfo->dwContextId)

WinHelp (pHelpInfo->dwContextId,

HELP_CONTEXTPOPUP);

return TRUE;

}

}

C++ 如何有效地使用对话框的更多相关文章

  1. 一步步开发自己的博客 .NET版(10、前端对话框和消息框的实现)

    关于前端对话框.消息框的优秀插件多不胜数.造轮子是为了更好的使用轮子,并不是说自己造的轮子肯定好.所以,这个博客系统基本上都是自己实现的,包括日志记录.响应式布局.评论功能等等一些本可以使用插件的.好 ...

  2. jQuery遮罩层登录对话框

    用户登录是许多网站必备的功能.有一种方式就是不管在网站的哪个页面,点击登录按钮就会弹出一个遮罩层,显示用户登录的对话框.这用方式比较灵活方便.而现在扫描二维码登录的方式也是很常见,例如QQ.微信.百度 ...

  3. Android系统默认对话框添加图片

    开发工具Android Studio 今天公司UI要求软件对话框改成加图片的,以前没有做过,所以就学习了一下,废话不多说, 看效果: 创建XML文件dialog_lsit_item.xml <L ...

  4. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  5. 使用CSS3的box-shadow实现双透明遮罩层对话框

    box-shadow介绍 在我之前的一篇文章<从天猫和支付宝身上学习opcity与rgba>中,介绍了实现双透明遮罩层效果的两种方法,分别是opacity和rgba.他们需要分别依赖于不同 ...

  6. 10.JAVA之GUI编程弹出对话框Dialog

    在上节基础上添加对话框显示错误信息. 代码如下: /*弹出对话框显示错误信息,对话框一般不单独出现,一般依赖于窗体.*/ /*练习-列出指定目录内容*/ import java.awt.Button; ...

  7. C#WebBrowrse拦截下载对话框

    为了实现这个功能,可算是折腾不少时间,网上搜素出来的结果基本都是如何屏蔽警告对话框.后来请教一个技术大牛(程序员之窗的主要作者Starts_2000),他用C++实现了,他尝试了下C#也没有解决,就忙 ...

  8. 实现对gridview删除行时弹出确认对话框的一种简单方法

    在VS2008提供的GridView中我们可以直接添加一个CommandField删除列:<asp:CommandField ShowDeleteButton="True" ...

  9. 炫酷的jQuery对话框插gDialog

    js有alert,prompt和confirm对话框,不过不是很美体验也不是很好,用jQuery也能实现, 体验效果:http://hovertree.com/texiao/jquery/34/ 代码 ...

随机推荐

  1. Android图片加载与缓存开源框架:Android Glide

    <Android图片加载与缓存开源框架:Android Glide> Android Glide是一个开源的图片加载和缓存处理的第三方框架.和Android的Picasso库类似,个人感觉 ...

  2. [Python模式]策略模式

    策略模式 定义了算法族,分别封装起来,让它们之间可以互相替换.此模式让算法的变化独立于使用算法的客户. 作为动态语言,Python实现策略模式非常容易,只要所有算法提供相同的函数即可. import ...

  3. equals 与 ==

    Object类中,方法equals():boolean equals(Object obj) {   return this==obj;} == 比较两个变量的值是否相等,对于基本类型,==直接比较变 ...

  4. Java基础小总结

    1,Java事件处理机制 (1)三部分:事件源.事件(处理)对象.实现事件监听器: (2)事件处理程序:可以通过ActionEvent e,e.getSource确定是哪个事件触发了,然后通过类似JB ...

  5. BFC学习笔记

    BFC:块级格式化上下文 占用某一块的空间位置,主要用于清除内部浮动(防止因浮动脱离文档流引起的布局错乱),margin值的嵌套(之前写过一篇关于margin-top嵌套的解决方法),三列布局(占用空 ...

  6. swift项目中使用OC/C的方法

    假如有个OC类OCViewController : UIViewController类里有两个方法 //swift调用oc或c的混编是比较常用的,反过来的调用很少.这里只写了swift调用oc和c的方 ...

  7. Zookeeper源码编译为Eclipse工程(转)

    原文地址:http://blog.csdn.net/jiyiqinlovexx/article/details/41179293 为了深入学习ZooKeeper源码,首先就想到将其导入到Eclispe ...

  8. sqlite入门

    SQLite官网: https://www.sqlite.org/index.html 1. 下载请到https://www.sqlite.org/download.html下载相应平台的sqlite ...

  9. java中的static关键词

    以下来自:http://www.cnblogs.com/codc-5117/archive/2011/12/04/2275298.html Static基本规则:             (1)一个类 ...

  10. qsort函数用法【转】

    qsort函数用法 qsort 功 能: 使用快速排序例程进行排序  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(con ...