顯示具有 MFC 標籤的文章。 顯示所有文章
顯示具有 MFC 標籤的文章。 顯示所有文章

2011年11月11日 星期五

[MFC] Set default focus of control item in dialog

The story is like this, we had some small requirement with our program.
Default focus of control item in our program was asked to change to some edit box for data input instead of setting on OK button as default, so that users don't need to change the focus by moving cursor or clicking Tab button.

And it can avoid an extra Enter button clicking to run OK when our program is launched.
But this request wasn't altered in next version.
I discussed with our engineer, and he said SetFocus() method of CWnd can work properly.
Then  I found something tricky in OnInitDialog() function when I looking into this issue.
It's just the key point to resolve this problem.
Share with you here.

* Set the focus of control item in OnInitDialog(), but it doesn't work.












* The key point is here,
OnInitDialog() must return FALSE so that the focus of control item can be set properly.




[MFC] 設定對話窗中預設的控制項焦點

事情是這樣的,我們的程式有個小修正
請工程師將程式預設的控制項改到輸入資料的Edit Box上
控制項焦點不要放在預設的OK按鈕
如此一來使用者便不用再用滑鼠點Edit Box或用Tab鍵更改控制焦點
也避免程式開起之後Enter多按一次馬上按到OK鍵
但是又過了一個版本依然沒有修正這問題
工程師反應CWnd的SetFocus()函式無法正確地將控制焦點設定好
後來去幫忙看問題時發現OnInitDialog()裡面有個小小的"眉角"
正是這個問題的關鍵所在!
與大家分享~

* 在OnInitDialog()設定了控制項焦點, 但是無法動作












* 關鍵在這裡, 
OnInitDialog()必須回傳FALSE, 設定控制項焦點才能正常動作




2011年11月4日 星期五

[MFC] How to change Tab order of control items in Dialog with VC 2010

First, open dialog editor.

Then, we can see current Tab order with Ctrl+D pressed.

Move mouse to click on the control items to change Tab order.
 
We can also change Tab order by clicking Menu -> Format -> Tab Order. Click Menu -> Format -> Test Dialog can test this Dialog.


[MFC] VC2010如何更改對話視窗中的控制項Tab順序

首先, 打開對話窗編輯器



然後, 按Ctrl+D, 可以看到目前的Tab順序
 

移動滑鼠點擊控制項, 即可更換順序
 

也可以從Meun -> Format點選Tab Order來更換Tab順序. 按Test Dialog(Ctrl+T)可以進行測試.

2011年10月27日 星期四

[MFC] How to get the pointers of Doc,View,MainFrmae,App in SDI, MDI

When developing MFC program on Document/View architecture, the first problem we usually have is how to get the pointers of Doucment, View, MainFrame and App.
Below are some common methods to get the pointers:
  1. Get the pointer of Application Class CWinApp:Use MFC Global Function AfxGetApp() to get it.
     

  2. Get the pointer of MainFrame
    • User member variable: m_pMainWnd
      (
      m_pMainWnd is the member variable of CWinThread, and CWinApp derived from CWinThread, so we can use the member of CWinThread to get the pointer of Main frame )
      CMainFrame* pMainWnd = (CMainFrame*) AfxGetApp()->m_pMainWnd;
    • Use MFC Global Function AfxGetMainWnd() to get it.
      CMainFrame* pMainWnd =(CMainFrame*) AfxGetMainWnd();
  3. Get the pointer of Document in View
    Us
    e the member function GetDocument() to get it.
    CMyAppDoc*
    pDoc = GetDocument();
  4. Get the pointer of Active View
    First,
    get the pointer of MainFrame, and then use GetActiveView() to get the pointer of View by MainFrame.
    CMainFrame* pMainWnd = (CMainFrame*) AfxGetApp()->m_pMainWnd; 
    CMyAppView* pView = ( CMyAppView*) pMainWnd->GetActiveView();
  5. Get the pointer of Active Document
    First,
    get the pointer of FrameWnd, and then use GetActiveDocument() to get the pointer of Document by FrameWnd.
    CFrameWnd* pMainFrm = (CFrameWnd*) AfxGetApp()->m_pMainWnd;
    CDocument* pCurDoc = (CFrameWnd*) pMainFrm ->GetActiveDocument();


[MFC] SDI, MDI如何取得Doc,View,MainFrmae,App之指標

開發MFC Document/View架構的程式時,首先遇到的問題就是如何取得Doucment, View, MainFrame與App的指標。
以下是一些常用的指標取得方法:

  1. 取得應用程式類別CWinApp指標
    用MFC全域函數 AfxGetApp() 取得
     
  2. 取得MainFrame指標
    • 使用成員變數m_pMainWnd
      (
      m_pMainWnd為CWinThread的成員變數, 而CWinApp繼承自CWinThread, 故可使用m_pMainWnd取得主視窗之指標)
      CMainFrame* pMainWnd = (CMainFrame*) AfxGetApp()->m_pMainWnd;
    • 透過MFC全域函數 AfxGetMainWnd() 取得
      CMainFrame* pMainWnd = (CMainFrame*) AfxGetMainWnd();
  3. 在View中取得Document指標
    用View的成員函數
    GetDocument()取得
    CMyAppDoc*
    pDoc=GetDocument();

  4. 取得現行的View指標先取得MainFrame指標,再由MainFrame之GetActiveView()函數取得View的指標
    CMainFrame* pMainWnd = (CMainFrame*) AfxGetApp()->m_pMainWnd; 

    CMyAppView* pView = ( CMyAppView*) pMainWnd->GetActiveView();


  5. 取得現行的Document指標
    先取得
    FrameWnd指標,再由FrameWnd的GetActiveDocument()函數取得Document的指標
    CFrameWnd* pMainFrm = (CFrameWnd*) AfxGetApp()->m_pMainWnd;
    CDocument* pCurDoc = (CFrameWnd*) pMainFrm ->GetActiveDocument();