|
Thanks!
|
|
|
|
|
Good morning.
I am trying to detect the music button events from the keyboard, but have been unsuccessful so far. I have the following code:
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;
using System.IO;
using System.Collections;
namespace KPlayer
{
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wmp = new WMPLib.WindowsMediaPlayer();
WMPLib.IWMPPlaylist pl;
WMPLib.IWMPMedia media;
ArrayList ar = new ArrayList();
int intCount = 0;
List<int> numbers = new List<int>();
float currentSize = 10;
string currentFont = "DS-Digital";
public Form1()
{
InitializeComponent();
}
[STAThread]
[DllImport("user32.dll")]
public static extern int SetWindowsHookEx(int hookType,
HookProc callback,
int instance,
int threadID);
[DllImport("user32.dll")]
public static extern int CallNextHookEx(int hookHandle, int code,
int wparam, int lparam);
[DllImport("user32.dll")]
public static extern bool UnhookWindowsHookEx(int hookHandle);
[DllImport("user32.dll")]
public static extern int GetKeyState(int vKey);
int hookHandle = 0;
HookProc cb = null;
public const int WH_KEYBOARD = 2;
public const int HC_ACTION = 0;
public const int HC_NOREMOVE = 3;
public const int VK_CONTROL = 0x11;
public const int VK_LWIN = 0x5B;
public const int VK_RWIN = 0x5C;
public const int VK_APPS = 0x5D;
public const int VK_LSHIFT = 0xA0;
public const int VK_RSHIFT = 0xA1;
public const int VK_LCONTROL = 0xA2;
public const int VK_RCONTROL = 0xA3;
public const int VK_LMENU = 0xA4;
public const int VK_RMENU = 0xA5;
public const int VK_BROWSER_BACK = 0xA6;
public const int VK_BROWSER_FORWARD = 0xA7;
public const int VK_BROWSER_REFRESH = 0xA8;
public const int VK_BROWSER_STOP = 0xA9;
public const int VK_BROWSER_SEARCH = 0xAA;
public const int VK_VOLUME_MUTE = 0xAD;
public const int VK_VOLUME_DOWN = 0xAE;
public const int VK_VOLUME_UP = 0xAF;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public const int VK_MEDIA_STOP = 0xB2;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int KF_UP = 0x8000;
public const long KB_TRANSITION_FLAG = 0x80000000;
public const int VK_W = 0x57;
public delegate int HookProc(int code, int wparam, int lparam);
public int Proc(int code, int wparam, int lparam)
{
if (code == HC_ACTION)
{
switch (wparam)
{
case VK_BROWSER_BACK:
textBox1.Text += "Browser Back key caught" +
Environment.NewLine;
break;
case VK_BROWSER_FORWARD:
textBox1.Text += "Browser Forward key caught" +
Environment.NewLine;
break;
case VK_BROWSER_REFRESH:
textBox1.Text += "Browser Refresh key caught" +
Environment.NewLine;
break;
case VK_BROWSER_STOP:
textBox1.Text += "Browser Stop key caught" +
Environment.NewLine;
break;
case VK_BROWSER_SEARCH:
textBox1.Text += "Browser Search key caught" +
Environment.NewLine;
break;
case VK_VOLUME_MUTE:
textBox1.Text += "Volume Mute key caught" +
Environment.NewLine;
break;
case VK_VOLUME_DOWN:
textBox1.Text += "Volume Down key caught" +
Environment.NewLine;
break;
case VK_VOLUME_UP:
textBox1.Text += "Volume Up key caught" +
Environment.NewLine;
break;
case VK_MEDIA_NEXT_TRACK:
textBox1.Text += "Media Next Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PREV_TRACK:
textBox1.Text += "Media Previous Track key caught" +
Environment.NewLine;
break;
case VK_MEDIA_STOP:
textBox1.Text += "Media Stop key caught" +
Environment.NewLine;
break;
case VK_MEDIA_PLAY_PAUSE:
textBox1.Text += "Media Play/Pause key caught" +
Environment.NewLine;
break;
}
}
return (CallNextHookEx(hookHandle, code, wparam, lparam));
}
private void Form1_Load(object sender, EventArgs e)
{
wmp.PlayStateChange +=
new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
wmp.MediaError +=
new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
}
I don't get any errors, but it doesn't appear to work. Any ideas?
WHEELS
|
|
|
|
|
Can you please post your call to SetWindowsHookEx.
What hook are you using WH_KEYBOARD or WH_KEYBOARD_LL?
Because the function delegate called by the hook (Proc in your case)
can be called from threads/processes beyond your scope, you should not
do this:
textBox1.Text += "Browser Back key caught" + Environment.NewLine;
Use an invoke or for testing purposes just trace.
Greetings
Covean
|
|
|
|
|
Hi Covean.
That is all the "hook" code I have. I am not exactly clear what you are asking for. WHEELS
|
|
|
|
|
Ok so this will be the problem. ^^
Without calling SetWindowsHookEx how should the system know what you wanna hook?
Somewhere you should call (for example at form init):
IntPtr hHook = SetWindowsHookEx(WH_KEYBOARD or WH_KEYBOARD_LL(for low-level hook), Proc (delegate to your hook proc), Process.GetCurrentProcess().Handle, 0);
if(hHook == IntPtr.Zero)
{
// hook failed
}
At destruction time call UnhookWindowsHookEx(hHook);
Greetings
Covean
|
|
|
|
|
Now I understand. I have the following:
private void button1_Click(object sender, System.EventArgs e)
{
if (hookHandle == 0)
{
cb = new HookProc(Proc);
hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0,
appdomain.GetCurrentThreadId( ));
}
else
{
textBox1.Text += "Hook already set" + Environment.NewLine;
}
textBox1.Text += "Start: " + hookHandle + Environment.NewLine;
}
private void button2_Click(object sender, System.EventArgs e)
{
textBox1.Text += "End: " + UnhookWindowsHookEx(hookHandle) +
Environment.NewLine;
hookHandle = 0;
}
I am coming up with an error on this line:
hookHandle = SetWindowsHookEx(WH_KEYBOARD, cb, 0,
appdomain.GetCurrentThreadId( ));
appdomain doesn't exist in the current context.
WHEELS
|
|
|
|
|
If you want to hook more than the input to your application, you should set dwThreadId to 0 and just set the instance handle (3rd parameter) to Process.GetCurrentProcess().Handle.
Btw: appdomain does not exists its called AppDomain.
Greetings
Covean
|
|
|
|
|
Still get same error when I capitalize appDomain. WHEELS
|
|
|
|
|
Wheels012 wrote: Still get same error when I capitalize appDomain. WHEELS
Still wrong. There are 2 letters to capatilaze: "AppDomain"
|
|
|
|
|
Hello All…
Hope all is well with you..
Actually I want to write a OutlookAddin it will add a simple dropdown combo box after the subject input area.... Can anyone give me some guideline..Please note that a have done this but the problem is it load at the end of the form.
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
it is a windows application
I want to validate the datagridview cell ,it has to accepct only alphabets and numerics
HFGHFGHFGHFGHFHFHFHFHFHFHFHFHF
|
|
|
|
|
i have program that search for white point in screen, the code is:
namespace CameraLab
{
public class CamManeger
{
Point pp;
public Point PP
{
get
{
return pp;
}
set
{
pp = value;
}
}
[DllImport("user32.dll")]
public static extern void mouse_event(MouseEventType dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32")]
public static extern int SetCursorPos(int x, int y);
public enum MouseEventType : int
{
LeftDown = 0x02,
LeftUp = 0x04,
RightDown = 0x08,
RightUp = 0x10
}
public void test(Bitmap bm, int X, int Y, int x, int y ,Point pp,bool L)
{
bool z = false;
for (int i = X; i < x; i++)
{
for (int j = Y; j < y; j++)
{
if (bm.GetPixel(i, j).R == 255 && bm.GetPixel(i, j).G == 255 && bm.GetPixel(i, j).B == 255 && bm.GetPixel(i, j).R == 255)
{
Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - i, Screen.PrimaryScreen.WorkingArea.Height - j);
Cursor.Position = p;
this.PP = p;
z = true;
break;
}
}
if (z == true)
{
break;
}
}
}
}
}
can i make this code more faster.
|
|
|
|
|
Edit your code and add <pre> tags, because i can very hardly to read
The only thing you could improve (as i see), modify your if statement. But you won't get performance boost
if (bm.GetPixel(i, j) == Color.White)
{
}
Maybe you shoud look into directly into unmanaged GDI. (GDI+ is only a wraper around unmanaged GDI)
Edit: Styleing
modified on Tuesday, January 5, 2010 10:59 AM
|
|
|
|
|
GetPixel is seriously slow. Take a look at this article:
ImageTraverser[^]
Regards,
Rob Philpott.
|
|
|
|
|
|
Sometimes I need to determine the color of different controls. I give a couple a of examples. In a MessageBox I needed a custom made gif-icon and I wanted the background color of the icon to match the color of the MessageBox itself. In another project I wanted to get rid of the little sort-array in the header of a DataGridView and to do that I had to override the Paint-method and paint the arrow with the background color. To determine the colors in these two examples I did a print screen and pasted the screen dump into Paint and used the color picker to get the RGB-values. This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer. Something tells me there must be a way to query the color to use instead of determining it by hand and then hardcoding it. How do I do this?
|
|
|
|
|
Gif supports transparency, can't you make the background transparent in your gif images? Would this do what you need?
|
|
|
|
|
Well, in the specific case with a gif-icon I could use transparency. However, I would still like to know how I can determine the color programmatically.
|
|
|
|
|
you could atempt use the folowing
Bitmap bmp;
// <snip>intelizing bmp
if (bmp.GetPixel(x,y) == Color.White) // Color has predefined colors. SystemColor has a system setting colors as predefind
{
}
|
|
|
|
|
I don't understand how to use this. Can you please give an example how I can use it when I override the Paint-method for DataGridViewColumnHeaderCell:
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
|
|
|
|
|
I actually don't remember Paint method has this many parameters. I assume you are inherited from DataGridView
The method I posted was intended from thread above yours. I posted in wrong but I left it because you asked a question in that post that could be correct answer.
It is better to get color from propties instead atempt to read from a point on the screan
DataGridView background color is default to SystemColors.AppWorkspace.
So I suggest you use to get your background color:
this.BackgroundColor
modified on Tuesday, January 5, 2010 10:57 AM
|
|
|
|
|
arnold_w wrote: This works fine on most computer, but I have noticed that on some computers (depending on operating system and other things) the gray color is not the same as on my computer.
That could be cousing also by monitors. Some have Color temperature (or even named as Color Tone) setting
|
|
|
|
|
What I mean is that my DataGridView (in the example above) can look perfect (meaing the arrow is not visible because it has been filled with the correct backgound color) on my computer, but on another computer the arrow is visible, appearing with a slightly different shade of gray. Thus, for some reason the DataGridView is not the same color on all computers, but the arrow I drew has the same color because I hard-coded it. I am therefore confident that this is not a monitor issue.
|
|
|
|
|
arnold_w wrote: Thus, for some reason the DataGridView is not the same color on all computers, but the arrow I drew has the same color because I hard-coded it.
So other background is not hardcoded
if you use static prop ties of class Color (Color.White or Color.Blue). Those colors are hard coded. However class SystemColor has also static proprties, (SystemColor.Window or SystemColor.Control) those properties are system specific tied to color schemes used by windows. These color schemes can be easily changed
|
|
|
|
|
SystemColor seems to be exactly what I was looking for. A big thank you for your help!
|
|
|
|