jiangsheng

http://www.csdn.net/develop/author/netauthor/jiangsheng/
随笔 - 142, 评论 - 629, 引用 - 27

导航

关于

 
这下要维护3个BLOG了,faint 其他的地址:

所有的文章版权归原文作者所有,任何人需要转载文章,必须征得原文作者授权。
我的MVP配置

标签

每月存档

最新留言

广告

获取指定范围内的随机数

CRT函数rand()返回一个整型随机数,在0到RAND_MAX之间平均分布。根据数学上的随机数理论,可以运用线性变换把整型随机数转化为指定范围内的随机数。如果对精度要求比较高,可以用浮点数或者双精度数进行这个线性变换:

#define GetRandom(min,max) rand()*((float)((max)-(min)))/(float)(RAND_MAX)+(min)

http://msdn.microsoft.com/library/en-us/vclib/html/_crt__beginthread.2c_._beginthreadex.asp中的求余变换
/* GetRandom returns a random integer between min and max. */
#define GetRandom( min, max ) ((rand() % (int)(((max) + 1) - (min))) + (min))
有可能存在一定的误差。实际上,这个求余算法只是把(0,RAND_MAX-(RAND_MAX% (int)(((max) + 1) - (min))))线性变换到( min, max ) 而已,误差的数学期望是((RAND_MAX % (int)(((max) + 1) - (min)))/RAND_MAX。如果RAND_MAX可以被范围整除,那么这个算法没有误差;在范围很小,并且min和max都是整数的时候,由于分母是RAND_MAX,所以这个算法的误差甚至可能比浮点数运算的舍入误差还要小。综上所述,上面的求余变换在指定的范围很小的时候可以作为一种可行的随机数产生方法。

posted on 2005-01-22 14:38:00 by jiangsheng  评论(3) 阅读(4378)

选择列功能的实现

在列表视图控件包含的列很多的时候,允许用户指定某些列是否显示是很有用的。大家在资源管理器和任务管理器中已经看见过这样的功能。在自己的程序中,使用一个虚列表视图实现这样的列也很容易。

首先定义以下概念:

  • 逻辑列
    表示数据的逻辑顺序。
  • 显示列
    表示数据在列表视图中的显示的顺序。用户可能自定义这个次序。

然后设计一个显示列和逻辑列之间的映射,在逻辑列和显示列之间互相转换。在示例代码中,我使用了两个数组,通过查表来进行有效地转换。

显示单元格数据的时候,需要首先查表获得对应的逻辑列,然后根据指定的行和获得的逻辑列计算需要显示的内容。这里我简单地对行和列进行格式化作为单元格的内容。

用户可能通过拖动列标题、列标题菜单或者列设置对话框来更改列的显示顺序和是否显示。这里我使用一个结构来保存列的设置

public value class DisplayColumnInfo
{
??? public:
??? int logicalIndex;
??? System::String^ text;
??? int width;
??? bool show;
};

为了避免在更改列设置时进行内存分配,使用了一个固定大小的数组来保存显示列设置,logicalIndex=-1时表示指定的显示列未用。

在自定义列的时候,考虑到之前用户可能拖动了列标题来更改显示列的显示顺序,而用户会希望在编辑时和自定义列设置之后保持这个顺序;所以要根据用户修改的显示列顺序重新进行显示列和逻辑列的映射。
??? array^ arOrder;
??? arOrder=gcnew array((int)ListView1Columns::ColumnCount);
??? for(int i=0;i<(int)ListView1Columns::ColumnCount;i++)
??? arOrder[i]=-1;
??? pin_ptr pp = &arOrder[0];
??? //pass a managed array to an unmanaged function?
??? SendMessage((HWND)this->listView1->Handle.ToInt32(),LVM_GETCOLUMNORDERARRAY,(int)ListView1Columns::ColumnCount,(LPARAM)(int*)pp);

??? DlgSelectColumn^ dlg=gcnew DlgSelectColumn();
??? dlg->m_arColumnInfo=gcnew array((int)ListView1Columns::ColumnCount);

?????for(int i=0;i<(int) ListView1Columns::ColumnCount;i++)
??? {
??????? if(arOrder[i]!=-1)
??????????? dlg->m_arColumnInfo[i]=m_arColumnInfo[arOrder[i]];
??????? else
??????????? dlg->m_arColumnInfo[i]=m_arColumnInfo[i];
??? }

这样,在列设置对话框中显示的顺序就是用户自定义过的了。考虑到用户可能增加或者减少显示列的数目,插入列之后再使用LVM_SETCOLUMNORDERARRAY恢复显示列的顺序是比较麻烦的,所以我在重新插入列的时候就更改了显示列的顺序,这样只需要更改显示列对应的逻辑列索引就可以更新显示了。

本文的代码在http://blog.joycode.com/jiangsheng/archive/2005/01/06/42506.aspx 可以看到。使用Visual C++ 2005 Express编译通过。

posted on 2005-01-07 09:13:00 by jiangsheng  评论(1) 阅读(3233)

Source Code of Select Column in a ListView control

<--Back To Article
#pragma once
#include "DlgSelectColumn.h"

namespace SelectColumn
{
    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 stdcli::language;

public value class DisplayColumnInfo
{
public:
int logicalIndex;
System::String^ text;
int width;
bool show;
};

 

enum class ListView1Columns
{
ColumnCount=10
};

/// /// Summary for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // m_arColumnInfo=gcnew array((int)ListView1Columns::ColumnCount); for(int i=0;i<(int)ListView1Columns::ColumnCount;i++) { m_arColumnInfo[i].logicalIndex=i; m_arColumnInfo[i].text=listView1_GetColumnText(i); m_arColumnInfo[i].show=true; m_arColumnInfo[i].width=-1; } m_arDC2LC=gcnew array((int)ListView1Columns::ColumnCount); m_arLC2DC=gcnew array((int)ListView1Columns::ColumnCount); } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::ListView^ listView1; //actural column information array^ m_arColumnInfo; //cached information for convertion between display column and logic column array^ m_arDC2LC; array^ m_arLC2DC; private: /// /// Required designer variable. /// System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->listView1 = gcnew System::Windows::Forms::ListView(); this->SuspendLayout(); this->listView1->Activation = System::Windows::Forms::ItemActivation::OneClick; this->listView1->AllowColumnReorder = true; this->listView1->Dock = System::Windows::Forms::DockStyle::Fill; this->listView1->FullRowSelect = true; this->listView1->GridLines = true; this->listView1->HeaderStyle = System::Windows::Forms::ColumnHeaderStyle::Nonclickable; this->listView1->HideSelection = false; this->listView1->HotTracking = true; this->listView1->HoverSelection = true; this->listView1->Location = System::Drawing::Point(0, 0); this->listView1->Name = L"listView1"; this->listView1->ShowItemToolTips = true; this->listView1->Size = System::Drawing::Size(493, 385); this->listView1->TabIndex = 0; this->listView1->View = System::Windows::Forms::View::Details; this->listView1->VirtualListSize = 10000; this->listView1->VirtualMode = true; this->listView1->Click += gcnew System::EventHandler(this, &Form1::listView1_Click); this->listView1->RetrieveVirtualItem += gcnew System::Windows::Forms::RetrieveVirtualItemEventHandler(this, &Form1::listView1_RetrieveVirtualItem); this->AutoScaleBaseSize = System::Drawing::Size(6, 14); this->ClientSize = System::Drawing::Size(493, 385); this->Controls->Add(this->listView1); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); } #pragma endregion private: System::Void listView1_RetrieveVirtualItem(System::Object^ sender, System::Windows::Forms::RetrieveVirtualItemEventArgs^ e) { e->Item=gcnew System::Windows::Forms::ListViewItem ; for(int displaycolumn=0;displaycolumn<(int)ListView1Columns::ColumnCount;displaycolumn++) { int logiccolumn=DC2LC(displaycolumn); if(displaycolumn==-1)continue; if(displaycolumn==0) { e->Item->Text=listView1_GetItemText(e->ItemIndex,logiccolumn); } else { e->Item->SubItems->Add(listView1_GetItemText(e->ItemIndex,logiccolumn)); } } } private: System::String^ listView1_GetItemText(int item,int logicalColumn) { return String::Format("X{0:d},Y{1:d}",logicalColumn,item); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { this->listView1->Hide(); listView1_RefreshColumns(); } private: System::Void listView1_RefreshColumns() { this->listView1->Hide(); this->listView1->Columns->Clear(); for(int i=0;i<(int)ListView1Columns::ColumnCount;i++) { if(m_arColumnInfo[i].show) { System::Windows::Forms::ColumnHeader^ columnHeader1; columnHeader1 = gcnew System::Windows::Forms::ColumnHeader(L""); columnHeader1->Text=m_arColumnInfo[i].text; if(m_arColumnInfo[i].width!=-1) columnHeader1->Width=m_arColumnInfo[i].width; int displaycolumn=this->listView1->Columns->Add(columnHeader1); m_arDC2LC[m_arColumnInfo[displaycolumn].logicalIndex]=displaycolumn; m_arLC2DC[displaycolumn]=m_arColumnInfo[displaycolumn].logicalIndex; } else { m_arDC2LC[i]=-1; m_arLC2DC[i]=-1; } } this->listView1->Show(); } private: System::String^ listView1_GetColumnText(int logicalColumn) { return String::Format("Column{0:d}",logicalColumn); } private: System::Void listView1_Click(System::Object^ sender, System::EventArgs^ e) { //column order can be rearranged by user //get column order array^ arOrder; arOrder=gcnew array((int)ListView1Columns::ColumnCount); for(int i=0;i<(int)ListView1Columns::ColumnCount;i++) arOrder[i]=-1; pin_ptr pp = &arOrder[0]; //pass a managed array to an unmanaged function SendMessage((HWND)this->listView1->Handle.ToInt32(), LVM_GETCOLUMNORDERARRAY,(int)ListView1Columns::ColumnCount,(LPARAM)(int*)pp); DlgSelectColumn^ dlg=gcnew DlgSelectColumn(); dlg->m_arColumnInfo=gcnew array((int)ListView1Columns::ColumnCount); //apply order before editing for(int i=0;i<(int)ListView1Columns::ColumnCount;i++) { if(arOrder[i]!=-1) { dlg->m_arColumnInfo[i]=m_arColumnInfo[arOrder[i]]; } else dlg->m_arColumnInfo[i]=m_arColumnInfo[i]; } System::Windows::Forms::DialogResult ds=dlg->ShowDialog(); if(ds==System::Windows::Forms::DialogResult::OK) { for(int i=0;i<(int)ListView1Columns::ColumnCount;i++) { m_arColumnInfo[i]=dlg->m_arColumnInfo[i]; } listView1_RefreshColumns(); } } private: int DC2LC(int displaycolumn) { if(m_arDC2LC!=nullptr &&displaycolumn>=0 &&displaycolumn<=(int)ListView1Columns::ColumnCount) return m_arDC2LC[displaycolumn]; return -1; } private: int LC2DC(int logicalcolumn) { if(m_arLC2DC!=nullptr &&logicalcolumn>=0 &&logicalcolumn<=(int)ListView1Columns::ColumnCount) return m_arLC2DC[logicalcolumn]; return -1; } }; }

posted on 2005-01-07 08:14:00 by jiangsheng  评论(1) 阅读(4694)

Powered by: Joycode.MVC引擎 0.5.2.0