Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 6 của 6
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi Tab
    Bạn xử lý thông điệp WM_CTLCOLOR rồi vẽ lại màu nền theo ý muốn.
    Đối với MFC, CCombobox kế thừa từ CWnd nên bạn nạp chồng OnCtlColor của CWnd rồi xử lý việc vẽ.
    mình mới học nên bạn nói thế khó quá ... sau 1 time google thì thành quả của mình như sau :

    -------------------------------------------------------Tutorial1.cpp------------------------------------------------


    // Tutorial1.cpp : Defines the class behaviors for the application.
    //

    #include "stdafx.h"
    #include "Tutorial1.h"
    #include "Tutorial1Dlg.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif

    // CTutorial1App

    BEGIN_MESSAGE_MAP(CTutorial1App, CWinApp)
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
    END_MESSAGE_MAP()


    // CTutorial1App construction

    CTutorial1App::CTutorial1App()
    {
    // support Restart Manager
    m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }


    // The one and only CTutorial1App object

    CTutorial1App theApp;


    // CTutorial1App initialization

    BOOL CTutorial1App::InitInstance()
    {
    // InitCommonControlsEx() is required on Windows XP if an application
    // manifest specifies use of ComCtl32.dll version 6 or later to enable
    // visual styles. Otherwise, any window creation will fail.
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // Set this to include all the common control classes you want to use
    // in your application.
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();


    AfxEnableControlContainer();

    // Create the shell manager, in case the dialog contains
    // any shell tree view or shell list view controls.
    CShellManager *pShellManager = new CShellManager;

    // Standard initialization
    // If you are not using these features and wish to reduce the size
    // of your final executable, you should remove from the following
    // the specific initialization routines you do not need
    // Change the registry key under which our settings are stored
    // TODO: You should modify this string to be something appropriate
    // such as the name of your company or organization
    SetRegistryKey(_T("Local AppWizard-Generated Applications"));

    CTutorial1Dlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    // dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    // dismissed with Cancel
    }

    // Delete the shell manager created above.
    if (pShellManager != NULL)
    {
    delete pShellManager;
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    // application, rather than start the application's message pump.
    return FALSE;
    }

    ------------------------------------------------------------------------------------------------------------------------
    ----------------------------------------------------------Tutorial1Dlg.cpp----------------------------------------------------------


    // Tutorial1Dlg.cpp : implementation file
    //

    #include "stdafx.h"
    #include "Tutorial1.h"
    #include "Tutorial1Dlg.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
    enum { IDD = IDD_ABOUTBOX };

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

    // Implementation
    protected:
    DECLARE_MESSAGE_MAP()


    };

    CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
    {
    }

    void CAboutDlg:[IMG]images/smilies/biggrin.png[/IMG]oDataExchange(CDataExchange* pDX)
    {
    CDialogEx:[IMG]images/smilies/biggrin.png[/IMG]oDataExchange(pDX);
    }

    BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
    END_MESSAGE_MAP()

    CTutorial1Dlg::CTutorial1Dlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(CTutorial1Dlg::IDD, pParent)
    {
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_pEditBkBrush = new CBrush(RGB(0, 0, 0));
    }

    void CTutorial1Dlg:[IMG]images/smilies/biggrin.png[/IMG]oDataExchange(CDataExchange* pDX)
    {
    CDialogEx:[IMG]images/smilies/biggrin.png[/IMG]oDataExchange(pDX);
    }

    BEGIN_MESSAGE_MAP(CTutorial1Dlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_WM_ERASEBKGND()
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_BN_CLICKED(IDOK, &CTutorial1Dlg::OnBnClickedOk)
    ON_BN_CLICKED(IDCANCEL, &CTutorial1Dlg::OnBnClickedCancel)
    END_MESSAGE_MAP()


    // CTutorial1Dlg message handlers

    BOOL CTutorial1Dlg::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 != NULL)
    {
    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
    CComboBox *CBox = new CComboBox;

    CBox->Create(WS_CHILD | WS_VISIBLE|CBS_DROPDOWNLIST|CTLCOLOR_EDIT|CTLCOLOR _MSGBOX|CTLCOLOR_LISTBOX,
    CRect(10, 10, 222, 150),
    this, 0x1448);

    //OnCtlColor(0, CBox, CTLCOLOR_EDIT);

    CBox->CenterWindow();

    // CTutorial1Dlg::OnCtlColor( 0, CBox, CTLCOLOR_EDIT);
    //CBox->

    CBox->AddString(L"Phong");
    CBox->AddString(L"Hoang");
    CBox->AddString(L"Hai");



    CBox->InsertString(0, L"Hello");

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

    void CTutorial1Dlg::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 CTutorial1Dlg::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();
    }
    }

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



    void CTutorial1Dlg::OnBnClickedOk()
    {
    // TODO: Add your control notification handler code here
    CDialogEx::OnOK();
    }


    void CTutorial1Dlg::OnBnClickedCancel()
    {
    // TODO: Add your control notification handler code here
    CDialogEx::OnCancel();
    }

    HBRUSH CTutorial1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    // Call the base class implementation first! Otherwise, it may
    // undo what we're trying to accomplish here.
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    CBrush m_brush;
    // Are we painting the IDC_MYSTATIC control? We can use
    // CWnd::GetDlgCtrlID() to perform the most efficient test.
    //if (pWnd->GetDlgCtrlID() == CTLCOLOR_EDIT)
    //{
    // switch (nCtlColor) {

    //case CTLCOLOR_EDIT:
    //case CTLCOLOR_MSGBOX:
    // Set the text color to red
    pDC->SetTextColor(RGB(255, 0, 330));
    pDC->SetBkColor(RGB(0, 0, 0));

    // Set the background mode for text to transparent
    // so background will show thru.
    // pDC->SetBkMode(TRANSPARENT);
    // default:
    //return hbr;
    // Return handle to our CBrush object

    //}
    // }
    hbr = m_brush;
    return hbr;
    }

    BOOL CTutorial1Dlg::OnEraseBkgnd(CDC* pDC)
    {
    CRect rect;
    GetClientRect(&rect);
    CBrush myBrush(RGB(0, 255, 0)); // dialog background color
    CBrush *pOld = pDC->SelectObject(&myBrush);
    BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
    pDC->SelectObject(pOld); // restore old brush
    return bRes; // CDialog::OnEraseBkgnd(pDC);
    }
    //CTutorial1Dlg dialog
    void CTutorial1Dlg::OnDestroy()
    {
    CDialog::OnDestroy();

    // Free the space allocated for the background brush
    delete m_pEditBkBrush;
    }
    /* close

    HBRUSH CTutorial1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    switch (nCtlColor) {

    case CTLCOLOR_EDIT:
    case CTLCOLOR_MSGBOX:
    // Set color to green on black and return the background brush.
    pDC->SetTextColor(RGB(255, 0, 330));
    pDC->SetBkColor(RGB(0, 0, 0));
    return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

    default:
    return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }
    }

    */

    ---------------------------------------------------------------------------------------------------------
    ------------------------------------------------Tutorial1Dlg.h----------------------------------------


    // Tutorial1Dlg.h : header file
    //

    #pragma once


    // CTutorial1Dlg dialog
    class CTutorial1Dlg : public CDialogEx
    {
    // Construction
    public:
    CTutorial1Dlg(CWnd* pParent = NULL); // standard constructor
    CBrush* m_pEditBkBrush;
    // Dialog Data
    enum { IDD = IDD_TUTORIAL1_DIALOG };

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


    // Implementation
    protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
    public:
    afx_msg void OnBnClickedOk();
    afx_msg void OnBnClickedCancel();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnDestroy();
    afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    };

    -----------------------------------------------------------------------------------------------------------------

    chừng này mới đổi được màu background nền và chữ + nền chữ của comboBox
    làm sao để đổi đc cả comboBox nữa vậy ???

    Thanks

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn mới học MFC ko biết đã đọc qua Win32API chưa. Bạn nên học từ từ, đừng nhảy cóc nếu không sẽ bị tẩu hỏa nhập ma đấy. Bạn nên dành thời gian đọc những thứ căn bản trước, nắm chắc thì sẽ không phải mất thời gian học lại sau này.

  3. #3
    Trích dẫn Gửi bởi Tab
    Bạn mới học MFC ko biết đã đọc qua Win32API chưa. Bạn nên học từ từ, đừng nhảy cóc nếu không sẽ bị tẩu hỏa nhập ma đấy. Bạn nên dành thời gian đọc những thứ căn bản trước, nắm chắc thì sẽ không phải mất thời gian học lại sau này.
    bạn có thể chỉ cho mình trc về cái này được không ??? thanks bạn

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Đổi màu comboBox trong MFC như thế nào?

    e đang tập tành học MFC tí ... có ai biết đổi màu comboBox như nào không cho e cái sample với

    Đổi màu cả comboBox chứ k phải mỗi chữ với màu nền của chữ đâu nhé

    Thanks

    [IMG]images/smilies/smile.png[/IMG]

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn xử lý thông điệp WM_CTLCOLOR rồi vẽ lại màu nền theo ý muốn.
    Đối với MFC, CCombobox kế thừa từ CWnd nên bạn nạp chồng OnCtlColor của CWnd rồi xử lý việc vẽ.

  6. #6
    Ngày tham gia
    Sep 2015
    Đang ở
    Số 60 Thái Hà, Đống Đa, Hà Nội
    Bài viết
    0
    Bạn tạo MyCombo dẫn xuất từ CCombobox, sau đó nạp chồng OnCtlColor và vẽ nền thôi.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •