Visual Studio C++ 2008/2010 Express EditionでWindowsフォームアプリケーションでwin32apiを使用する。その2
c++ cli Form で ディスプレイ 解像度の変更プログラムを作る
学習内容
1. c++ cli Form でwin32apiを使用
2. ref class 参照クラスの配列を作る
3. 画面 解像度の変更
Visual Studio C++ 2008/2010 Express EditionでWindowsフォームアプリケーションではC++/CLIが採用されているので、.NET Frameworkを使用する。
有料版にはMFCが付いているが、無料版にはない。Formアプリケーションにwin32apiを使用することは御法度のようであるが、邪道で使う方法
までプロジェクトでFormアプリケーションを作成
今回の作成では ref class 参照クラスの配列を作る
今回作るアプリケーション

使用法 リストボックスから選択 ボタン2で確認 ボタン1で変更
赤字は入力
d_data.hの中身
#pragma once
ref class d_data
{
public:
int no;
int x_data;
int y_data;
int perpel;
int hz;
d_data(void){
no=0;
x_data=0;
y_data=0;
perpel=0;
hz=0;
}
};
d_data.cppの中身
#include "StdAfx.h"
#include "d_data.h"
stdafx.hの中身
#pragma once
#include"windows.h"
#include "commctrl.h"
#include <SDKDDKVer.h>
#include "d_data.h"
stdafx.cppの中身
#include "stdafx.h"
#include <windows.h>
#include <Winuser.h>
form.hの中身
#pragma once
using namespace System::Diagnostics;
#include "d_data.h"
namespace disp32form1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//EnableWindow(ListView_GetHeader(reinterpret_cast(this->listView1->
Handle.ToInt32())),FALSE);
//
}
protected:
/// <summary>
/// 使用中のリソースをすべてクリーンアップします。
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::ListBox^ listBox1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox2;
private:
/// <summary>
/// 必要なデザイナー変数です。
/// </summary>
System::ComponentModel::Container ^components;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::TextBox^ textBox5;
static array<d_data^>^ disp_data = gcnew array<d_data^> (1000); //参照クラスの配列の収納場所を作る
#pragma region Windows Form Designer generated code
/// <summary>
/// デザイナー サポートに必要なメソッドです。このメソッドの内容を
/// コード エディターで変更しないでください。
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->textBox4 = (gcnew System::Windows::Forms::TextBox());
this->textBox5 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(15, 19);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(78, 19);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->ItemHeight = 12;
this->listBox1->Location = System::Drawing::Point(12, 91);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(461, 292);
this->listBox1->TabIndex = 2;
//
// button2
//
this->button2->Location = System::Drawing::Point(373, 19);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 19);
this->button2->TabIndex = 4;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(138, 19);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(193, 19);
this->textBox2->TabIndex = 5;
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(15, 57);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 19);
this->textBox1->TabIndex = 6;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(138, 60);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(100, 19);
this->textBox3->TabIndex = 7;
//
// textBox4
//
this->textBox4->Location = System::Drawing::Point(255, 63);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(100, 19);
this->textBox4->TabIndex = 8;
//
// textBox5
//
this->textBox5->Location = System::Drawing::Point(373, 65);
this->textBox5->Name = L"textBox5";
this->textBox5->Size = System::Drawing::Size(100, 19);
this->textBox5->TabIndex = 9;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(636, 399);
this->Controls->Add(this->textBox5);
this->Controls->Add(this->textBox4);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button2);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
DEVMODE dispMode; // デバイス構造体
HDC hdc; // 現在の色解像度を取得するためのデバイスコンテキスト
hdc = GetDC(NULL); // デバイス構造体を初期化する
ZeroMemory(&dispMode,sizeof(dispMode));
dispMode.dmSize = sizeof(dispMode);
dispMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
dispMode.dmBitsPerPel = GetDeviceCaps(hdc,BITSPIXEL);
dispMode.dmPelsWidth = int::Parse(textBox3->Text); // 解像度横サイズ(width x)
dispMode.dmPelsHeight =int::Parse(textBox4->Text); // 解像度縦サイズ(hight y)
dispMode.dmBitsPerPel =int::Parse(textBox5->Text); // 1ピクセル当たりのビット数
dispMode.dmDisplayFrequency =int::Parse(textBox6->Text); // リフレッシュ周波数
ReleaseDC(NULL,hdc);
// ディスプレイモードのチック合格なら解像度変更を行う。
if( ChangeDisplaySettings(&dispMode,CDS_TEST) == DISP_CHANGE_SUCCESSFUL ) {
ChangeDisplaySettings(&dispMode,CDS_FULLSCREEN);}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
//使用できるディスプレイモードの確認
String^ text;
static DEVMODE dispmode; // ディスプレイモード
static int disp_no;
/*-------------------
richTextBox1->SaveFile("C:\\dispmode.rtf"); 保存する場合
richTextBox1->LoadFile("C:\\dispmode.rtf");
-------------------*/
// 使用できるディスプレイモードをリストボックスに表示
disp_no = 0;
text="";
while( TRUE )
{
//全モードの列挙
if( !EnumDisplaySettings( NULL, disp_no, &dispmode ) ){ break; }
// リッチテキストに表示する
text=disp_no.ToString()+" "+dispmode.dmPelsWidth.ToString()+" "+dispmode.dmPelsHeight.ToString()+" "+dispmode.dmBitsPerPel.ToString()+" "+dispmode.dmDisplayFrequency.ToString()+"\n";
disp_data[disp_no]=gcnew d_data; //重要 クラスの配列の中身の確保
disp_data[disp_no]->no = disp_no;
disp_data[disp_no]->x_data =dispmode.dmPelsWidth; // 解像度横サイズ(width x)
disp_data[disp_no]->y_data =dispmode.dmPelsHeight; // 解像度縦サイズ(hight y)
disp_data[disp_no]->perpel =dispmode.dmBitsPerPel; // 1ピクセル当たりのビット数
disp_data[disp_no]->hz =dispmode.dmDisplayFrequency; // リフレッシュ周波数
listBox1->Items->Add(text);
disp_no++;
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
int s_no;
textBox2->Text=listBox1->Text;
textBox1->Text=listBox1->SelectedIndex.ToString();
s_no=listBox1->SelectedIndex;
textBox3->Text=disp_data[s_no]->x_data.ToString();
textBox4->Text=disp_data[s_no]->y_data.ToString();
textBox5->Text=disp_data[s_no]->perpel.ToString();
}
};
}


