第六篇--MFC美化界面


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);
}
View Code

 

當然,對於不想要改變的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;
}
View Code

 

額外提示: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);
}
View Code

 

親測可用,捕捉鼠標按下與釋放。

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<HCURSOR>(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
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM