MFC 静态文本框使用/更改button背景色/不使能button/设置标题/不显示控件/窗口最大最小化

来源:互联网 发布:盘古网络为什么总招人 编辑:程序博客网 时间:2024/05/18 02:28

1 设置标题

SetWindowText(_T("Lenovo Mirage AR Headset FW Upgrade Tool"));

2 不显示某个控件

GetDlgItem(IDC_STATIC)->ShowWindow(SW_HIDE);

3 设置对话

CRect temprect(0, 0, 728, 477); CWnd::SetWindowPos(NULL, 0, 0,
temprect.Width(), temprect.Height(), SWP_NOZORDER | SWP_NOMOVE);

4 设置控件位置

//升级完成后的对号位置 GetDlgItem(IDC_STATIC_Upgrade_Complete)->SetWindowPos(0,
318, 161, 0, 0, SWP_NOSIZE);
//ok BUTTON的位置
GetDlgItem(IDC_BUTTON_OK_Comple)->SetWindowPos(0, 570, 406, 0, 0,
SWP_NOSIZE);

5 现窗口的最大化和最小化
将对话框的Border选为None,添加两个按钮,min和close,将按钮的bitmap设置为TRUE;用于添加最小和最大的背景图片
双击min按钮事件中添加:

CWnd::ShowWindow(SW_SHOWMINIMIZED);`

双击close按钮事件中添加:

exit(0);  

6、全局变量的使用,这样就可以在对话框中调用另一个对话框,显示不同的值;同一个对话框根据不同情况显示不同的信息。
在stdafx.cpp中加入:

int myInt;
CString versionInformation;

然后在stdafx.h中加入:

extern int myInt
extern CString versionInformation;

这样定义以后无论在什么文件中都是可见的.
7 Picture Control添加bmp图片:
先将bmp图片放到工程的res文件夹下,然后Bitmap右键–>添加资源–>选择Bitmap–>导入
这里写图片描述

在控件选项中的type选为Bitmap,image则选择填入照片的名字

这里写图片描述

8、静态文本框居中显示/字符串显示/字体设置(Static Text)
居中:选择这个控件–>属性
Align Text –> Center 左右居中
Center Image –> True 上下居
静态文本框自动换行:
Align Text –> Center
Center Image –> False
显示的字符:

CStatic * p1 = (CStatic *)GetDlgItem(IDC_STATIC);
p1->SetWindowText((L”USB connected”));

字体设置:

font.CreatePointFont(120,_T("微软雅黑"));GetDlgItem(IDC_STATIC)->SetFont(&font);

9 静态文本框的背景颜色设置为透明
在对话框类向导中添加消息OnCtlColor(点击右键-类向导)
这里写图片描述

在响应函数中添加如下代码,注意静态文本框的id需要改变:

HBRUSH CDiaglogDeviceNoCon::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  {      HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);     // TODO:  在此更改 DC 的任何特性      if(pWnd->GetDlgCtrlID()== IDC_STATIC_device)      {          //pDC->SetBkColor(RGB(255,0,0));//指定背景颜色          pDC->SetTextColor(RGB(0,0,0));//文字的颜色          pDC->SetBkMode(TRANSPARENT);//设置为透明,和下一句一起使用          hbr = (HBRUSH)GetStockObject(NULL_BRUSH);//获取画笔颜色混合后的画笔,完成透明      }      // TODO:  如果默认的不是所需画笔,则返回另一个画笔      return hbr;  }  

设置文件框的背景色后,如果要动态的刷新文件框的内容,则会出现重叠现象;因为背景色已确定,每次都要已文本的自带的颜色画,背景色不能更新。
10 button更改背景色 button不可用
button不可用:
GetDlgItem(IDC_Upgrade_Btn)->EnableWindow(FALSE);
更改背景颜色:需要先将owner draw设置为true,否则无法显示背景色
类向导添加消息–>WM_DRAWITEM

void CSkinDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) {    if(nIDCtl==IDC_BUTTON1) //checking for the button    {           //Button属性为自画     //Button属性为自画                CDC dc;            RECT rect;            dc.Attach(lpDrawItemStruct->hDC); // Get the Button DC to CDC             rect = lpDrawItemStruct->rcItem; //Store the Button rect to our local rect.                                              //dc.Draw3dRect(&rect, RGB(255, 255, 255), RGB(0, 0, 0));            dc.Draw3dRect(&rect, RGB(0, 0, 0), RGB(0, 0, 0));            dc.FillSolidRect(&rect, RGB(0, 0, 0));//Here you can define the required color to appear on the Button.            dc.SetBkColor(RGB(1, 1, 1)); //Setting the Text Background color            dc.SetTextColor(RGB(254, 254, 254)); //Setting the Text Color             TCHAR buffer[MAX_PATH]; //To store the Caption of the button.            ZeroMemory(buffer, MAX_PATH); //Intializing the buffer to zero            ::GetWindowText(lpDrawItemStruct->hwndItem, buffer, MAX_PATH); //Get the Caption of Button Window             dc.DrawText(buffer, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);//Redraw the Caption of Button Window             dc.Detach(); // Detach the Button DC    }     CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);}
阅读全文
0 0
原创粉丝点击