M
Hi,
ich möchte dem TextBox-Rahmen eine andere Farbe geben (zum Beispiel rot).
Durch die Ableitung von der TextBox Klasse und Beobachtung des WM_NCPAINT Messages habe ich eine Lösung gebastelt, die aber ein kleines Problemchen mit dem Clientbereich hat:
wenn das Control upgedated wird, entstehen im Clientbereich am Rahmen zufällige Punkte.
So sieht das aus:
#pragma once
namespace TestTextBox
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Windows::Forms::VisualStyles;
using namespace System::Runtime::InteropServices;
/// <summary>Die Klasse unterdrückt die Bearbeitung der Evens Keys::Tab und Keys::Enter</summary>
public ref class MyTextBox : public System::Windows::Forms::TextBox
{
public:
[DllImport("USER32.DLL")]
static IntPtr GetWindowDC(IntPtr hWND);
[DllImport("USER32.DLL")]
static int ReleaseDC(IntPtr hWND, IntPtr hDC);
[DllImport("USER32.DLL")]
static IntPtr GetDCEx (IntPtr hWND, IntPtr hrgnClip, int flags);
[DllImport("GDI32.DLL")]
static IntPtr CreateRectRgn (int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
private:
static const Int32 WM_NCPAINT = 0x0085;
static const Int32 WM_NCCALCSIZE = 0x0083;
public: MyTextBox()
{
__super::TextBox();
m_ErrorFlag = false;
}
//[SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
virtual void WndProc (Message% m) override
{
switch ( m.Msg )
{
case WM_NCPAINT:
if (DrawNCBorder (m))
return;
break;
case WM_NCCALCSIZE:
break;
}
TextBox::WndProc( m );
}
private:
bool DrawNCBorder(Message% m)
{
IntPtr hDC = GetWindowDC (m.HWnd);
if (hDC.ToInt32() == 0)
return false;
Graphics^ gr = Graphics::FromHdc (hDC);
if (gr != nullptr)
{
Rectangle bounds(0, 0, this->Size.Width , this->Size.Height);
ControlPaint::DrawBorder (gr, bounds, Color::Red, ButtonBorderStyle::Solid);
m.Result = (IntPtr)1;
ReleaseDC (m.HWnd, hDC);
return true;
}
else
{
return false;
}
}
public:
bool m_ErrorFlag;
};
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: MyTextBox^ textBox1;
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->textBox1 = (gcnew MyTextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(33, 58);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 40);
this->textBox1->TabIndex = 0;
this->textBox1->RightToLeft = System::Windows::Forms::RightToLeft::Yes;
// button1
//
this->button1->Location = System::Drawing::Point(22, 141);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 266);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
};
}
Was fehlt der Lösung?