Added open/close to test form.

This commit is contained in:
Eugene Wang 2023-04-01 19:38:04 -04:00
parent a779ee6b0e
commit 1417dbbb14
3 changed files with 123 additions and 22 deletions

View File

@ -35,11 +35,19 @@
lblCurrent = new System.Windows.Forms.Label();
btnEnumSources = new System.Windows.Forms.Button();
listSources = new System.Windows.Forms.ListBox();
btnSetDef = new System.Windows.Forms.Button();
btnOpen = new System.Windows.Forms.Button();
splitContainer1 = new System.Windows.Forms.SplitContainer();
btnClose = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit();
splitContainer1.Panel1.SuspendLayout();
splitContainer1.Panel2.SuspendLayout();
splitContainer1.SuspendLayout();
SuspendLayout();
//
// btnSelect
//
btnSelect.Location = new System.Drawing.Point(12, 12);
btnSelect.Location = new System.Drawing.Point(13, 14);
btnSelect.Name = "btnSelect";
btnSelect.Size = new System.Drawing.Size(151, 23);
btnSelect.TabIndex = 0;
@ -50,7 +58,7 @@
// label1
//
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(12, 51);
label1.Location = new System.Drawing.Point(13, 53);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(98, 15);
label1.TabIndex = 1;
@ -59,7 +67,7 @@
// lblDefault
//
lblDefault.AutoSize = true;
lblDefault.Location = new System.Drawing.Point(115, 51);
lblDefault.Location = new System.Drawing.Point(116, 53);
lblDefault.Name = "lblDefault";
lblDefault.Size = new System.Drawing.Size(24, 15);
lblDefault.TabIndex = 2;
@ -68,7 +76,7 @@
// label2
//
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(12, 81);
label2.Location = new System.Drawing.Point(13, 14);
label2.Name = "label2";
label2.Size = new System.Drawing.Size(97, 15);
label2.TabIndex = 3;
@ -77,7 +85,7 @@
// lblCurrent
//
lblCurrent.AutoSize = true;
lblCurrent.Location = new System.Drawing.Point(115, 81);
lblCurrent.Location = new System.Drawing.Point(116, 14);
lblCurrent.Name = "lblCurrent";
lblCurrent.Size = new System.Drawing.Size(24, 15);
lblCurrent.TabIndex = 4;
@ -85,7 +93,7 @@
//
// btnEnumSources
//
btnEnumSources.Location = new System.Drawing.Point(12, 114);
btnEnumSources.Location = new System.Drawing.Point(13, 81);
btnEnumSources.Name = "btnEnumSources";
btnEnumSources.Size = new System.Drawing.Size(151, 23);
btnEnumSources.TabIndex = 5;
@ -95,30 +103,87 @@
//
// listSources
//
listSources.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
listSources.Anchor = System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
listSources.FormattingEnabled = true;
listSources.ItemHeight = 15;
listSources.Location = new System.Drawing.Point(12, 143);
listSources.Location = new System.Drawing.Point(13, 115);
listSources.Name = "listSources";
listSources.Size = new System.Drawing.Size(279, 289);
listSources.Size = new System.Drawing.Size(289, 394);
listSources.TabIndex = 6;
//
// btnSetDef
//
btnSetDef.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
btnSetDef.Location = new System.Drawing.Point(12, 515);
btnSetDef.Name = "btnSetDef";
btnSetDef.Size = new System.Drawing.Size(169, 23);
btnSetDef.TabIndex = 7;
btnSetDef.Text = "Set selected as default";
btnSetDef.UseVisualStyleBackColor = true;
btnSetDef.Click += btnSetDef_Click;
//
// btnOpen
//
btnOpen.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
btnOpen.Location = new System.Drawing.Point(187, 515);
btnOpen.Name = "btnOpen";
btnOpen.Size = new System.Drawing.Size(104, 23);
btnOpen.TabIndex = 8;
btnOpen.Text = "Open selected";
btnOpen.UseVisualStyleBackColor = true;
btnOpen.Click += btnOpen_Click;
//
// splitContainer1
//
splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
splitContainer1.Location = new System.Drawing.Point(0, 0);
splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
splitContainer1.Panel1.Controls.Add(btnSelect);
splitContainer1.Panel1.Controls.Add(btnOpen);
splitContainer1.Panel1.Controls.Add(label1);
splitContainer1.Panel1.Controls.Add(btnSetDef);
splitContainer1.Panel1.Controls.Add(lblDefault);
splitContainer1.Panel1.Controls.Add(listSources);
splitContainer1.Panel1.Controls.Add(btnEnumSources);
//
// splitContainer1.Panel2
//
splitContainer1.Panel2.Controls.Add(btnClose);
splitContainer1.Panel2.Controls.Add(label2);
splitContainer1.Panel2.Controls.Add(lblCurrent);
splitContainer1.Size = new System.Drawing.Size(800, 564);
splitContainer1.SplitterDistance = 305;
splitContainer1.TabIndex = 9;
//
// btnClose
//
btnClose.Location = new System.Drawing.Point(13, 45);
btnClose.Name = "btnClose";
btnClose.Size = new System.Drawing.Size(142, 23);
btnClose.TabIndex = 0;
btnClose.Text = "Close current source";
btnClose.UseVisualStyleBackColor = true;
btnClose.Click += btnClose_Click;
//
// Form1
//
AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(800, 450);
Controls.Add(listSources);
Controls.Add(btnEnumSources);
Controls.Add(lblCurrent);
Controls.Add(label2);
Controls.Add(lblDefault);
Controls.Add(label1);
Controls.Add(btnSelect);
ClientSize = new System.Drawing.Size(800, 564);
Controls.Add(splitContainer1);
Name = "Form1";
Text = "TWAIN Test";
splitContainer1.Panel1.ResumeLayout(false);
splitContainer1.Panel1.PerformLayout();
splitContainer1.Panel2.ResumeLayout(false);
splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit();
splitContainer1.ResumeLayout(false);
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -130,5 +195,9 @@
private System.Windows.Forms.Label lblCurrent;
private System.Windows.Forms.Button btnEnumSources;
private System.Windows.Forms.ListBox listSources;
private System.Windows.Forms.Button btnSetDef;
private System.Windows.Forms.Button btnOpen;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.Button btnClose;
}
}

View File

@ -15,7 +15,7 @@ namespace WinForm32
public Form1()
{
InitializeComponent();
Text += TwainPlatform.Is32bit ? " 32bit" : " 64bit";
TwainPlatform.PreferLegacyDSM = true;
twain = new TwainSession(Assembly.GetExecutingAssembly().Location);
@ -70,5 +70,26 @@ namespace WinForm32
listSources.Items.Add(ds);
}
}
private void btnSetDef_Click(object sender, EventArgs e)
{
if (listSources.SelectedItem is TW_IDENTITY_LEGACY ds)
{
twain.DGControl.Identity.Set(ds);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (listSources.SelectedItem is TW_IDENTITY_LEGACY ds)
{
twain.DGControl.Identity.OpenDS(ds);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
twain.DGControl.Identity.CloseDS();
}
}
}

View File

@ -92,10 +92,21 @@ namespace NTwain
/// <returns>The final state.</returns>
public STATE TryStepdown(STATE targetState)
{
if (State > targetState)
int tries = 0;
while (State > targetState)
{
// shouldn't care about handle when closing really
DGControl.Parent.CloseDSM(ref _hwnd);
if (tries++ > 5) break;
switch (State)
{
case STATE.S4:
DGControl.Identity.CloseDS();
break;
case STATE.S3:
// shouldn't care about handle when closing really
DGControl.Parent.CloseDSM(ref _hwnd);
break;
}
}
return State;
}