الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كود سي# محول من VB ولا يعمل

مغلق
بدأه ADEL في 24 أغسطس 2004 · 2 رد · 1,035 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هاذا الكود حولته من VB.NET الي سي# ولكنه لم يعمل فهل من مساعدة ..

public class frmMain : System.Windows.Forms.Form 
{ 
 [System.Runtime.InteropServices.DllImport("user32", EntryPoint="SetWindowsHookExA")] 
 int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId); 
 [System.Runtime.InteropServices.DllImport("user32", EntryPoint="UnhookWindowsHookEx")] 
 int UnhookWindowsHookEx(int hHook); 
 delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); 
 [System.Runtime.InteropServices.DllImport("user32", EntryPoint="CallNextHookEx")] 
 int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); 
 struct KBDLLHOOKSTRUCT 
 { 
   int vkCode; 
   int scanCode; 
   int flags; 
   int time; 
   int dwExtraInfo; 
 } 
 int intLLKey; 

 public frmMain() 
 { 
   base.New(); 
   InitializeComponent(); 
 } 

 protected override void Dispose(bool disposing) 
 { 
   if (disposing) { 
     if (!(components == null)) { 
       components.Dispose(); 
     } 
   } 
   base.Dispose(disposing); 
 } 
 private System.ComponentModel.IContainer components; 

 [System.Diagnostics.DebuggerStepThrough()] 
 private void InitializeComponent() 
 { 
   this.lblInfo = new System.Windows.Forms.Label(); 
   this.SuspendLayout(); 
   this.lblInfo.Location = new System.Drawing.Point(16, 16); 
   this.lblInfo.Name = "lblInfo"; 
   this.lblInfo.Size = new System.Drawing.Size(216, 88); 
   this.lblInfo.TabIndex = 0; 
   this.lblInfo.Text = "Alt-Tab, Alt-Esc, Ctrl-Esc, and the Windows key are disabled. Ctrl-Alt-Del and Al" + "t-F4 are still enabled. Close the application to reset the keyboard. While this " + "program is running, the low-level keyboard effect is global."; 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
   this.ClientSize = new System.Drawing.Size(248, 118); 
   this.Controls.AddRange(new System.Windows.Forms.Control[] {this.lblInfo}); 
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 
   this.MaximizeBox = false; 
   this.MinimizeBox = false; 
   this.Name = "frmMain"; 
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
   this.Text = "LowLevel Keyboard Hook"; 
   this.ResumeLayout(false); 
 } 

 private void frmMain_Load(object sender, System.EventArgs e) 
 { 
   intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, /* might be wrong, please check */ new EventHandler(LowLevelKeyboardProc), System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32(), 0); 
 } 

 private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
 { 
   UnhookWindowsHookEx(intLLKey); 
 } 

 private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam) 
 { 
   bool blnEat = false; 
   if (wParam == 256 || wParam == 257 || wParam == 260 || wParam == 261) { 
     blnEat = ((lParam.vkCode == 9) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 0)) | ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1)); 
   } 
   if (blnEat == true) { 
     return 1; 
   } else { 
     return CallNextHookEx(0, nCode, wParam, lParam); 
   } 
 } 
 internal System.Windows.Forms.Label lblInfo; 
}
#2

???Please include more info, post the VB.net code as well as what's supposed to be done with this code

رأس الحكمة الإيمان بالله

#3

ADEL,

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmMain : System.Windows.Forms.Form 
	{ 
  [STAThread]
  public static void Main()
  {
 	 Application.Run( new frmMain() );
  }
  
  const int WH_KEYBOARD_LL = 13;
  delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); 
  
  [DllImport("user32", EntryPoint="SetWindowsHookExA")] 
  extern static int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId); 
  [DllImport("user32", EntryPoint="UnhookWindowsHookEx")] 
  extern static int UnhookWindowsHookEx(int hHook); 
  [DllImport("user32", EntryPoint="CallNextHookEx")] 
  extern static int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam); 
  struct KBDLLHOOKSTRUCT 
  { 
 	 public int vkCode; 
 	 public int scanCode; 
 	 public int flags; 
 	 public int time; 
 	 public int dwExtraInfo; 
  } 
  int intLLKey; 

  public frmMain() 
  { 
 	 InitializeComponent(); 
  } 

  protected override void Dispose(bool disposing) 
  { 
 	 if (disposing) 
 	 { 
    if (!(components == null)) 
    { 
   	 components.Dispose(); 
    } 
 	 } 
 	 base.Dispose(disposing); 
  } 
  private System.ComponentModel.IContainer components; 

  [System.Diagnostics.DebuggerStepThrough()] 
  private void InitializeComponent() 
  { 
 	 this.lblInfo = new System.Windows.Forms.Label();
 	 this.SuspendLayout();
 	 // 
 	 // lblInfo
 	 // 
 	 this.lblInfo.Location = new System.Drawing.Point(16, 16);
 	 this.lblInfo.Name = "lblInfo";
 	 this.lblInfo.Size = new System.Drawing.Size(216, 88);
 	 this.lblInfo.TabIndex = 0;
 	 this.lblInfo.Text = "Alt-Tab, Alt-Esc, Ctrl-Esc, and the Windows key are disabled. Ctrl-Alt-Del and Al" +
    "t-F4 are still enabled. Close the application to reset the keyboard. While this " +
    "program is running, the low-level keyboard effect is global.";
 	 // 
 	 // frmMain
 	 // 
 	 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
 	 this.ClientSize = new System.Drawing.Size(248, 118);
 	 this.Controls.Add(this.lblInfo);
 	 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
 	 this.MaximizeBox = false;
 	 this.MinimizeBox = false;
 	 this.Name = "frmMain";
 	 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
 	 this.Text = "LowLevel Keyboard Hook";
 	 this.Closing += new System.ComponentModel.CancelEventHandler(this.frmMain_Closing);
 	 this.Load += new System.EventHandler(this.frmMain_Load);
 	 this.ResumeLayout(false);

  } 

  private void frmMain_Load(object sender, System.EventArgs e) 
  { 
 	 intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, /* might be wrong, please check */ new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc), System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0); 
  } 

  private void frmMain_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
  { 
 	 UnhookWindowsHookEx(intLLKey); 
  } 

  private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam) 
  { 
 	 bool blnEat = false; 
 	 if (wParam == 256 || wParam == 257 || wParam == 260 || wParam == 261) 
 	 { 
    blnEat = ((lParam.vkCode == 9) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 32)) | ((lParam.vkCode == 27) && (lParam.flags == 0)) | ((lParam.vkCode == 91) && (lParam.flags == 1)) | ((lParam.vkCode == 92) && (lParam.flags == 1)); 
 	 } 
 	 if (blnEat == true) 
 	 { 
    return 1; 
 	 } 
 	 else 
 	 { 
    return CallNextHookEx(0, nCode, wParam, ref lParam); 
 	 } 
  } 
  internal System.Windows.Forms.Label lblInfo; 
	}

}

هذا الموضوع مغلق.

مواضيع مشابهة