CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2006
    Posts
    23

    MFC Dialog as Mainwnd, Setting DWStyle

    My question is ultra simple -

    When I have a dialog box as the main window, how do I specify the DWSTYLE of the window it shows?

    Project_Main_Header.h

    Code:
    #include <afxwin.h>
    #include <afxcmn.h>
    #ifndef _DIALOGASMAINWINDOW_H_
    #define _DIALOGASMAINWINDOW_H_
    
    
    class CMyApp : public CWinApp
    {
    public:
    	virtual BOOL InitInstance();
    };
    
    class CMainDialog : public CDialog
    {
    public:
    	//Constructor for the main dialog
    	CMainDialog();
    
    	//Function Prototype Declarations for Buttons
    	void OnDialogExit();
    	void OnInsertAbove();
    	void OnInsertBelow();
    	void OnAddTask();
    	void OnDeleteTask();
    	
    	//Function Prototype Declaration for Initialization
    	//of ListCtrl
    	void OnInitListCtrl();
    protected:
    	virtual void DoDataExchange(CDataExchange *pDX);
    	virtual BOOL OnInitDialog();
    	virtual void PostNcDestroy();
    	afx_msg void OnPaint();
    	DECLARE_MESSAGE_MAP()
    private:
    	CListCtrl *pListCtrl;
    };
    
    
    
    #endif

    Project_Main_Code.cpp

    Code:
    #include "Project_Main_Header.h"
    #include "resource.h"
    
    CMyApp myApp;
    
    BOOL CMyApp::InitInstance()
    {
    	//Create dialog box as the main window
    	m_pMainWnd = new CMainDialog;
    	m_pMainWnd->ShowWindow(m_nCmdShow);
    	m_pMainWnd->UpdateWindow();
    	return TRUE;
    }
    
    
    BEGIN_MESSAGE_MAP(CMainDialog,CDialog)
    	
    	//Message Handlers for the button pressed
    	ON_BN_CLICKED(IDC_BUTTON_EXIT,OnDialogExit)
    	ON_BN_CLICKED(IDC_BUTTON_ADD,OnAddTask)
    	ON_BN_CLICKED(IDC_BUTTON_DELETE,OnDeleteTask)
    	ON_BN_CLICKED(IDC_BUTTON_INSA,OnInsertAbove)
    	ON_BN_CLICKED(IDC_BUTTON_INSB,OnInsertBelow)
    
    	//Message Handler to repaint the dialog box
    	ON_WM_PAINT()
    END_MESSAGE_MAP()
    
    CMainDialog::CMainDialog()
    {
    	//Constructor to create the dialog box
    	Create(IDD_MAIN_DIALOG);
    	
    }
    
    BOOL CMainDialog::OnInitDialog()
    {
    	CDialog::OnInitDialog();
    	
    	//Initialization function call for ListCtrl.
    	OnInitListCtrl(); 
    	return TRUE;
    }
    
    void CMainDialog::DoDataExchange(CDataExchange *pDX)
    {
    }
    
    void CMainDialog::OnInitListCtrl()
    {
    	//Retrieve Dialog item for List Ctrl
    	//And store it in pListCtrl
    	pListCtrl = (CListCtrl*) GetDlgItem(IDC_LIST_TASK);
    
    	//Insert all columns displayed on the list control
    	pListCtrl->InsertColumn(0,"Start Time",LVCFMT_LEFT,100);
    	pListCtrl->InsertColumn(1,"Month",LVCFMT_CENTER,100);
    	pListCtrl->InsertColumn(2,"Date",LVCFMT_CENTER,100);
    	pListCtrl->InsertColumn(3,"Day",LVCFMT_CENTER,100);
    	pListCtrl->InsertColumn(4,"Year",LVCFMT_CENTER,100);
    	pListCtrl->InsertColumn(5,"Title",LVCFMT_CENTER,270);
    
    }
    
    void CMainDialog::OnPaint()
    {
    	CPaintDC dc (this);	
    }
    
    void CMainDialog::OnDialogExit()
    {
    	DestroyWindow();
    }
    
    void CMainDialog::OnInsertAbove()
    {
    }
    
    void CMainDialog::OnInsertBelow()
    {
    }
    
    void CMainDialog::OnAddTask()
    {
    }
    
    void CMainDialog::OnDeleteTask()
    {
    }
    
    void CMainDialog::PostNcDestroy()
    {
    	delete this;
    }

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: MFC Dialog as Mainwnd, Setting DWStyle

    Usually, it's easiest to change styles in the resource editor.

    What styles are you trying to set?

  3. #3
    Join Date
    Oct 2006
    Posts
    23

    Re: MFC Dialog as Mainwnd, Setting DWStyle

    One without the exit button (X mark) on the top-right corner of my dialog box.

    [Edit]

    And as you mentioned, that was the easiest method to go by, thanks ^^

    While I'm here, I want to ask another question -

    How do I manipulate the DDX controls to disable / enable buttons?

    What I mean is, I have a CEdit object with multiline enabled, and I want to disable the "OK" Button if the Editbox is empty, and enable the "OK" Button as soon as the user starts typing something in the box.

    [Second Edit]

    Whoa.. actually the Resource Compiler did not have the "exact" option of the border line. I still want the title bar showing, but the "x" mark gone. How do I do this?
    Last edited by l46kok; May 3rd, 2008 at 03:35 PM.

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: MFC Dialog as Mainwnd, Setting DWStyle

    There are several approaches. One of them is to handle EN_CHANGE or EN_UPDATE from edit control and toggle enabled state of the button.

    Other (I think better) would be to handle WM_KICKIDLE and call UpdateDialogControls from it. Use ON_UPDATE_COMMAND_UI macro to map handler for a button update. Toggle enabled state in this update.

    As for “x” is not realy X; it is a button that if clicked closes window.
    You cannot remove this button unless you override WM_NCPAINT.

    You can disable this button by removing WS_SYSMENU. That will remove icon from a title bar.
    If you want to keep an icon, remove SC_CLOSE command from a system menu. This will disable close button.

    [EDIT]
    WM_KICKIDLE not a WM_LICKIDLE
    Last edited by JohnCz; May 4th, 2008 at 06:48 AM.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    Oct 2006
    Posts
    23

    Re: MFC Dialog as Mainwnd, Setting DWStyle

    Quote Originally Posted by JohnCz
    There are several approaches. One of them is to handle EN_CHANGE or EN_UPDATE from edit control and toggle enabled state of the button.

    Other (I think better) would be to handle WM_LICKIDLE and call UpdateDialogControls from it. Use ON_UPDATE_COMMAND_UI macro to map handler for a button update. Toggle enabled state in this update.

    As for “x” is not realy X; it is a button that if clicked closws window.
    You cannot remove this button unless you override WM_NCPAINT.

    You can disabre this button by removing WS_SYSMENU. That will remove icon from a title bar.
    If you want to keep an icon, remove SC_CLOSE command from a system menu. This will disable close button.
    Thanks. That cleared up my question once and for all!

    One last question I want to ask -

    How do you handle the messages for any selections made in the List Control in report view? Would it be ON_NOTIFY? I need to enable certain buttons depending on which item I click on.

    Same goes for any unselections made in the list control. I need to disable the buttons, should the user click outside of the List Control box, to unselect the items. How do I do this?
    Last edited by l46kok; May 3rd, 2008 at 06:54 PM.

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: MFC Dialog as Mainwnd, Setting DWStyle

    Yes, notification code from a list view control is sent as a parameter of the WM_NOTIFY message. You can insert handlers for specific notification code using VS.
    List view control uses LVN_ITEMCHANGED notification to notify parent about selection changes.

    If you use my preferred approach, the only thing you have to do is to is to map UI update for a button you want to toggle enable state. You do not have to handle any notifications from list view, just to update UI elements

    Using WM_KICKIDLE (MFC message) and UpdateDialogControls allows you to handle UI updates when dialog is entering idle state. UI visual states can be updated using results of changes, without getting into details what caused that change (handling each case based on notification form many controls).

    To give you an example:
    Buttons visual state depends on selection and other states of list view control items. You can change buttons state handling notification for each item, using complex logic determining what button has to change appearance and how.

    Using WM_KICKIDLE and handling WMchanges state of specific button based upon current combined state of other controls.


    Clicking outside of the list control does not change selection of any item. If you choose LVS_SHOWSELALWAYS style, you will see seelcted item even if list control does not have focus.

    Use GetFocus in UI update handler for a given button to determine if list control has a focus.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured