第六篇--MFC美化界面
阅读原文时间:2023年07月14日阅读:3

_**1.MFC如何设置背景颜色

**_

首先,为对话框添加WM_CTLCOLOR消息,方法为:右击Dialog窗口 --> Class Wizard --> Messages --> WM_CTLCOLOR --> Add Handler --> Edit Code

然后,在Dlg.h文件中添加成员变量CBrush m_brush;

接着,在之前Edit Code的位置,写上

HBRUSH CMFCInterfaceDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return m_brush;
}

最后,在Dlg.cpp文件的OnInitDialog()函数后面加上m_brush.CreateSolidBrush(RGB(255, 255, 102));//设置背景颜色为黄色。编译运行函数即可。

2.MFC如何设置背景图片

首先在网上下载一张Bitmap的图片,放在res文件夹下

接着在OnInitDialog()后面加上如下代码

   CString strBmpPath = _T(".\\res\\backgroundone.jpg");

CImage img;

img.Load(strBmpPath);

MoveWindow(0, 0, img.GetWidth(), img.GetHeight());

CBitmap bmpTmp;

bmpTmp.Attach(img.Detach());

m\_brush.CreatePatternBrush(&bmpTmp);

3.MFC修改可执行文件和标题栏图标

创建一个新工程,可以什么都不加。打开.rc,  创建或打开Icon资源(以下都以Icon为例)。    
单击工程窗口的资源视图标签,选中资源ID为IDR_MAINFRAME图标资源,然后按Delete键把它删除掉,当然也可以不删,具体后面会讲到。    
从资源菜单中选择Resource,然后选择Icon,添加资源,选中Icon类型,点击导入,此时会叫你选择本地的图片,记住必须选择.ioc格式的图片,否则导入失败。    
把新图标的资源ID改为IDI_ICON(也可以不改)。 具体做法如下:

有一点很重要,你改知道就是 打开Header Files下的Resource.h,找到Icon下的图标,系统默认是从128开始的,

#define IDR_MAINFRAME               128

#define IDR_ICONTETYPE             129(单/多文档程序的文档图标)

#define IDI_ICON1                            130

#define IDI_ICON2                            131

a.修改exe文件图标

VS2010生成的exe文件图标是用Icon下几个图标中value值最小的,顺序为IDR_MAINFRAME、IDR_ICONTETYPE、新加 的,所以想更改生成的exe文件图标,只要保证图标的value值是Icon下几个图标中最小的就可以了

  1. 导入自己的.ioc图片,并在Resource.h中,将自己加的icon资源Value改为最小,如下,因此不一定要删除IDR_MAINFRAME

#define IDR_MAINFRAME                   129

#define IDI_ICON1                               128(自己添加的icon)

  2. 然后编译运行,找到你的debug目录,你将看到图标已经更改的.exe执行文件,这种方法可以将任何你喜欢的图片做成.exe文件图标

b.修改标题栏图标

  1. 如上操作,导入自己喜爱的ico图片,编辑新加icon的ID,比如我新加的Icon资源ID为 IDI_ICON1

  22. 基于对话框的程序,在构造函数中有一句

   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    此时把IDR_MAINFRAME改为我的IDI_ICON1的图标名。

4. Button的绘制

首先,拖动几个Button到窗口,然后修改Button的Owner Draw属性为true

接着,右击Dialog窗口 --> Class Wizard --> Messages --> WM_DRAWITEM --> Add Handler --> Edit Code

void CMFCInterfaceDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default

//获得button标题  
//CString btnCaption = \_T("Dialog");  
CDC\* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

CString btnCaption = L"";  
//设置标题  
switch (lpDrawItemStruct->CtlID)  
{  
case ID1:  
    btnCaption = "1";  
    break;  
case ID2:  
    btnCaption = "2";  
    break;  
case ID3:  
    btnCaption = "3";  
    break;  
default:  
    ;  
}

CRect drawRect;  
//获得绘图DC  
//得到原Button的矩形大小  
drawRect.CopyRect(&(lpDrawItemStruct->rcItem));  
//绘制控件框架  
pDC->DrawFrameControl(&drawRect, DFC\_BUTTON, lpDrawItemStruct->CtlType);

//创建画刷  
CBrush pBrush;  
pBrush.CreateSolidBrush(RGB(100, 130, 10));  
//画矩形  
pDC->FillRect(drawRect, &pBrush);

//定义一个CRect用于绘制文本  
CRect textRect;  
//拷贝矩形区域  
textRect.CopyRect(&drawRect);  
//获得字符串尺寸  
CSize sz = pDC->GetTextExtent(btnCaption);  
//调整文本位置 居中  
textRect.top += (textRect.Height() - sz.cy) / 2;  
//设置文本背景透明  
pDC->SetBkMode(TRANSPARENT);  
//设置文本颜色  
pDC->SetTextColor(RGB(0, 0, 255));  
//绘制文本内容  
pDC->DrawText(btnCaption, &textRect, DT\_RIGHT | DT\_CENTER | DT\_BOTTOM);

CDialogEx::OnDrawItem(nIDCtl, lpDrawItemStruct);  

}

当然,对于不想要改变的Button,将Owner Draw属性设为false就行。

5.static text字体及颜色的绘制

颜色绘制:

在OnCTLColor中

HBRUSH CMFCInterfaceDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
//HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

if (pWnd == this)  
{  
    return m\_brush;  
}

if (pWnd->GetDlgCtrlID() == (IDC\_STATIC\_TITLE))//IDC\_STATIC\_TITLE:标题ID  
{  
    pDC->SetBkMode(TRANSPARENT);  
    pDC->SetTextColor(RGB(255, 251, 240));  
    return HBRUSH(GetStockObject(HOLLOW\_BRUSH));  
}

if (pWnd->GetDlgCtrlID() == (IDC\_STATIC))//IDC\_STATIC:文本ID  
{  
    pDC->SetBkMode(TRANSPARENT);  
    pDC->SetTextColor(RGB(0, 225, 225));  
    return HBRUSH(GetStockObject(HOLLOW\_BRUSH));  
}

return hbr;

//return m\_brush;  

}

额外提示:pDC->SetBkMode(TRANSPARENT);是设置文本背景透明。

字体绘制:

首先,在Dlg.h文件中声明Font变量CFont newFont;

然后,在OnInitDialog()函数后面加上以下代码

    CFont\* font;  
font = static\_title.GetFont();//获取CFont对象  
LOGFONT lf;  
font->GetLogFont(&lf);//获取LOGFONT结构体  
lf.lfHeight = -15;    //修改字体大小  
lf.lfItalic = TRUE;        //倾斜  
lf.lfWeight = 400;   //修改字体的粗细  
newFont.CreateFontIndirectW(&lf);//创建一个新的字体  
static\_title.SetFont(&newFont);    

6.MFC取消标题栏,以及自制标题栏

首先,去掉原本的标题栏,将属性Border设置为NONE。

然后,观察上面那张图片,上面那个矩形区域就可以当做标题栏。首先要做的就是为它添加一个退出按钮,就是那个叉叉。

1. 拖动一个button到矩形区域,自行调整大小以及位置,设置它的Caption为大写的X,哈哈哈,其实可以为Button贴图,不过本人为了方便,写个X冒充。将其ID设置为IDC_BUTTON_CLOSE。

2. 为这个Button添加函数,实现单击时关闭程序。双击Button进入代码编辑页面,输入以下代码

void CMFCInterfaceDlg::OnBnClickedButtonClose()
{
CDialog::OnOK();
}

此时,它的退出功能已经完成。但是运行时,可能发现Button的位置偏左,这时候,就可以用代码微调Button的位置,在OnInitDialog()函数后面加上以下代码

CRect rect;
GetDlgItem(IDC_BUTTON_CLOSE)->GetWindowRect(&rect);//获得空间的绝对坐标
ScreenToClient(&rect);//获得相对于主窗体的坐标
rect.OffsetRect(CSize(15, 0));//这里要是要移动的相对位置
GetDlgItem(IDC_BUTTON_CLOSE)->MoveWindow(rect);//移动到目标位置

数字可以自己调节。

另外关于Button贴图的,附上此链接:https://blog.csdn.net/u011711997/article/details/52551106

7. 如何实现标题栏的拖动功能

需要添加三个函数

void CTestDllDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ReleaseCapture();

CDialogEx::OnLButtonUp(nFlags, point);  

}

void CTestDllDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();

CDialogEx::OnLButtonDown(nFlags, point);  

}

void CTestDllDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

static CPoint PrePoint = CPoint(0, 0);  
if (MK\_LBUTTON == nFlags)  
{  
    if (point != PrePoint)  
    {  
        CPoint ptTemp = point - PrePoint;  
        CRect rcWindow;  
        GetWindowRect(&rcWindow);  
        rcWindow.OffsetRect(ptTemp.x, ptTemp.y);  
        MoveWindow(&rcWindow);  
        return;  
    }  
}  
PrePoint = point;

CDialogEx::OnMouseMove(nFlags, point);  

}

亲测可用,捕捉鼠标按下与释放。

cpp

// TestDllDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestDll.h"
#include "TestDllDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();

// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif

protected:  
virtual void DoDataExchange(CDataExchange\* pDX);    // DDX/DDV support

// Implementation
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()

// CTestDllDlg dialog

CTestDllDlg::CTestDllDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_TESTDLL_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CTestDllDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO1, m_combo);
DDX_Control(pDX, IDC_COMBO2, m_combo_sec);
DDX_Control(pDX, IDC_BUTTON_CLOSE, m_btnClose);
DDX_Control(pDX, IDC_STATIC_TITLE, static_title);
}

BEGIN_MESSAGE_MAP(CTestDllDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_EN_CHANGE(IDC_MFCEDITBROWSE1, &CTestDllDlg::OnEnChangeMfceditbrowse1)
ON_CBN_SELCHANGE(IDC_COMBO1, &CTestDllDlg::OnCbnSelchangeCombo1)
ON_CBN_SETFOCUS(IDC_COMBO1, &CTestDllDlg::OnCbnSetfocusCombo1)
ON_BN_CLICKED(OK, &CTestDllDlg::OnBnClickedOk)
ON_STN_CLICKED(result, &CTestDllDlg::OnStnClickedresult)
ON_BN_CLICKED(clear, &CTestDllDlg::OnBnClickedclear)
ON_CBN_SETFOCUS(IDC_COMBO2, &CTestDllDlg::OnCbnSetfocusCombo2)
ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CTestDllDlg::OnBnClickedButtonClose)
ON_WM_CTLCOLOR()
ON_WM_DRAWITEM()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_SIZE()
END_MESSAGE_MAP()

// CTestDllDlg message handlers

BOOL CTestDllDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM\_ABOUTBOX must be in the system command range.  
ASSERT((IDM\_ABOUTBOX & 0xFFF0) == IDM\_ABOUTBOX);  
ASSERT(IDM\_ABOUTBOX < 0xF000);

CMenu\* pSysMenu = GetSystemMenu(FALSE);  
if (pSysMenu != nullptr)  
{  
    BOOL bNameValid;  
    CString strAboutMenu;  
    bNameValid = strAboutMenu.LoadString(IDS\_ABOUTBOX);  
    ASSERT(bNameValid);  
    if (!strAboutMenu.IsEmpty())  
    {  
        pSysMenu->AppendMenu(MF\_SEPARATOR);  
        pSysMenu->AppendMenu(MF\_STRING, IDM\_ABOUTBOX, strAboutMenu);  
    }  
}

// 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

CString strBmpPath = \_T(".\\\\res\\\\backgroundone.jpg");

CImage img;

img.Load(strBmpPath);

MoveWindow(0, 0, img.GetWidth(), img.GetHeight());

CBitmap bmpTmp;

bmpTmp.Attach(img.Detach());

m\_brush.CreatePatternBrush(&bmpTmp);

CRect rect\_close;  
GetDlgItem(IDC\_BUTTON\_CLOSE)->GetWindowRect(&rect\_close);//获得空间的绝对坐标  
ScreenToClient(&rect\_close);//获得相对于主窗体的坐标  
//rect.OffsetRect(CSize(5, 0));//这里要是要移动的相对位置  
rect\_close.OffsetRect(CSize(17, 0));//这里要是要移动的相对位置  
GetDlgItem(IDC\_BUTTON\_CLOSE)->MoveWindow(rect\_close);//移动到目标位置

CFont\* font;  
font = static\_title.GetFont();//获取CFont对象  
LOGFONT lf;  
font->GetLogFont(&lf);//获取LOGFONT结构体  
lf.lfHeight = -15;    //修改字体大小  
lf.lfItalic = TRUE;        //倾斜  
lf.lfWeight = 400;   //修改字体的粗细  
newFont.CreateFontIndirectW(&lf);//创建一个新的字体  
static\_title.SetFont(&newFont);

m\_combo.AddString(L"Add");  
m\_combo.AddString(L"Delete");  
m\_combo.AddString(L"Update");  
m\_combo.AddString(L"Query");  
m\_combo.SetCurSel(0);

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

}

void CTestDllDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}

// 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 CTestDllDlg::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();  
    //CDialogEx::OnPaint();  
}  

}

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

void CTestDllDlg::OnEnChangeEdit1()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialogEx::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO:  Add your control notification handler code here  

}

char* cstringTochar(CString str_tmp) {

char \*ptr;  
CString str;  
str = str\_tmp;  

#ifdef _UNICODE
LONG len;
len = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
ptr = new char[len + 1];
memset(ptr, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, str, -1, ptr, len + 1, NULL, NULL);
#else
ptr = new char[str.GetAllocLength() + 1];
sprintf(ptr, _T("%s"), str);
#endif
return ptr;
}

void CTestDllDlg::OnEnChangeMfceditbrowse1()
{

// TODO:  If this is a RICHEDIT control, the control will not  
// send this notification unless you override the CDialogEx::OnInitDialog()  
// function and call CRichEditCtrl().SetEventMask()  
// with the ENM\_CHANGE flag ORed into the mask.

// TODO:  Add your control notification handler code here

//CString strFile = \_T("");  
//CFileDialog dlgFile(TRUE, NULL, NULL, OFN\_HIDEREADONLY, \_T("Describe Files (\*.ini)|\*.ini|All Files (\*.\*)|\*.\*||"), NULL);  
//if (dlgFile.DoModal()) {  
//    strFile = dlgFile.GetPathName();  
//}  
//SetDlgItemText(path, strFile);  
//SetDlgItemText(key, L"aaa");  
m\_combo\_sec.ResetContent();//清空所有ComboBox的项目  
CString path\_cstr;  
GetDlgItemText(IDC\_MFCEDITBROWSE1, path\_cstr);  
char\* path\_str;  
path\_str = cstringTochar(path\_cstr);

HINSTANCE hInst;  
hInst = LoadLibrary(\_T("IniDll.dll"));  
typedef int(\*Getsec\_num)(char\* path\_str);  
Getsec\_num getsec\_num = (Getsec\_num)GetProcAddress(hInst, "GetSections\_num");  
int num = getsec\_num(path\_str);

char\* sec\_set;  
typedef char\*(\*Getsec)(char\* path\_str, int num);  
Getsec getsec = (Getsec)GetProcAddress(hInst, "GetSections");  
for (int i = 1; i < num + 1; i++) {  
    sec\_set = getsec(path\_str, i);  
    CString value\_cstr\_tmp(sec\_set);  
    m\_combo\_sec.AddString(value\_cstr\_tmp);  
    //m\_combo.SetCurSel(0);  
}  
m\_combo\_sec.SetCurSel(-1);  
FreeLibrary(hInst);  
//SetDlgItemText(key, path\_cstr);  

}

void CTestDllDlg::OnCbnSelchangeCombo1()
{
// TODO: Add your control notification handler code here
//CString strPre, strNew;
////GetDlgItem(IDC_COMBO1)->GetWindowTextW(m_paramname);//改变前的文本
////SetDlgItemText(path, m_paramname);
//int nSel = m_combo.GetCurSel();
//m_combo.GetLBText(nSel, strNew);
////SetDlgItemText(path, strNew);
////m_combo.GetWindowText(strPre);
//if (!strNew.CompareNoCase(L"Query")) {
// SetDlgItemText(value, strNew);
// //query_page.DoModal();
// //query_page.ChangeProc(GetDlgItem(IDC_COMBO1)->GetSafeHwnd());
//}
GetDlgItem(value)->EnableWindow(TRUE);
CString strNew_change;
int nSel_change = m_combo.GetCurSel();
m_combo.GetLBText(nSel_change, strNew_change);
if (!strNew_change.CompareNoCase(L"Query")) {
GetDlgItem(value)->EnableWindow(FALSE);
}
}

void CTestDllDlg::OnCbnSetfocusCombo1()
{
//TODO: Add your control notification handler code here
//m_combo.ResetContent();//清空所有ComboBox的项目
//m_combo.AddString(L"Add");
//m_combo.AddString(L"Delete");
//m_combo.AddString(L"Update");
//m_combo.AddString(L"Query");

//int nSel = m\_combo.GetCurSel();  
//m\_combo.SetCurSel(nSel);  

}

void CTestDllDlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
CString strNew;
int nSel = m_combo.GetCurSel();
m_combo.GetLBText(nSel, strNew);

CString path\_cstr, sec\_cstr, key\_cstr, value\_cstr;  
char\* path\_str;  
char\* sec\_str;  
char\* key\_str;  
//GetDlgItemText(path, path\_cstr);//获取指定ID的编辑框内容  
GetDlgItemText(IDC\_MFCEDITBROWSE1, path\_cstr);  
if (!strNew.CompareNoCase(L"Query")) {  
    int nSel\_sec = m\_combo\_sec.GetCurSel();  
    m\_combo\_sec.GetLBText(nSel\_sec, sec\_cstr);  
}  
else {  
    m\_combo\_sec.GetWindowText(sec\_cstr);  
}  
//GetDlgItemText(section, sec\_cstr);//获取指定ID的编辑框内容  
GetDlgItemText(key, key\_cstr);//获取指定ID的编辑框内容  
path\_str = cstringTochar(path\_cstr);  
sec\_str = cstringTochar(sec\_cstr);  
key\_str = cstringTochar(key\_cstr);

if (!strNew.CompareNoCase(L"Query")) {  
    GetDlgItem(value)->EnableWindow(TRUE);  
    //SetDlgItemText(value, strNew);  
    HINSTANCE hInst;  
    hInst = LoadLibrary(L"IniDll.dll");  
    typedef char\*(\*Read)(char\* sec\_str, char\* key\_str, char\* path\_str);  
    Read read\_string = (Read)GetProcAddress(hInst, "Ini\_Read");  
    //Read read\_string = (Read)GetProcAddress(hInst, (LPCSTR)MAKEINTRESOURCE(2));  
    char\* a = read\_string(sec\_str, key\_str, path\_str);  
    //char\* a = Ini\_Read(sec\_str, key\_str, path\_str);  
    CString value\_cstr\_tmp(a);

    SetDlgItemText(value, value\_cstr\_tmp);

    //SetDlgItemText(result, L"查询成功");//获取指定ID的编辑框内容  
    FreeLibrary(hInst);  
    AfxMessageBox(\_T("查询成功!"));  
    GetDlgItem(value)->EnableWindow(FALSE);  
}  
else if (!strNew.CompareNoCase(L"Add")) {  
    GetDlgItemText(value, value\_cstr);  
    char\* value\_str = new char\[1024\];  
    value\_str = cstringTochar(value\_cstr);

    HINSTANCE hInst;  
    hInst = LoadLibrary(\_T("IniDll.dll"));  
    typedef void(\*Write)(char\* sec\_str, char\* key\_str, char\* value\_str, char\* path\_str);  
    Write write = (Write)GetProcAddress(hInst, "Ini\_Write");  
    write(sec\_str, key\_str, value\_str, path\_str);  
    //SetDlgItemText(result, L"添加成功");//获取指定ID的编辑框内容  
    FreeLibrary(hInst);  
    AfxMessageBox(\_T("添加成功!"));  
}  
else if (!strNew.CompareNoCase(L"Update")) {  
    GetDlgItemText(value, value\_cstr);  
    char\* value\_str = new char\[1024\];  
    value\_str = cstringTochar(value\_cstr);

    HINSTANCE hInst;  
    hInst = LoadLibrary(\_T("IniDll.dll"));  
    typedef void(\*Write)(char\* sec\_str, char\* key\_str, char\* value\_str, char\* path\_str);  
    Write write = (Write)GetProcAddress(hInst, "Ini\_Write");  
    write(sec\_str, key\_str, value\_str, path\_str);  
    //SetDlgItemText(result, L"修改成功");//获取指定ID的编辑框内容  
    FreeLibrary(hInst);  
    AfxMessageBox(\_T("修改成功!"));  
}  
else if (!strNew.CompareNoCase(L"Delete")) {  
    //GetDlgItemText(value, value\_cstr);  
    //char\* value\_str = new char\[1024\];  
    //value\_str = cstringTochar(value\_cstr);

    if (!key\_cstr.CompareNoCase(L"")) {  
        HINSTANCE hInst;  
        hInst = LoadLibrary(\_T("IniDll.dll"));  
        typedef void(\*Del\_Sec)(char\* sec\_str, char\* path\_str);  
        Del\_Sec del\_sec = (Del\_Sec)GetProcAddress(hInst, "Ini\_Del\_Sec");  
        del\_sec(sec\_str, path\_str);  
        FreeLibrary(hInst);  
        AfxMessageBox(\_T("删除成功!"));  
    }  
    else {  
        HINSTANCE hInst;  
        hInst = LoadLibrary(\_T("IniDll.dll"));  
        typedef void(\*Del\_Key)(char\* sec\_str, char\* key\_str, char\* path\_str);  
        Del\_Key del\_key = (Del\_Key)GetProcAddress(hInst, "Ini\_Del\_Key");  
        del\_key(sec\_str, key\_str, path\_str);  
        FreeLibrary(hInst);  
        AfxMessageBox(\_T("删除成功!"));  
    }  
}

}

void CTestDllDlg::OnStnClickedresult()
{
// TODO: Add your control notification handler code here
}

void CTestDllDlg::OnBnClickedclear()
{
// TODO: Add your control notification handler code here
SetDlgItemText(IDC_MFCEDITBROWSE1, L"");
m_combo_sec.ResetContent();//清空所有ComboBox的项目
SetDlgItemText(key, L"");
SetDlgItemText(value, L"");
SetDlgItemText(result, L"");
}

void CTestDllDlg::OnBnClickedbutton()
{
// TODO: Add your control notification handler code here
}

void CTestDllDlg::OnCbnSetfocusCombo2()
{
// TODO: Add your control notification handler code here
}

void CTestDllDlg::OnBnClickedButtonClose()
{
// TODO: Add your control notification handler code here
CDialog::OnOK();
}

HBRUSH CTestDllDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO:  Change any attributes of the DC here

// TODO:  Return a different brush if the default is not desired

if (pWnd == this)  
{  
    return m\_brush;  
}

if (pWnd->GetDlgCtrlID() == (IDC\_STATIC\_TITLE))  
{  
    //MessageBox(\_T("static text"));  
    pDC->SetBkMode(TRANSPARENT);  
    pDC->SetTextColor(RGB(255, 251, 240));  
    return HBRUSH(GetStockObject(HOLLOW\_BRUSH));  
}

if (pWnd->GetDlgCtrlID() == (IDC\_STATIC))  
{  
    //MessageBox(\_T("static text"));  
    pDC->SetBkMode(TRANSPARENT);  
    //pDC->SetTextColor(RGB(0, 225, 225));  
    return HBRUSH(GetStockObject(HOLLOW\_BRUSH));  
}

return hbr;  

}

void CTestDllDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default

////获得button标题  
//CString btnCaption = \_T("Dialog");  
//CDC\* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

CString btnCaption = L"";  
//设置标题  
switch (lpDrawItemStruct->CtlID)  
{  
case OK:  
    btnCaption = "OK";  
    break;  
case clear:  
    btnCaption = "Clear";  
    break;  
default:  
    ;  
}  
CDC\* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

CRect drawRect;  
//获得绘图DC  
//得到原Button的矩形大小  
drawRect.CopyRect(&(lpDrawItemStruct->rcItem));  
//绘制控件框架  
pDC->DrawFrameControl(&drawRect, DFC\_BUTTON, lpDrawItemStruct->CtlType);

//创建画刷  
CBrush pBrush;  
//pBrush.CreateSolidBrush(RGB(100, 130, 10));  
pBrush.CreateSolidBrush(RGB(0, 128, 255));  
//画矩形  
pDC->FillRect(drawRect, &pBrush);

//定义一个CRect用于绘制文本  
CRect textRect;  
//拷贝矩形区域  
textRect.CopyRect(&drawRect);  
//获得字符串尺寸  
CSize sz = pDC->GetTextExtent(btnCaption);  
//调整文本位置 居中  
textRect.top += (textRect.Height() - sz.cy) / 2;  
//设置文本背景透明  
pDC->SetBkMode(TRANSPARENT);  
//设置文本颜色  
pDC->SetTextColor(RGB(0, 0, 255));  
//绘制文本内容  
pDC->DrawText(btnCaption, &textRect, DT\_RIGHT | DT\_CENTER | DT\_BOTTOM);

CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);

//CDialogEx::OnDrawItem(nIDCtl, lpDrawItemStruct);  

}

void CTestDllDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
ReleaseCapture();

CDialogEx::OnLButtonUp(nFlags, point);  

}

void CTestDllDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetCapture();

CDialogEx::OnLButtonDown(nFlags, point);  

}

void CTestDllDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

static CPoint PrePoint = CPoint(0, 0);  
if (MK\_LBUTTON == nFlags)  
{  
    if (point != PrePoint)  
    {  
        CPoint ptTemp = point - PrePoint;  
        CRect rcWindow;  
        GetWindowRect(&rcWindow);  
        rcWindow.OffsetRect(ptTemp.x, ptTemp.y);  
        MoveWindow(&rcWindow);  
        return;  
    }  
}  
PrePoint = point;

CDialogEx::OnMouseMove(nFlags, point);  

}

void CTestDllDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);

//CDialogEx::OnSize(nType, cx, cy);

// TODO: Add your message handler code here  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章