PDA

View Full Version : مبتدی: استفاده از حافظه كليپ بورد به صورت متوالي در دو برنامه مجزا



naser61nsn
سه شنبه 07 مهر 1394, 18:45 عصر
با سلام ، در نرم افزار مشتركين اداره ما ، هر 15 روز حدود هزار فرم قطع گاز صادر مي گردد و جهت هر شماره اشتراك يك فرم مجزا ثبت مي شود . اشتراك ها روي كاغذ چاپ شده و بايستي يكي يكي از روي كاغذ در فرم مربوطه وارد شود . چگونه مي توان به جاي وارد كردن شماره اشتراك ها از روي كاغذ ، با زدن كليد ctrl+V شماره اشتراك در فرم مربوطه درج گردد تا ثبت هزار اشتراك سريع تر انجام شود ؟ من در يك برنامه مجزا هزار اشتراك را در يك ديتاگريد لود كرده ام . چطور مي توان تك تك اشتراك ها از برنامه من ، به ترتيب در حافظه كليپ بورد لود و با زدن كليد ctrl+V در تكست باكس فرم قطع نرم افزار مشتركين گاز درج گردد ؟ باتشكر

naser61nsn
شنبه 11 مهر 1394, 04:52 صبح
درخواست مجدد

parvizwpf
شنبه 11 مهر 1394, 08:18 صبح
شما چرا میخوای کلیپ برد استفاده کنی خودت یه دکمه در برنامت تعبیه کن که همیشه آخرین شماره رو با زدن اون ببره تو حافظه و بعد بیاره تو تکست مثلا

naser61nsn
سه شنبه 14 مهر 1394, 21:19 عصر
با زدن مكرر دكمه در برنامه اول ، روند درج شماره در تكست باكس برنامه دوم طولاني خواهد شد . درج شماره در تكست باكس برنامه دوم ، بايستي با سرعت و با زدن متوالي كليد Ctrl+V صورت پذيرد و شماره ها يكي يكي و پشت سر هم در كليپ بورد لود و سپس در تكست باكس برنامه دوم درج بشه . پس از پيست شدن اولين شماره در برنامه دوم ، ساير داده ها وارد و فرم ثبت بشه و با زدن مجدد كليد Ctrl+V شماره بعدي از برنامه اول به برنامه دوم وارد شود . دوستان خوب همكار و برنامه نويس لطفاً راهنمايي كنيد .

aliagamon
چهارشنبه 15 مهر 1394, 01:09 صبح
برای اجرای چیزی که توی ذهنتونه میتونید یک global shortcut تنظیم کنید برای ctrl + V نمونه توی اینترنت و همین سایت زیاد هست چنتا رو براتون قرار میدم
http://www.dreamincode.net/forums/topic/180436-global-hotkeys/
http://stackoverflow.com/questions/81150/best-way-to-tackle-global-hotkey-processing-in-c
http://stackoverflow.com/questions/2450373/set-global-hotkeys-using-c-sharp
حالا تا اینجا شما یه ایونت میتونی بسازی که وقتی کلید های ctrl +V فشرده میشن بجای انجام عمل پیست تابعی که شما تعریف کردید اجرا خواهد شد.
حال لازم دارید که با هربار پیست به جای کلیپ برد کد ردیف مورد نظرتونو پیست کنه و یه ردیف بیاد پایین (برای سری بعد که پیست میخواید کنید.)
اینکارو باید باز هم با هوک کردن api های ویندوز انجام داد شما این کد رو به برنامتون اضافه کنید:

/// <summary>
/// Synthesizes keystrokes corresponding to the specified Unicode string,
/// sending them to the currently active window.
/// </summary>
/// <param name="s">The string to send.</param>
public static void SendString(string s)
{
// Construct list of inputs in order to send them through a single SendInput call at the end.
List<INPUT> inputs = new List<INPUT>();

// Loop through each Unicode character in the string.
foreach (char c in s)
{
// First send a key down, then a key up.
foreach (bool keyUp in new bool[] { false, true })
{
// INPUT is a multi-purpose structure which can be used
// for synthesizing keystrokes, mouse motions, and button clicks.
INPUT input = new INPUT
{
// Need a keyboard event.
type = INPUT_KEYBOARD,
u = new InputUnion
{
// KEYBDINPUT will contain all the information for a single keyboard event
// (more precisely, for a single key-down or key-up).
ki = new KEYBDINPUT
{
// Virtual-key code must be 0 since we are sending Unicode characters.
wVk = 0,

// The Unicode character to be sent.
wScan = c,

// Indicate that we are sending a Unicode character.
// Also indicate key-up on the second iteration.
dwFlags = KEYEVENTF_UNICODE | (keyUp ? KEYEVENTF_KEYUP : 0),

dwExtraInfo = GetMessageExtraInfo(),
}
}
};

// Add to the list (to be sent later).
inputs.Add(input);
}
}

// Send all inputs together using a Windows API call.
SendInput((uint)inputs.Count, inputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
}

const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_UNICODE = 0x0004;
const uint KEYEVENTF_SCANCODE = 0x0008;
const uint XBUTTON1 = 0x0001;
const uint XBUTTON2 = 0x0002;
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;

struct INPUT
{
public int type;
public InputUnion u;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
/*Virtual Key code. Must be from 1-254. If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.*/
public ushort wVk;
/*A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character which is to be sent to the foreground application.*/
public ushort wScan;
/*Specifies various aspects of a keystroke. See the KEYEVENTF_ constants for more information.*/
public uint dwFlags;
/*The time stamp for the event, in milliseconds. If this parameter is zero, the system will provide its own time stamp.*/
public uint time;
/*An additional value associated with the keystroke. Use the GetMessageExtraInfo function to obtain this information.*/
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}

[DllImport("user32.dll")]
static extern IntPtr GetMessageExtraInfo();

[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);



منبع کد بالا : stackoverflow.com/questions/26311047/get-any-input-focus-element-in-windows-with-c-sharp

حالا توی ایونتی که تعریف کردید با استفاده از این کد متن دلخواهتونو ارسال کنید:

SendString("سلام");

اینطوری با استفاده از ctrl + V پیست انجام میشه و با استفاده از راست کلیک و استفاده از گذینه ی پیست کلیپ برد کپی خواهد شد.
سوالی بود در خدمتم

naser61nsn
پنج شنبه 16 مهر 1394, 02:59 صبح
با راهنمايي ارزشمند جناب آقاي aliagamon ساعت 23 شروع و 4 بامداد نتيجه گرفتم . دعاي خير بنده را پذيرا باشيد و عمري با عزت داشته باشيد .
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public static string[] array1 = new string[10];
public static int i;

enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}

public Form1()
{
InitializeComponent();
RegisterHotKey(this.Handle, 0, (int)KeyModifier.Control , Keys.V.GetHashCode());
}

private void Form1_Load(object sender, EventArgs e)
{
array1[0] = "سلام"; array1[1] = " عليكم";
}

private void Searching()
{
SendString(array1[i]);
}

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
switch (id)
{
case 0: { Searching(); i++; } break;
}
}
}

public static void SendString(string s)
{
// Construct list of inputs in order to send them through a single SendInput call at the end.
List<INPUT> inputs = new List<INPUT>();

// Loop through each Unicode character in the string.
foreach (char c in s)
{
// First send a key down, then a key up.
foreach (bool keyUp in new bool[] { false, true })
{
// INPUT is a multi-purpose structure which can be used
// for synthesizing keystrokes, mouse motions, and button clicks.
INPUT input = new INPUT
{
// Need a keyboard event.
type = INPUT_KEYBOARD,
u = new InputUnion
{
// KEYBDINPUT will contain all the information for a single keyboard event
// (more precisely, for a single key-down or key-up).
ki = new KEYBDINPUT
{
// Virtual-key code must be 0 since we are sending Unicode characters.
wVk = 0,

// The Unicode character to be sent.
wScan = c,

// Indicate that we are sending a Unicode character.
// Also indicate key-up on the second iteration.
dwFlags = KEYEVENTF_UNICODE | (keyUp ? KEYEVENTF_KEYUP : 0),

dwExtraInfo = GetMessageExtraInfo(),
}
}
};

// Add to the list (to be sent later).
inputs.Add(input);
}
}

// Send all inputs together using a Windows API call.
SendInput((uint)inputs.Count, inputs.ToArray(), Marshal.SizeOf(typeof(INPUT)));
}

const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_UNICODE = 0x0004;
const uint KEYEVENTF_SCANCODE = 0x0008;
const uint XBUTTON1 = 0x0001;
const uint XBUTTON2 = 0x0002;
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;

struct INPUT
{
public int type;
public InputUnion u;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}

[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
public int dx;
public int dy;
public uint mouseData;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct KEYBDINPUT
{
/*Virtual Key code. Must be from 1-254. If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.*/
public ushort wVk;
/*A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character which is to be sent to the foreground application.*/
public ushort wScan;
/*Specifies various aspects of a keystroke. See the KEYEVENTF_ constants for more information.*/
public uint dwFlags;
/*The time stamp for the event, in milliseconds. If this parameter is zero, the system will provide its own time stamp.*/
public uint time;
/*An additional value associated with the keystroke. Use the GetMessageExtraInfo function to obtain this information.*/
public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public uint uMsg;
public ushort wParamL;
public ushort wParamH;
}

[DllImport("user32.dll")]
static extern IntPtr GetMessageExtraInfo();

[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
}
}

:تشویق: