<code class="lang-cpp">// wpfcli2.cpp: 主项目文件。 #include "stdafx.h" // 编译为GUI程序而不是控制台程序,同时使用main为入口点 #pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:main") using namespace System; // 需要添加这两个命名空间 using namespace System::Windows; using namespace System::Windows::Controls; // 除此之外,还需要添加以下程序集的引用 // PresentationCore // PresentationFramework // WindowBase ref class MyMainWindow : public Window { TextBlock ^txt1; TextBlock ^txt2; TextBlock ^txt3; TextBox ^tb1; TextBox ^tb2; TextBox ^tb3; Button ^btnOK; Button ^btnCancel; public: MyMainWindow() { // 设定窗口属性 this->Title = L"用户信息"; this->Width = 400; this->Height = 250; // 根表格 Grid ^rootgrid = gcnew Grid(); // 建立对象 rootgrid->Margin = Thickness(20); // 设置边界宽度 this->Content = rootgrid; // 附加到窗口 // 设定表格行高 RowDefinition ^row; row = gcnew RowDefinition(); row->Height = GridLength(25); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(10); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(25); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(10); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(25); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(10); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(1, GridUnitType::Star); rootgrid->RowDefinitions->Add(row); row = gcnew RowDefinition(); row->Height = GridLength(25); rootgrid->RowDefinitions->Add(row); // 设定表格列宽 ColumnDefinition ^col; col = gcnew ColumnDefinition(); col->Width = GridLength(80); rootgrid->ColumnDefinitions->Add(col); col = gcnew ColumnDefinition(); col->Width = GridLength(10); rootgrid->ColumnDefinitions->Add(col); col = gcnew ColumnDefinition(); col->Width = GridLength(1, GridUnitType::Star); rootgrid->ColumnDefinitions->Add(col); // 标签:姓名 txt1 = gcnew TextBlock(); // 建立对象 Grid::SetRow(txt1, 0); Grid::SetColumn(txt1, 0); // 设定行和列 txt1->Text = L"姓名"; // 设定Text属性 // 直接写VerticalAlignment::Center会被解析为this->VerticalAlignment::Center报错 txt1->VerticalAlignment = System::Windows::VerticalAlignment::Center; // 设定VerticalAlignment属性 rootgrid->Children->Add(txt1); // 附加到根表格rootgrid // 标签:性别 txt2 = gcnew TextBlock(); Grid::SetRow(txt2, 2); Grid::SetColumn(txt2, 0); txt2->Text = L"性别"; txt2->VerticalAlignment = System::Windows::VerticalAlignment::Center; rootgrid->Children->Add(txt2); // 标签:联系方式 txt3 = gcnew TextBlock(); Grid::SetRow(txt3, 4); Grid::SetColumn(txt3, 0); txt3->Text = L"联系方式"; txt3->VerticalAlignment = System::Windows::VerticalAlignment::Center; rootgrid->Children->Add(txt3); // 文本框:姓名 tb1 = gcnew TextBox(); Grid::SetRow(tb1, 0); Grid::SetColumn(tb1, 2); rootgrid->Children->Add(tb1); // 文本框:性别 tb2 = gcnew TextBox(); Grid::SetRow(tb2, 2); Grid::SetColumn(tb2, 2); rootgrid->Children->Add(tb2); // 文本框:联系方式 tb3 = gcnew TextBox(); Grid::SetRow(tb3, 4); Grid::SetColumn(tb3, 2); rootgrid->Children->Add(tb3); // 用于布局按钮的子表格grid2 Grid ^grid2 = gcnew Grid(); Grid::SetRow(grid2, 7); Grid::SetColumn(grid2, 2); rootgrid->Children->Add(grid2); // 设置子表格grid2列宽 //ColumnDefinition ^col; col = gcnew ColumnDefinition(); col->Width = GridLength(80); grid2->ColumnDefinitions->Add(col); col = gcnew ColumnDefinition(); col->Width = GridLength(10); grid2->ColumnDefinitions->Add(col); col = gcnew ColumnDefinition(); col->Width = GridLength(80); grid2->ColumnDefinitions->Add(col); // 确定按钮 btnOK = gcnew Button(); Grid::SetColumn(btnOK, 0); btnOK->Content = L"确定"; // 设定Content属性 btnOK->Click += gcnew RoutedEventHandler(this, &MyMainWindow::btnOK_Click); // 添加事件处理程序 grid2->Children->Add(btnOK); // 附加到子表格grid2 // 取消按钮 btnCancel = gcnew Button(); Grid::SetColumn(btnCancel, 2); btnCancel->Content = L"取消"; btnCancel->Click += gcnew RoutedEventHandler(this, &MyMainWindow::btnCancel_Click); grid2->Children->Add(btnCancel); } void btnOK_Click(Object ^sender, RoutedEventArgs ^args) { MessageBox::Show(L"姓名:" + tb1->Text + L"\n性别:" + tb2->Text + L"\n联系方式:" + tb3->Text, L"你单击了确定", MessageBoxButton::OK, MessageBoxImage::Information); } void btnCancel_Click(Object ^sender, RoutedEventArgs ^args) { MessageBox::Show(L"程序即将退出", L"你单击了取消", MessageBoxButton::OK, MessageBoxImage::Information); this->Close(); } }; [STAThreadAttribute] // 必须加上这句,这句话表示此线程使用消息循环进行同步 int main(array<system::string ^> ^args) { Application ^app = gcnew Application(); // Application对象用于分发消息 app->MainWindow = gcnew MyMainWindow(); // 建立主窗口对象 app->MainWindow->Show(); // 显示主窗口 app->Run(); // 进入消息循环 return 0; }</system::string></code>
[修改于 8年11个月前 - 2016/02/01 22:34:06]
时段 | 个数 |
---|---|
{{f.startingTime}}点 - {{f.endTime}}点 | {{f.fileCount}} |