ホーム » .NET (ページ 2)
「.NET」カテゴリーアーカイブ
.NET と Win32 API
HDC の利用
IntPtr のまま与えてしまうと,
c:\…\Form1.h(156) : error C2664: ‘DrawText’ : 1 番目の引数を ‘System::IntPtr’ から ‘HDC’ に変換できません。
IntPtr pDC = e->Graphics->GetHdc() ;
HDC hDC = static_cast(pDC.ToPointer()) ;
// DrawText(hDC, tstr.c_str(), -1, &rect, 0 );
e->Graphics->ReleaseHdc(pDC) ;
ちょっと古いが,プログラミング Visual C++.NET Vol.2 P.459
コードを修正しビルドすると,
~.obj : error LNK2028: 未解決のトークン (0A00005B) …
~.obj : error LNK2019: 未解決の外部シンボル …
「親または…」にチェックを入れる.
C# のコードを C++.NET で…
MSDN にあるサンプルを VC++2008 でビルドする.
MSDN 四角形内にテキストを折り返して描画する
新規プロジェクトで「Windows フォーム アプリケーション」.
Paint のハンドラを追加.
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
/*
string text2 = “Draw text in a rectangle by passing a RectF to the DrawString method.”;
using (Font font2 = new Font(“Arial”, 12, FontStyle.Bold, GraphicsUnit.Point))
{
Rectangle rect2 = new Rectangle(30, 10, 100, 122);
// Specify the text is wrapped.
TextFormatFlags flags = TextFormatFlags.WordBreak;
TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2));
}
*/
String^ text2 = “Draw text in a rectangle by passing a RectF to the DrawString method.”;
Drawing::Font^ font2 = gcnew Drawing::Font(“Arial”, 12, FontStyle::Bold, GraphicsUnit::Point) ;
Drawing::Rectangle^ rect2 = gcnew Drawing::Rectangle(30,10,100,122) ;
TextFormatFlags flags = TextFormatFlags::WordBreak ;
TextRenderer::DrawText(e->Graphics,text2,font2,*rect2,Color::Blue,flags) ;
e->Graphics->DrawRectangle(Pens::Black,*rect2) ;
}
RECT または CRect にあたるものは,Drawing::Rectangle .
ここで利用している Font は Drawing::Font としないとエラーになる.
c:\…\drawt_2\Form1.h(87) : error C2061: 構文エラー : 識別子 ‘Font’
DrawText などの Rectangle はハンドルではなく実体.
2013/11/05
C# の文字列の前の ‘@’
リテラル文字列
MFC での文字列の変数に対する += .
CString str ;
str += _T(“123”) ;
C++/CLI で System::String は可能であるが,C# の string ではエラーとなるみたい.
2013/11/06
C# で g.Dispose() ;
C++ では delete g ;
Graphics.Dispose メソッド
この要求の処理に必要なリソースの解析中に…
プログラミング Visual C++.NET の Ex34a で,
‘/’ アプリケーションでサーバー エラーが発生しました。
パーサー エラー
説明: この要求の処理に必要なリソースの解析中にエラーが発生しました。以下の解析エラーの詳細を確認し、ソースファイルに変更を加えてください。
パーサー エラー メッセージ: 型 ‘Ex34a.ManagedCPPPage’ を読み込めませんでした。
ソース エラー:
|
ソース ファイル: /Test/ASP_net/Ex34a.aspx
行: 1
バージョン情報: Microsoft .NET Framework バージョン:2.0.50727.3649; ASP.NET バージョン:2.0.50727.3634
「インターネット インフォメーション サービス」を起動.
対象のフォルダ(ここでは Test/ASP_net )を選択して,「右クリック」-「プロパティ」.
「ディレクトリ」タブの真ん中あたり,「アプリケーションの設定」で「作成」する.
https://dev.mish.work/wordpress/2010/01/29/構成にエラーがあります/
C2146
次の様なコードで,C2146 , C2065
private: System::Void Form1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
Size cr = this->ClientSize ;
Point lt = Point(cr.Width/3*1,cr.Height/3*1) ;
Point rb = Point(cr.Width/3*2,cr.Height/3*2) ;
array<Point>^ pts = gcnew array<Point>(5) ;
{
pts->SetValue(gcnew Point(lt.X,lt.Y),0) ;
pts->SetValue(gcnew Point(lt.X,rb.Y),1) ;
pts->SetValue(gcnew Point(rb.X,rb.Y),2) ;
pts->SetValue(gcnew Point(rb.X,lt.Y),3) ;
pts->SetValue(gcnew Point(lt.X,lt.Y),4) ;
}
e->Graphics->DrawLines(gcnew Pen(Color::Red),pts) ;
}
private: System::Void Form1_SizeChanged(System::Object^ sender, System::EventArgs^ e) {
this->Invalidate() ;
}
—— ビルド開始: プロジェクト: DrwLines, 構成: Debug Win32 ——
コンパイルしています…
DrwLines.cpp
c:\…\drwlines\Form1.h(75) : error C2146: 構文エラー : ‘;’ が、識別子 ‘cr’ の前に必要です。
c:\…\drwlines\Form1.h(75) : error C2065: ‘cr’ : 定義されていない識別子です。
c:\…\drwlines\Form1.h(76) : error C2228: ‘.Width’ の左側はクラス、構造体、共用体でなければなりません
型は ”unknown-type” です。
c:\…\drwlines\Form1.h(76) : error C2228: ‘.Height’ の左側はクラス、構造体、共用体でなければなりません
型は ”unknown-type” です。
c:\…\drwlines\Form1.h(77) : error C2228: ‘.Width’ の左側はクラス、構造体、共用体でなければなりません
型は ”unknown-type” です。
c:\…\drwlines\Form1.h(77) : error C2228: ‘.Height’ の左側はクラス、構造体、共用体でなければなりません
型は ”unknown-type” です。
ビルドログは “file://c:\…\DrwLines\Debug\BuildLog.htm” に保存されました。
DrwLines – エラー 6、警告 0
========== ビルド: 0 正常終了、1 失敗、2 更新、0 スキップ ==========
Drawing::Size とすることにより通る様になるが,イマイチどの様に書くべきかが理解できてない.
DrawLines についても,よくわかってない.array<Point>^ の array って何?
ここの説明がわかりやすい.
pts->SetValue(gcnew Point(lt.X,lt.Y),0) ; の gcnew はいらない?
関数の引数の const ~ & はどこへ?
printf や CString::Format の “%10.3f” の様な形式
String::Format で “{0,10:F3}”
C:\…\HelpPnt.hxx(72) : error C3083: ‘Drawing’: ‘::’ の左側のシンボルには、型を指定しなければなりません
C:\…\HelpPnt.hxx(72) : error C2039: ‘Point’ : ‘System’ のメンバではありません。
C:\…\HelpPnt.hxx(72) : error C2065: ‘Point’ : 定義されていない識別子です。
#using <System.Drawing.dll> を追加
CLI + MFC
CLR コンソール アプリケーションを作成.
Console::WriteLine を追加.
#include “stdafx.h”
using namespace System;
int main(array ^args)
{
Console::WriteLine(L”Hello World”);
#ifdef _WIN32
Console::WriteLine(L”WIN32″);
#endif
#ifdef __cplusplus_cli
Console::WriteLine(L”CLI”);
#endif
#ifdef __CLR_VER
Console::WriteLine(__CLR_VER);
#endif
#ifdef _MFC_VER
Console::WriteLine(_MFC_VER);
#endif
return 0;
}
実行すると,
C:\…>”C:\…\debug\ConAp.exe”
Hello World
WIN32
CLI
20050727
これに MFC の機能を追加しようとしたがわからなかったので.MFC コンソール AP を作成して /clr を追加することにした.
#include “stdafx.h”
#include “ConMFC.h”
//
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
using namespace System;
// 唯一のアプリケーション オブジェクトです。
CWinApp theApp;
//
using namespace std;
//
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
//
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) {
_tprintf(_T(“致命的なエラー: MFC の初期化ができませんでした。\n”));
nRetCode = 1;
}
else {
// TODO: アプリケーションの動作を記述するコードをここに挿入してください。
}
Console::WriteLine(L”Hello World”);
#ifdef _WIN32
Console::WriteLine(L”WIN32″);
#endif
#ifdef __cplusplus_cli
Console::WriteLine(L”CLI”);
#endif
#ifdef __CLR_VER
Console::WriteLine(__CLR_VER);
#endif
#ifdef _MFC_VER
Console::WriteLine(_MFC_VER);
#endif
return nRetCode;
}
実行すると,
C:\…>”C:\…\debug\ConMFC.exe”
Hello World
WIN32
CLI
20050727
2048
CLR のバージョンは,2.0.50727
MFC は 0x0800
VC 定義済みマクロ
ASP.NET Web
Visual Studio .NET スタートブック を読みながら,ASP.NET Web サービスをテスト(使用した VS は 2005 ).
新規プロジェクトで出来上がった,Service1.asmx.vb の
<WebMethod()> Public Function HelloWOrld() … End Function の次に,以下を追加.
<WebMethod()> Public Function CalcSvc(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function
実行も本の通り.
同様に,Service1.asmx.cs では
[WebMethod]
public string HelloWorld() {
return “Hello World”;
}
[WebMethod]
public int CalcSvcCS(int x ,int y) {
return x + y ;
}
今度は C++ .
面倒だったので CalcCPPClass.h のみに.
[System::Web::Services::WebMethod]
String ^HelloWorld();
[System::Web::Services::WebMethod]
int CalcSvcCPP(int x,int y) { return x+y ; }
ソリューションに複数のプロジェクトとしていたので,どこかの設定が違うのか VS から直接の起動が出来ない.
ブラウザで //localhast/~/~.asmx を指定すれば期待した動作となる.
Mono
ここを参考にさせてもらって,MonoDevelop をインストール.
C# のコンソールプロジェクトで作成したスケルトンのまま,ビルド,実行はすんなり動作した.
先日作成したフォームを塗りつぶすコードも,プロジェクトをそのままコピーして実行できる.
private void Form1_Paint(object sender, PaintEventArgs e)
{
LinearGradientBrush brush = new LinearGradientBrush
(ClientRectangle,Color.Green,Color.White,LinearGradientMode.ForwardDiagonal);
e.Graphics.FillRectangle(brush,ClientRectangle);
}
VisualC#.NET プログラミング入門にある AssemblyViewer も起動はしている.
機能の動作はエラーとなることもあるが,これはこの exe などの制限かもしれない.
VS 2005 で生成できる C# のスクリーン セーバ スタート キットも実行できる.
/c で設定画面を表示して rss を指定しても,RSS の読込が正しく行われない?
→ と思ったが,やり直したら?正しく RSS が読めている.
mono SSaveCS.exe [/c]
VB.NET のプロジェクトは生成できるが,ビルドしようとするとコンパイラがないとのエラー.
検索して,vbnc をインストール.
コンソール AP は通る様になったが,Windows フォームではコンパイルエラー?
Application.Designer.vb
Me.MainForm = Global.WinApVB.FormVB
’WinApVB.FormVB’ is a type and cannot be use as an expression.(VBNC30691)
VB Windows フォームには対応していないのか?
フォームへの描画
フォームに,左上から右下へグラデーションで表示する動作を幾つかの言語で書いてみた.
C#
//…
using System.Drawing.Drawing2D;
//…
namespace WinApCS
{
public partial class FormCS : Form
{
public FormCS()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
LinearGradientBrush brush = new LinearGradientBrush
(ClientRectangle,Color.Green,Color.White,LinearGradientMode.ForwardDiagonal);
e.Graphics.FillRectangle(brush,ClientRectangle);
}
protected override void OnResize(System.EventArgs e)
{
Invalidate();
}
}
}
VB
Imports System.Drawing.Drawing2D
Public Class FormVB
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim brush As New LinearGradientBrush
(ClientRectangle, Color.Blue, Color.White, LinearGradientMode.ForwardDiagonal)
e.Graphics.FillRectangle(brush, ClientRectangle)
End Sub
Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
Invalidate()
End Sub
End Class
J#
//…
import System.Drawing.Drawing2D.*;
//…
public class FormJS extends System.Windows.Forms.Form
{
//…
private void Form1_Paint(Object sender, PaintEventArgs e)
{
LinearGradientBrush brush = new LinearGradientBrush
(get_ClientRectangle(),Color.get_Red(),Color.get_White(),LinearGradientMode.ForwardDiagonal) ;
e.get_Graphics().FillRectangle(brush,get_ClientRectangle()) ;
}
protected void OnResize(System.EventArgs e)
{
this.Invalidate();
}
}
C++/CLI
//…
namespace WFmCpClr {
//…
using namespace System::Drawing::Drawing2D;
//…
public ref class FormVC : public System::Windows::Forms::Form
{
//…
private: System::Void FormVC_Paint
(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {
LinearGradientBrush^ brush = gcnew LinearGradientBrush
(ClientRectangle,Color::Olive,Color::White,LinearGradientMode::ForwardDiagonal) ;
e->Graphics->FillRectangle(brush,ClientRectangle) ;
}
private: System::Void FormVC_Resize(System::Object^ sender, System::EventArgs^ e) {
Invalidate();
}
};
}