📄 datagridimagecolumn.h
字号:
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::Data;
using namespace System::Collections;
namespace My
{
/// <summary>
/// DataGridImageColumn 摘要
/// </summary>
__gc public class DataGridImageColumn : public System::Windows::Forms::DataGridColumnStyle
{
private:
System::Windows::Forms::ImageList * imageList1;
System::Windows::Forms::Label * GridLabel;
int _xMargin;// = 2;
int _yMargin;// = 1;
bool _inEdit;//= false;
DataTable* _dataTable;
DataGrid* _dataGrid;
public:
DataGridImageColumn(void)
{
InitializeComponent();
}
DataGridImageColumn(System::ComponentModel::IContainer *container) : components(0)
{
/// <summary>
/// Windows.Forms 类撰写设计器支持所必需的
/// </summary>
container->Add(this);
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::ComponentModel::IContainer * components;
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
/// <summary>
/// 设计器支持所需的方法 - 不要用代码编辑器
///修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->components = new System::ComponentModel::Container();
}
//---------------------------成员方法------------------------------------
public:
DataGridImageColumn(String* colName, DataTable* dataSource,DataGrid* dataGrid,ImageList* _ImageList)
{
InitializeComponent();
_xMargin = 2;
_yMargin = 2;
_inEdit = false;
this->imageList1 = _ImageList;
//选中单元格时显示
this->GridLabel = new System::Windows::Forms::Label();
//
// GridLabel
//
this->GridLabel->ImageList = this->imageList1;
this->GridLabel->ImageIndex = 0;
this->GridLabel->Text = S"状态";
this->GridLabel->Width = 48;
this->GridLabel->Height = 48;
this->GridLabel->ImageAlign = System::Drawing::ContentAlignment::TopCenter;
this->GridLabel->TextAlign = System::Drawing::ContentAlignment::BottomCenter;
//this->GridLabel->BorderStyle = BorderStyle::Fixed3D;
this->GridLabel->Enabled = false;
//this->GridLabel->set_PreferredHeight = this->GridLabel->Height;
//this->GridLabel->PreferredWidth = this->GridLabel->Width;
this->GridLabel->Hide();
_dataTable = dataSource;
this->Width = this->GridLabel->Width;
this->MappingName = colName;
this->HeaderText = colName;
dataGrid->Controls->Add(this->GridLabel);
_dataGrid = dataGrid;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="rowNum"></param>
protected:
void Abort(int rowNum) {
_inEdit = false;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="dataSource"></param>
/// <param name="rowNum"></param>
/// <returns></returns>
bool Commit(CurrencyManager* dataSource,int rowNum)
{
return true;
}
/// <summary>
/// Standard override
/// </summary>
/// <returns></returns>
int GetMinimumHeight() {
return this->GridLabel->Height + _yMargin;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="val"></param>
/// <returns></returns>
int GetPreferredHeight(Graphics* g ,Object* val)
{
return this->GridLabel->Height + _yMargin;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="val"></param>
/// <returns></returns>
Size GetPreferredSize(Graphics* g, Object* val)
{
//Size _extents = Size::Ceiling(g->MeasureString(GetText(val), this->DataGridTableStyle->DataGrid->Font));
//_extents.Width += _xMargin * 2 ;
//_extents.Height += _yMargin;
return this->GridLabel->Size;//_extents;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="source"></param>
/// <param name="rowNum"></param>
/// <param name="bounds"></param>
/// <param name="readOnly"></param>
/// <param name="instantText"></param>
/// <param name="cellIsVisible"></param>
void Edit(CurrencyManager* source, int rowNum, Rectangle bounds, bool readOnly, String* instantText, bool cellIsVisible)
{
_inEdit = true;
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
/// <param name="source"></param>
/// <param name="rowNum"></param>
void Paint(Graphics* g,Rectangle bounds,CurrencyManager* source,int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
/// <summary>
/// Standard override
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
/// <param name="source"></param>
/// <param name="rowNum"></param>
/// <param name="alignToRight"></param>
void Paint(Graphics* g,Rectangle bounds,CurrencyManager* source,int rowNum, bool alignToRight)
{
System::Windows::Forms::Label* label = GetLabel(GetColumnValueAtRow(source, rowNum));
if(label != NULL)
{
//label->Bounds = bounds;
//label->Show();
g->FillRectangle(new SolidBrush(this->_dataGrid->BackColor),bounds);
g->DrawImage(label->Image,(int)((bounds.Width - label->Image->Width) / 2 + bounds.X),bounds.Y);
SizeF TextSize = (g->MeasureString(label->Text,label->Font));
g->DrawString(label->Text,label->Font,new SolidBrush(label->ForeColor),(float)((bounds.Width - TextSize.Width) / 2 + bounds.X),(float)(bounds.Y + label->Image->Height));
}
else
{
g->FillRectangle(new SolidBrush(this->_dataGrid->BackColor),bounds);
}
}
System::Windows::Forms::Label* GetLabel(Object* val)
{
if(val == System::DBNull::Value)
{
return NULL;
}
if(val != NULL)
{
return __try_cast<System::Windows::Forms::Label*>(val);
}
else
{
return NULL;
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -