- 首先打開你的VS2005
- 依次點擊菜單欄的文件-->新建-->項目
- 在左邊的語言列表選里,選擇C++分支下的CLR,然后在右邊單擊“CLR 空項目”
- 在名稱框里輸入Exercise2:
- 點擊“OK”,創建項目成功。
- 好了,這只是創建了好了一個托管C++項目,要使用WINFORM,我們要引入相關的.NET組件。
- 在vs主界面上依次點擊菜單欄的項目-->"引用";
- 在彈出的子窗口中選擇添加新引用;
- 在.NET標簽頁下選擇System,點確定,再點擊添加新應用,在.NET標簽頁下選擇System.Window.Forms,然后確定,如圖
- 都完成后,應該像這樣,然后點擊確定
- 創建一個新的類,點擊VS主界面菜單欄的項目-->添加類;
- 在彈出的子窗體右邊選擇C++類,點確定,會出現添加類向導,在類名中輸入CExercise,基類中填Form
- 然后我們來到自動生成的頭文件代碼中,添加System和Form的引用,修改后的文件應該如下
-
1
2#pragma once
3
4using namespace System;
5using namespace System::Windows::Forms;
6
7public ref class CExercise : public Form
8{
9public:
10CExercise(void);
11};
然后我們要創建一個主程序來調用這個類。這個大家應該都比我熟的,右擊源文件文件夾,點擊添加-->新建項,選擇CPP文件,代碼如下: -
1
2
3#include <windows.h>
4#include "Exercise.h"
5
6int APIENTRY WinMain(HINSTANCE hInstance,
7HINSTANCE hPrevInstance,
8LPSTR lpCmdLine,
9int nCmdShow)
10{
11Application::Run(gcnew CExercise());
12
13return 0;
14}
含義我不多說了,把那個Cexercise換成你自己別的名字的類就可以運行那個類了,當然了,要注意包含頭文件。點擊運行看看,一個白白的窗體,對嗎,好的,革命成功邁出了堅實的一步。 - 下面我們來試著添加一個button按鈕,修改Exercise.cpp的代碼如下
-
1
2#include "Exercise.h"
3
4CExercise::CExercise(void)
5{
6System::Windows::Forms::Button^ button1=gcnew Button;//創建按鈕
7this->Controls->Add(button1);//把按鈕附加到我們的窗體上
8}
9
在運行看看,應該像這樣 - 然后如果你還想繼續給這個按鈕添加文字設置尺寸什么的,你可以通過像這樣的代碼,具體我不說多少,因為選擇Winform就是看中快速的控件拖拉功能,都是手寫我還不如用GTK
-
this->button1->Name = L"button1";//設置按鈕文字
this->button1->Size = System::Drawing::Size(75, 23);//設置按鈕尺寸
- 好了,下面重點來了,如何像C#一樣自由地拖拉控件改屬性呢,我們右擊項目,選擇添加-->新建項。
- 在彈出的子窗體右邊選擇Windows窗體,輸入一個名字,比如“HelloWorld”,點擊確定
- 然后我們就欣喜的發現,哇,又回到Winform拖拉控件的時代啦,是的,沒錯,而且我們可以以C++寫后臺。沒用過c#的朋友請在界面上找找工具箱,實在沒找到請依次點擊主窗體上菜單欄的視圖-->工具箱,然后從工具箱里找到一個button控件拖動到主窗體上。
- 之后雙擊這個button,會自動添加一個button的單擊事件,我們希望單擊的時候彈出一個“我是傻逼”的消息框,代碼如下:
-
1
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
2//MessageBox.Show(this,"wocao");//C#格式的調用方法是不行的
3MessageBox::Show(this,"我是傻逼");//這才是正確的
4}
5};
6}
第一個參數是父窗體,第二個參數是消息框的內容。細心的同學發現了,這里和C#不太一樣,是的,如果你按C#的寫法:Messsage.Show(...),這樣程序是沒法通過編譯的,畢竟是兩種語言嘛。- 好了,我們回到主程序入口那里,修改Exercise()為HelloWorld(),運行看看,出錯了!!沒錯,你還得添加頭文件,添加引用,真是麻煩啊,沒辦法,這兒還沒有在C#下的引用添加提示,不知道Clipse下的Cdt有沒有這個功能,最終代碼如下
1
#include <windows.h>
2#include "Hello World.h"
4using namespace WInformCpp;
5int APIENTRY WinMain(HINSTANCE hInstance,
6HINSTANCE hPrevInstance,
7LPSTR lpCmdLine,
8int nCmdShow)
9{
10Application::Run(gcnew HelloWorld());
11
12return 0;
13}
好了,運行一下看看。像這樣,那就對了,再深入地使用就不是本貼討論的范疇了,在C++上,各位都是我的前輩
因為筆者是以前是做C#的,對Winform情有獨鐘,最近想轉C++,想把以前的一些Delphi轉成c++,MFC我不熟而且用起來相當煩效果又丑,GTK圖形庫用起來太麻煩,琢磨著研究一下WInform前臺,C++做后臺的方法,谷歌了一下,沒看到什么中國資料,我草,我以前看過的啊,找來找去,找到一篇英文的,寫得太糾結,我結合自己使用的經驗,重新整理一下,言簡意賅,簡明易懂。謹供新人參考,老鳥勿拍。
FeedBack:
# re: c++用WinForm做界面的實現
2010-12-17 10:26 | waiting4you
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現
# re: c++用WinForm做界面的實現[未登錄]
# re: c++用WinForm做界面的實現
2014-07-14 11:38 | AlvenBlack
# re: c++用WinForm做界面的實現[未登錄]
# re: c++用WinForm做界面的實現
2015-05-24 10:14 | wenluderen
多謝博主的講解 , 我在VS2012 測試了
結果不是很好 報錯。
1> Exercise.cpp
1>e:\exercise2\exercise2\Exercise.h(9): error C3624: “System::ComponentModel::Component”: 使用此類型需要引用 程序集“System”
1> 在導入類型“System::Windows::Forms::Control ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ScrollableControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ContainerControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::Form ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1>Exercise.cpp(5): fatal error C1903: 無法從以前的錯誤中恢復;正在停止編譯
1> MainGsz.cpp
1>e:\exercise2\exercise2\Exercise.h(9): error C3624: “System::ComponentModel::Component”: 使用此類型需要引用 程序集“System”
1> 在導入類型“System::Windows::Forms::Control ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ScrollableControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ContainerControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::Form ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 正在生成代碼...
========== 全部重新生成: 成功 0 個,失敗 1 個,跳過 0 個 ==========
回復 更多評論
結果不是很好 報錯。
1> Exercise.cpp
1>e:\exercise2\exercise2\Exercise.h(9): error C3624: “System::ComponentModel::Component”: 使用此類型需要引用 程序集“System”
1> 在導入類型“System::Windows::Forms::Control ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ScrollableControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ContainerControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::Form ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1>Exercise.cpp(5): fatal error C1903: 無法從以前的錯誤中恢復;正在停止編譯
1> MainGsz.cpp
1>e:\exercise2\exercise2\Exercise.h(9): error C3624: “System::ComponentModel::Component”: 使用此類型需要引用 程序集“System”
1> 在導入類型“System::Windows::Forms::Control ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ScrollableControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::ContainerControl ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 在導入類型“System::Windows::Forms::Form ”(從程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中導入)時發生此診斷。
1> 正在生成代碼...
========== 全部重新生成: 成功 0 個,失敗 1 個,跳過 0 個 ==========
回復 更多評論
只有注冊用戶登錄后才能發表評論。 | ||
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
![]() |
||
相關文章:
|
||
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|
| |||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
---|---|---|---|---|---|---|---|---|---|
29 | 30 | 1 | 2 | 3 | 4 | 5 | |||
6 | 7 | 8 | 9 | 10 | 11 | 12 | |||
13 | 14 | 15 | 16 | 17 | 18 | 19 | |||
20 | 21 | 22 | 23 | 24 | 25 | 26 | |||
27 | 28 | 29 | 30 | 31 | 1 | 2 | |||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
常用鏈接
留言簿(4)
隨筆分類
隨筆檔案
- 2012年9月 (1)
- 2012年8月 (1)
- 2012年5月 (1)
- 2011年2月 (1)
- 2010年12月 (4)
- 2010年11月 (1)
- 2010年10月 (2)
- 2010年9月 (3)
- 2010年6月 (1)
- 2010年4月 (1)
搜索
最新評論

- 1.?re: c++用WinForm做界面的實現
-
@wenluderen
項目屬性, 添加新引用"System", 和文章第11步一樣 - --Taoism
- 2.?re: c++用WinForm做界面的實現
- 評論內容較長,點擊標題查看
- --wenluderen
- 3.?re: c++用WinForm做界面的實現
- 我按你的步驟 怎么操作不成功呢
- --劉
- 4.?re: c++用WinForm做界面的實現
-
非常感謝@fei
- --hooknn
- 5.?re: c++用WinForm做界面的實現[未登錄]
- 樓主寫的很實用,也很詳細,只是有一點點沒有說,在地33點中的WInformCpp是HelloWord中的命名空間,一般默認生成,我剛開始就沒找到,對菜鳥來說還真的不好找啊
- --菜鳥