2012年4月11日 星期三

OnInitDialog 請將初始化設定在這裡

This message is sent to the dialog box during the Create, CreateIndirect, or DoModal calls, which occur immediately before the dialog box is displayed.
from MSDN

其實不用從上面的說明,從function的名稱也可以猜到dialog初始化時會先執行function所定義的項目.
在vc2003之前,你可以從Class wizard->message->WM_INITDIALOG.
在vc2003之後,請在Class wizard->Override->OnInitDialog

其實也挺合理的,這本來就是一個virtual function,在我還不知道class wizard可以添加之前,也都是直接手動定義的,這邊也分享一下原始碼,如果還是找不到添加的位置就手動key吧!!正所謂DIY會更有感覺 XD.

(In head file *.h)
class Dlg : public CDialog
{
.....
protected:
virtual BOOL OnInitDialog();
.....
}

(In source code *.cpp)
BOOL Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
      //TODO:在此添加額外的初始化設定


return TRUE;
}

2012年4月9日 星期一

如何從Editbox取得其值並轉換成整數(Get value from editbox and transfer to Int)

Example: Editbox 其值的存取以及如何在Cstring與int間做轉換


CEdit   *pEdit   =   (CEdit*)GetDlgItem(IDC_EDIT1); 
CString   c_str; 
pEdit-> GetWindowText(c_str); //取得editbox1中的值並放入Cstring變數c_str中
(CEdit*)GetDlgItem(IDC_EDIT2)-> SetWindowText(str1);//將 c_str 放入editbox2中



Cstring to Int
int aa;
aa=atoi(c_str); 

Int to CString 
c_str.Format( "%d",aa); 





以下範例將說明,如何利用dialog2上的Button物件來控制dialog1上的CStatic物件

現在有兩個 Dialog 視窗分別為 dlg1 (Main) , dlg2 ,
在dlg1上有Button1物件,以下為Button1事件的程式碼


void dlg1::OnButton1()
{
        dlg1->m_static1.ShowWindow(SW_HIDE) ; // 將 m_static 設定為看不見
Dlg *dlg2 = new Dlg();
dlg2->Create(IDD_DIALOG1,this);
dlg2->ShowWindow(SW_SHOW);
}

.......
.......

在dlg2上有Button2物件,以下為Button2事件的程式碼
#include "dlg1.h"
void dlg2::OnButton2()
{
        //從dlg2的Button來控制dlg1的Cstatic改變其屬性
        ((dlg1*)AfxGetMainWnd())->m_static1.ShowWindow(SW_SHOW) ;
}


重點就在(dlg1*)AfxGetMainWnd() , 轉成dlg1物件並利用AfxGetMainWnd()來取得其物件指標,藉此操控dlg1上的Cstatic物件!!!

ps : #include "dlg1.h"

2012年4月5日 星期四

[C++] Marco PI 去哪裡了 ??? --> M_PI

一直記得C++內定義了PI的數值 , 怎麼這次使用PI找不到定義 ??!!
稍微Google了一下得到以下定義 :

/* Definitions of useful mathematical constants
 * M_E        - e
 * M_LOG2E    - log2(e)
 * M_LOG10E   - log10(e)
 * M_LN2      - ln(2)
 * M_LN10     - ln(10)
 * M_PI       - pi
 * M_PI_2     - pi/2
 * M_PI_4     - pi/4
 * M_1_PI     - 1/pi
 * M_2_PI     - 2/pi
 * M_2_SQRTPI - 2/sqrt(pi)
 * M_SQRT2    - sqrt(2)
 * M_SQRT1_2  - 1/sqrt(2)
 */


那就來使用M_PI搞定它 !!!!
...........
...........
還是沒有定義 (翻桌) !!!! 怎麼可能 !!!! 把math.h翻出來看 ,
/* Define _USE_MATH_DEFINES before including math.h to expose these macro
 * definitions for common math constants.  These are placed under an #ifdef
 * since these commonly-defined names are not part of the C/C++ standards.
 */

原來已經從C++ standard移除了 , 從此以後都要先定義_USE_MATH_DEFINES.
所以想要使用M_PI , 需要以下:

1. #define _USE_MATH_DEFINES
(在標頭檔定義)
2. #include "math.h"

Done.

Any problem , please leave your message.
Sean.

2012年4月4日 星期三

C++ 複數運算


利用C++標準的函式庫來達到複數的運算
#include <complex>
using namespace std ;


建立一個基本的MFC Dialog , 按下OK button來顯示複數運算結果 , 
部份程式碼如下 :



void CdiaggDlg::OnBnClickedOk1()
{
       // 宣告3個複數的class , 並進行運算
       complex<double> complex1(1,2);
       complex<double> complex2(3,-3);
       complex<double> complex3 = complex1+complex2 ; 
       // 利用Cstatic顯示運算結果
       CString cstr_real,cstr_imag;
       cstr_real.Format("%.2f",complex3.real());
       cstr_imag.Format("%.2f",complex3.imag());
       SetDlgItemText(IDC_STATIC1,cstr_real+"  "+cstr_imag+"i");
}



_______________________
如果有任何建議,歡迎提出討論!!

Sean

2012年4月1日 星期日

Using MFC Dialog to start !!!

介紹如何利用Visual studio 2003來內建精靈建立自己的第一個視窗程式 !!!

Step1. Create a New Project












Step2. Change the application type to Dialog based

你可以從User Interface Feature去選擇你喜歡的視窗格式.
在這裡我把About box取消只保留了System menu的部份.








Finished !!!
按下F5開始編譯 , 你會發現一個基本的視窗已經由MFC為你建立好了 !!
你可以在Resource下找到一個Dialog-IDD_projectname_DIALOG的視窗資源.
Visual Studio預設產出兩個Button元件 , 你如果不喜歡可以自行移除.

ps.兩個Button的ID預設為IDOK & IDCANCEL , 都是按下之後就會關閉視窗的事件,
想要繼續使用這兩個元件又不想要按下就關閉視窗,只需要將ID指定成其他的就可以了!