Visual Studio C++ 2008/2010 Express EditionでWindowsフォームアプリケーションでwin32apiを使用する。その2
c++ cli Form で ディスプレイ 解像度の変更プログラムを作る
学習内容
1. c++ cli Form でwin32apiを使用
2. ref class 参照クラスの配列を作る
3. 画面 解像度の変更
4. フォームFormにコマンドラインから引数を読み込む
5. デスクトップに引数付きのショートカットを作る
Visual Studio C++ 2008/2010 Express EditionでWindowsフォームアプリケーションではC++/CLIが採用されているので、.NET Frameworkを使用する。
有料版にはMFCが付いているが、無料版にはない。Formアプリケーションにwin32apiを使用することは御法度のようであるが、邪道で使う方法
までプロジェクトでFormアプリケーションを作成、 今回の作成では ref class 参照クラスの配列を作る。フォームFormにコマンドラインから引数を読み込むため、次のデバッグモードで使用する。
プロジェクト プロパティ リンカー コマンドライン
/INCREMENTAL /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"

今回作るアプリケーション

デバック モードでは コマンドラインから
resolution_change.exe 1024 768 32 60 で引数を渡すことができる
Microsoft .NET Framework 4 (Web インストーラー)
どうしてもDLLでしかコンパイルできないときは vcredist_x86.exeをインストール
clrとマルチスレッド/MTは併用できない
http://www.microsoft.com/downloads/ja-jp/details.aspx?FamilyID=A7B7A05E-6DE6-4D3A-A423-37BF0912DB84
赤字は入力
// disp32form1.cpp : メイン プロジェクト ファイルです。
#include "stdafx.h"
#include "Form1.h"
using namespace disp32form1;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// メイン ウィンドウを作成して、実行します
Application::Run(gcnew Form1(args)); //argsを付ける
return 0;
}
stdafx.hの内容
// stdafx.h : 標準のシステム インクルード ファイルのインクルード ファイル、または
// 参照回数が多く、かつあまり変更されない、プロジェクト専用のインクルード ファイル
// を記述します。
#pragma once
#include"windows.h"
#include "commctrl.h"
#include <SDKDDKVer.h>
#include "d_data.h"
#include <commctrl.h>
#include <shlobj.h>
// compile with: /clr
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// マーシャリング用ヘッダ
//http://msdn.microsoft.com/ja-jp/library/bb531313.aspx
#include <msclr/marshal.h>
d_data.cpp
#include "StdAfx.h"
#include "d_data.h"
d_data::d_data(void){
no=-1;
x_data=0;
y_data=0;
perpel=0;
hz=0;
}
d_data.h
#pragma once
ref class d_data
{
public:
d_data(void);
int no;
int x_data;
int y_data;
int perpel;
int hz;
};
disp.h
#pragma once
ref class disp
{
public:
DEVMODE* dispmode; // ディスプレイモード // ネイティブ・コードのクラス(※ポインタ)
static array<d_data^>^ disp_data; //参照クラスの配列の収納場所を作る
int old_no;
int old_x_data;
int old_y_data;
int old_perpel;
int old_hz;
int disp_end_no;
disp(void);
void disp_use(void);
void disp_old(void);
int disp_chang(int width_x,int height_y,int perpel, int frequency_hz);
};
disp.cpp
#include "StdAfx.h"
#include "disp.h"
#include "d_data.h"
disp::disp(void)
{
static DEVMODE dispmode; // デバイス構造体
disp_data = gcnew array<d_data^> (1000); //参照クラスの配列の収納場所を作る
EnumDisplaySettings( NULL, ENUM_REGISTRY_SETTINGS, &dispmode ); // ディスプレイ解像度のレジストリ保存値を求める
old_x_data = dispmode.dmPelsWidth;
old_y_data = dispmode.dmPelsHeight;
old_perpel = dispmode.dmBitsPerPel;
old_hz = dispmode.dmDisplayFrequency;
}
void disp::disp_use(void)
{
//使用できるディスプレイモードの確認 配列に収納
static DEVMODE dispmode; // ディスプレイモード
int disp_no;
disp_no = 0;
while( TRUE )
{
//全モードの列挙
if( !EnumDisplaySettings( NULL, disp_no, &dispmode ) ) break;
// リッチテキストに表示する
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; // リフレッシュ周波数
disp_end_no=disp_no;
disp_no++;
}
}
int disp::disp_chang(int width_x,int height_y,int perpel, int frequency_hz){
static 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 = width_x; // 解像度横サイズ(width x)
dispmode.dmPelsHeight =height_y; // 解像度縦サイズ(hight y)
dispmode.dmBitsPerPel=perpel; // 1ピクセル当たりのビット数
dispmode.dmDisplayFrequency =frequency_hz; // リフレッシュ周波数
ReleaseDC(NULL,hdc);
// ディスプレイモードのチック合格なら解像度変更を行う。
if( ChangeDisplaySettings(&dispmode,CDS_TEST) == DISP_CHANGE_SUCCESSFUL ) {
ChangeDisplaySettings(&dispmode,CDS_FULLSCREEN);
return 0;
}
return 1;
}
void disp::disp_old(void){
static 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 = old_x_data; // 解像度横サイズ(width x)
dispmode.dmPelsHeight =old_y_data; // 解像度縦サイズ(hight y)
dispmode.dmBitsPerPel=old_perpel; // 1ピクセル当たりのビット数
dispmode.dmDisplayFrequency =old_hz; // リフレッシュ周波数
ReleaseDC(NULL,hdc);
// ディスプレイモードのチック合格なら解像度変更を行う。
if( ChangeDisplaySettings(&dispmode,CDS_TEST) == DISP_CHANGE_SUCCESSFUL ) {
ChangeDisplaySettings(&dispmode,CDS_FULLSCREEN);}
}
shortcut.hの入力
#pragma once
ref class shortcut
{
public:
shortcut(void);
BOOL CreateLinkFile(LPCTSTR lpszShortcutFile, LPCTSTR lpszTargetFile, LPCTSTR lpszArgs, LPCTSTR lpszDescription, LPCTSTR lpszDir);
int Stcut(LPCTSTR pszStr2,LPCTSTR pszStr3);
};
shortcut.cppの入力
#include "StdAfx.h"
#include "shortcut.h"
//====================
#include <windows.h>
#include <tchar.h>
#include <shlobj.h> // for IShellLink
#include <stdio.h>
//====================
#include <tchar.h> // L _T
#include <locale.h>
//ショートカット作成
hortcut::shortcut(void)
{
}
// *********************************************************
// ショートカットをデスクトップに作成
// *********************************************************
//http://winofsql.jp/sh/html/create_shortcut.htm
//http://lightbox.matrix.jp/ginpro/patio.cgi?mode=view2
&f=92&no=9&w=700&type=ref&opt=text-align:left;margin-left:5px;&xml=y
//http://support.microsoft.com/kb/229092/ja
//http://msdn.microsoft.com/ja-jp/library/bb774950(en-us).aspx
//http://dobon.net/vb/dotnet/file/createshortcut.html C#
//http://support.microsoft.com/kb/244677/ja
//http://www.h4.dion.ne.jp/~fht/wptips/shortcut.html
//http://www.h4.dion.ne.jp/~fht/wptips/selectfolder.html
//http://www.h4.dion.ne.jp/~fht/wptips/
//http://rararahp.cool.ne.jp/vc/vctips/api.htm
int shortcut::Stcut(LPCTSTR pszStr2,LPCTSTR pszStr3)
{
//LPCTSTR は const char * 書き換えできない(読み取り専用の)
//LPTSTR は char * 読み書きできる
//http://msdn.microsoft.com/ja-jp/library/cc410901.aspx
//http://support.microsoft.com/kb/207008/ja
MessageBox(NULL, TEXT(pszStr3), TEXT("準備完了"), 0);
/*HINSTANCE, HINSTANCE, LPTSTR, int*/
//デスクトップフォルダを取得します
LPITEMIDLIST pid;
HRESULT hResult = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pid);
if(SUCCEEDED(hResult)) {
TCHAR szPath[MAX_PATH * sizeof(WCHAR) / sizeof(TCHAR)];
// MessageBox(NULL, TEXT(pszStr3), TEXT( pszStr2), 0);
// LPCTSTR pszStr = _T("\\resolution_change.lnk"); //ANSI 版ではLPCSTR pszStr = "test"; Unicode 版ではLPCWSTR pszStr = L"test";
LPCTSTR pszStr = _T(pszStr3); //ANSI 版ではLPCSTR pszStr = "test"; Unicode 版ではLPCWSTR pszStr = L"test";
if(SHGetPathFromIDList(pid, szPath)) {
// lstrcat(szPath, TEXT("\\resolution_change.lnk"));
lstrcat(szPath, pszStr);
//プログラムを実行しているフォルダ名、ファイル名を取得する
// 実行ファイルのフルパス取得
char szPath2[_MAX_PATH];
GetModuleFileName( NULL, szPath2, MAX_PATH);
//デスクトップ上にメモ帳へのショートカットを作成します
// if(CreateLinkFile(szPath, TEXT("C:\\WINDOWS\\NOTEPAD.EXE"), NULL,NULL, NULL)) {
if(CreateLinkFile(szPath, szPath2, pszStr2,NULL, NULL)) {
MessageBox(NULL, TEXT("デスクトップにショートカットが作成されました"), TEXT(""), 0);
}
}
CoTaskMemFree(pid);
}
return 0;
}
//lpszShortcutFile : ショートカットファイルのパス
//lpszTargetFile : リンク元のファイルのパス
//lpszArgs : コマンドライン引数
//lpszDescription : デスクリプション
//lpszDir : 作業ディレクトリ
BOOL shortcut::CreateLinkFile(LPCTSTR lpszShortcutFile, LPCTSTR lpszTargetFile, LPCTSTR lpszArgs, LPCTSTR lpszDescription, LPCTSTR lpszDir)
{
BOOL bResult = FALSE;
//COMの初期化
CoInitialize(NULL);
//IShellLink インターフェースを取得
IShellLink* psl;
// C言語での使用例:
// CoCreateInstance( &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void **)&psl );
// C++言語での使用例:
// CoCreateInstance( CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl );
//http://atata.sakura.ne.jp/references/functions/CoCreateInstance.html
// hResult = ::CoCreateInstance((REFCLSID) CLSID_Class1, 0, CLSCTX_INPROC_SERVER, (REFIID) IID__Class1, (LPVOID*)&pClass1);
HRESULT hResult =::CoCreateInstance(( REFCLSID) CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,(REFIID) IID_IShellLink,(LPVOID*) reinterpret_cast<void **>(&psl)); //shortcut.obj : error LNK2020: 未解決のトークン (0A000013) CLSID_ShellLink
// HRESULT hResult =::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<void **>(&psl)); //shortcut.obj : error LNK2020: 未解決のトークン (0A000013) CLSID_ShellLink
if(SUCCEEDED(hResult)) {
//IPersistFile インターフェースを取得
IPersistFile* ppf;
hResult = psl->QueryInterface((REFIID)IID_IPersistFile,(LPVOID*) reinterpret_cast<void **>(&ppf)); //shortcut.obj : error LNK2020: 未解決のトークン (0A000011) IID_IPersistFile
if(SUCCEEDED(hResult)) {
//リンク元のパスを設定
hResult = psl->SetPath(lpszTargetFile);
if(SUCCEEDED(hResult)) {
//コマンドライン引数を設定
if(lpszArgs != NULL) {
psl->SetArguments(lpszArgs);
}
//デスクリプションを設定
if(lpszDescription != NULL) {
psl->SetDescription(lpszDescription);
}
//作業ディレクトリを設定
if(lpszDir != NULL) {
psl->SetWorkingDirectory(lpszDir);
}
#ifdef UNICODE
hResult = ppf->Save(lpszShortcutFile, TRUE);
if(SUCCEEDED(hResult)) {
bResult = TRUE;
}
#else
WCHAR wszShortcutFile[MAX_PATH];
if(MultiByteToWideChar(CP_ACP, 0, lpszShortcutFile, -1, wszShortcutFile, MAX_PATH) > 0) {
//ショートカットファイルの保存
hResult = ppf->Save(wszShortcutFile, TRUE);
if(SUCCEEDED(hResult)) {
bResult = TRUE;
}
}
#endif
}
ppf->Release();
}
psl->Release();
}
CoUninitialize();
return bResult;
}
//http://www.asahi-net.or.jp/~kv8s-yjm/another/yja104.htm
Form1.hの入力
#pragma once
using namespace System::Diagnostics;
using namespace msclr::interop; // msclr
#include "d_data.h"
#include "disp.h"
#include "shortcut.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)
array<System::String ^> ^Args_data;
Form1(array<System::String ^> ^args)//voiを変更
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
Args_data = args; // 追加
//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::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^ textBox6;
private: System::Windows::Forms::TextBox^ textBox5;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::TextBox^ textBox7;
private: System::Windows::Forms::TextBox^ textBox8;
private: System::Windows::Forms::TextBox^ textBox9;
private: System::Windows::Forms::TextBox^ textBox10;
private: System::Windows::Forms::Label^ label6;
private: System::Windows::Forms::Button^ button4;
//============================================================-
static disp m_disp;
static shortcut shc;
/* static array<wchar_t> awc;
static pin_ptr<wchar_t> ppwc;*/
//============================================================-
#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->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->textBox6 = (gcnew System::Windows::Forms::TextBox());
this->button3 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->textBox7 = (gcnew System::Windows::Forms::TextBox());
this->textBox8 = (gcnew System::Windows::Forms::TextBox());
this->textBox9 = (gcnew System::Windows::Forms::TextBox());
this->textBox10 = (gcnew System::Windows::Forms::TextBox());
this->label6 = (gcnew System::Windows::Forms::Label());
this->button4 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->BackColor = System::Drawing::Color::IndianRed;
this->button1->Location = System::Drawing::Point(83, 1);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(82, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"変更";
this->button1->UseVisualStyleBackColor = false;
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, 115);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(523, 352);
this->listBox1->TabIndex = 2;
this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBox1_SelectedIndexChanged);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(171, 3);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(193, 19);
this->textBox2->TabIndex = 5;
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(43, 57);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(47, 19);
this->textBox1->TabIndex = 6;
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(125, 57);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(78, 19);
this->textBox3->TabIndex = 7;
//
// textBox4
//
this->textBox4->Location = System::Drawing::Point(234, 57);
this->textBox4->Name = L"textBox4";
this->textBox4->Size = System::Drawing::Size(69, 19);
this->textBox4->TabIndex = 8;
//
// textBox5
//
this->textBox5->Location = System::Drawing::Point(340, 58);
this->textBox5->Name = L"textBox5";
this->textBox5->Size = System::Drawing::Size(48, 19);
this->textBox5->TabIndex = 9;
//
// textBox6
//
this->textBox6->Location = System::Drawing::Point(455, 59);
this->textBox6->Name = L"textBox6";
this->textBox6->Size = System::Drawing::Size(68, 19);
this->textBox6->TabIndex = 10;
//
// button3
//
this->button3->BackColor = System::Drawing::Color::Chartreuse;
this->button3->Location = System::Drawing::Point(2, 1);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 11;
this->button3->Text = L"元に戻す";
this->button3->UseVisualStyleBackColor = false;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 58);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(19, 12);
this->label1->TabIndex = 12;
this->label1->Text = L"No";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(102, 59);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(17, 12);
this->label2->TabIndex = 13;
this->label2->Text = L"横";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(211, 58);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(17, 12);
this->label3->TabIndex = 14;
this->label3->Text = L"縦";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(317, 59);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(17, 12);
this->label4->TabIndex = 15;
this->label4->Text = L"色";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(395, 59);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(54, 12);
this->label5->TabIndex = 16;
this->label5->Text = L"リフレッシュ";
//
// textBox7
//
this->textBox7->Location = System::Drawing::Point(129, 89);
this->textBox7->Name = L"textBox7";
this->textBox7->Size = System::Drawing::Size(74, 19);
this->textBox7->TabIndex = 17;
//
// textBox8
//
this->textBox8->Location = System::Drawing::Point(209, 90);
this->textBox8->Name = L"textBox8";
this->textBox8->Size = System::Drawing::Size(80, 19);
this->textBox8->TabIndex = 18;
//
// textBox9
//
this->textBox9->Location = System::Drawing::Point(305, 90);
this->textBox9->Name = L"textBox9";
this->textBox9->Size = System::Drawing::Size(40, 19);
this->textBox9->TabIndex = 19;
//
// textBox10
//
this->textBox10->Location = System::Drawing::Point(362, 90);
this->textBox10->Name = L"textBox10";
this->textBox10->Size = System::Drawing::Size(42, 19);
this->textBox10->TabIndex = 20;
//
// label6
//
this->label6->AutoSize = true;
this->label6->Location = System::Drawing::Point(91, 95);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(29, 12);
this->label6->TabIndex = 21;
this->label6->Text = L"引数";
//
// button4
//
this->button4->BackColor = System::Drawing::Color::MediumPurple;
this->button4->Location = System::Drawing::Point(2, 24);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(163, 27);
this->button4->TabIndex = 22;
this->button4->Text = L"ディスクトップにショートカットを作る";
this->button4->UseVisualStyleBackColor = false;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Plum;
this->ClientSize = System::Drawing::Size(550, 479);
this->Controls->Add(this->button4);
this->Controls->Add(this->label6);
this->Controls->Add(this->textBox10);
this->Controls->Add(this->textBox9);
this->Controls->Add(this->textBox8);
this->Controls->Add(this->textBox7);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button3);
this->Controls->Add(this->textBox6);
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->listBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"画面解像度変更";
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) {
// disp m_disp;
int width_x = int::Parse(textBox3->Text); // 解像度横サイズ(width x)
int height_y =int::Parse(textBox4->Text); // 解像度縦サイズ(hight y)
int perpel =int::Parse(textBox5->Text); // 解像度縦サイズ(hight y)
int frequency_hz =int::Parse(textBox6->Text); // リフレッシュ周波数
int y;
y=m_disp.disp_chang( width_x, height_y, perpel, frequency_hz);
if(y==1) MessageBox::Show("解像度変更失敗");
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
//フォームへの引数の引き渡し 横 縦 色 リフレッシュ 例1028 768 32 60
int h_data0;
int h_data1;
int h_data2;
int h_data3;
/*======================================
if(0 < CommandLineArgs->Length)
{ System::Diagnostics::Debug::WriteLine("引数の個数 = " + CommandLineArgs->Length);
for(int i = 0;i < CommandLineArgs->Length;i++)System::Diagnostics::Debug::WriteLine("No." + i + " = " + CommandLineArgs[i]);
}else
{ CommandLineArgs = gcnew array<String ^>(2);
CommandLineArgs[0] = "";
}
{ MessageBox::Show("引数の個数 = " + CommandLineArgs->Length);
for(int i = 0;i < CommandLineArgs->Length;i++) MessageBox::Show("No." + i + " = " + CommandLineArgs[i]);
}else
{ CommandLineArgs = gcnew array<String ^>(2);
CommandLineArgs[0] = "";
}
//==========================*/
if(Args_data->Length==4){
h_data0=int::Parse(Args_data[0]);
h_data1=int::Parse(Args_data[1]);
h_data2=int::Parse(Args_data[2]);
h_data3=int::Parse(Args_data[3]);
}else{ Args_data = gcnew array<String ^>(2);
Args_data[0] = "";}
//フォームへの引数の引き渡し終わり
//disp m_disp;
//現在のモード
textBox3->Text=m_disp.old_x_data.ToString();
textBox4->Text=m_disp.old_y_data.ToString();
textBox5->Text=m_disp.old_perpel.ToString();
textBox6->Text=m_disp.old_hz.ToString();
//引数
textBox7->Text=h_data0.ToString();
textBox8->Text=h_data1.ToString();
textBox9->Text=h_data2.ToString();
textBox10->Text=h_data3.ToString();
static DEVMODE dispmode; // デバイス構造体
//使用できるディスプレイモードの確認
m_disp.disp_use();
String^ text;
text="";
int disp_no=0;
/*-------------------
richTextBox1->SaveFile("C:\\dispmode.rtf"); 保存する場合
richTextBox1->LoadFile("C:\\dispmode.rtf");
-------------------*/
// 使用できるディスプレイモードをリストボックスに表示
disp_no = 0;
int flag=0;
if(h_data0==0 && h_data1==0 && h_data2==0 && h_data3==0 ) flag=-1;
int y;
//while( TRUE )
// while(m_disp.disp_data[disp_no]->no = -1)
for(disp_no=0;disp_no<=m_disp.disp_end_no;disp_no++)
{
//全モードの列挙
//if( m_disp.disp_data[disp_no]->no==-1 ) break;
// リッチテキストに表示する
text=disp_no.ToString()+" "+m_disp.disp_data[disp_no]->x_data.ToString()+" "+m_disp.disp_data[disp_no]->y_data.ToString()+
" "+m_disp.disp_data[disp_no]->perpel.ToString()+" "+m_disp.disp_data[disp_no]->hz.ToString()+"\n";
listBox1->Items->Add(text);
//フォームへの引数が解像度と適合しているかのチェック
if( m_disp.disp_data[disp_no]->x_data==h_data0 &&
m_disp.disp_data[disp_no]->y_data==h_data1 &&
m_disp.disp_data[disp_no]->perpel==h_data2 &&
m_disp.disp_data[disp_no]->hz==h_data3) flag=1;
// disp_no++;
}
textBox1->Text=flag.ToString();
//フォームへの引数の引き渡しがあったときの処理
if(flag==1){
y=m_disp.disp_chang( h_data0,h_data1,h_data2,h_data3); //フォームへの引数で解像度変更
this->WindowState = FormWindowState::Minimized; //現在のフォームを最小化
MessageBox::Show("現在のフォームを最小化しました。");
}
else {
if(flag !=-1){
text= h_data0.ToString()+" "+h_data1.ToString()+" "+h_data2.ToString()+" "+h_data3.ToString()+" ";
if (MessageBox::Show(text+"対応していない解像度です。強制変更してみますか?", "確認", MessageBoxButtons::YesNo) == System::Windows::Forms::DialogResult::Yes) {
y=m_disp.disp_chang( h_data0,h_data1,h_data2,h_data3);
}
}
}
if(y==1) MessageBox::Show("解像度変更失敗");
/*
現在のフォームを最小化
this->WindowState = FormWindowState::Minimized;
現在のフォームを最大化
this->WindowState = FormWindowState::Maximized;
現在のフォームを最小化、あるいは最大化から元に戻す
this->WindowState = FormWindowState::Normal;
現在のフォームを非表示
this->Hide();
現在のフォームを非表示から表示
this->Show();
*/
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
// disp m_disp;
int s_no=0;
textBox2->Text=listBox1->Text;
textBox1->Text=listBox1->SelectedIndex.ToString();
s_no=listBox1->SelectedIndex;
textBox3->Text=m_disp.disp_data[s_no]->x_data.ToString();
textBox4->Text=m_disp.disp_data[s_no]->y_data.ToString();
textBox5->Text=m_disp.disp_data[s_no]->perpel.ToString();
textBox6->Text=m_disp.disp_data[s_no]->hz.ToString();
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
m_disp.disp_old();
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
//http://code.msdn.microsoft.com/VisualC-7c6bc862
//http://yokohama.cool.ne.jp/chokuto/urawaza/com/shell.html
//http://minidx.blogspot.com/2007/03/vc_14.html 文字ニツイテ
//http://fatalita.sakura.ne.jp/Junk/_T/index.html
//const TCHAR * text = _T("文字列"); と LPCTSTR text = _T("文字列"); は同じ意味です。
//http://ysmt.blog21.fc2.com/blog-entry-244.html
//http://support.microsoft.com/kb/311259/ja
//http://codezine.jp/article/detail/4774
shortcut stc;
int ret;
String^ option1;
String^ option2;
option1= textBox3->Text + " "+ textBox4->Text + " " + textBox5->Text + " " + textBox6->Text;
option2="\\resolution_change "+option1+".lnk";
MessageBox::Show(option1+"解像度の準備をします");
/*
String^ -> char*
//marshal_contextのインスタンスを作ってメソッドのmarshal_asを呼ぶ
#include <msclr/marshal.h>
using namespace msclr::interop;
//
String^ str = "test_text";
marshal_context context;
const char* ptr = context.marshal_as<const char*>(str);
*/
//LPTSTR pszStr; //exeample ="1024 768 32 60";
const char* pszStr1;
marshal_context ctx1;
pszStr1 = ctx1.marshal_as<const char*>(option1);
const char* pszStr2;
marshal_context ctx2;
pszStr2 = ctx2.marshal_as<const char*>(option2);
// s2 = ctx.marshal_as<const wchar_t*>(s0);
ret=stc.Stcut(pszStr1,pszStr2);
if(ret) MessageBox::Show("デスクトップのショートカットを確認してください");
}
private: System::Void listBox1_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e) {
// disp m_disp;
int s_no=0;
textBox2->Text=listBox1->Text;
textBox1->Text=listBox1->SelectedIndex.ToString();
s_no=listBox1->SelectedIndex;
textBox3->Text=m_disp.disp_data[s_no]->x_data.ToString();
textBox4->Text=m_disp.disp_data[s_no]->y_data.ToString();
textBox5->Text=m_disp.disp_data[s_no]->perpel.ToString();
textBox6->Text=m_disp.disp_data[s_no]->hz.ToString();
}
};
}



