Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing line on desktop very slow on Win 7

I.m using this code to draw a line with a mouse on top of all windows. I'm using mouse hook to get mouse coordinates and on each mouse movement while right mouse button is down I'm drawing the line. I'm using NOTXORPEN so I will be able to delete the line only by drawing it again.

Init:

ScreenDC := GetDcEx(GetDesktopWindow,0,DCX_LOCKWINDOWUPDATE);

PenHandle := CreatePen(PS_SOLID,HitStorage.GetPenWidth,10);

SelectObject(ScreenDC ,PenHandle);
OldPenMode := SetRop2(ScreenDC, R2_NOTXORPEN);  // Setting the PenMode to NotXor

MoveToEx(ScreenDC, X, Y, Nil); //Move The Pen Position to the Cursor Position

I'm using this code on each mouse movement:

LineTo(ScreenDC, X, Y); // Draw a Line between the prev Point to Current Point

This code worked for Windows 95, 98, 2000 and XP but now with Windows 7 its very slow.

I wrote a mouse gestures program (written in 1999) in Delphi 3 using ONLY Windows API (mouse hook was written in VC because of Delphi BUG in version 3). This program is doing exactly what StrokeIt is doing but somehow StrokeIt managed to figure out the solution to this problem.

My code is in Delphi but an answer in any language we will be appreciated!

I will explain in more details what exactly I'm trying to build. my application is divided to three parts :

  1. Mouse Gesture tracking and storing while Right-Mouse-Button is clicked
  2. Gesture Recognition based on limited number of shapes
  3. Macro activation based on the recognized gesture and the Application that was under the cursor while starting the gesture

I'm having trouble making Part1 to work on Windows7 (Vista), and as Martyn wrote in his answer I guess I have to change concept. Now I'm doing the tracking by hooking the mouse messages and the gesture drawing by the code i attached above... I understood the basics of Layered windows but what will work for me? do you think making a fullscreen topmost tranparent window and drawing on it will do the job? can some help me writing the code for this?

like image 692
Shlantz Avatar asked Nov 12 '11 23:11

Shlantz


1 Answers

Before Windows Vista, the desktop was drawn as an actual GDI surface, and so this kind of activity worked - though how fast it worked depended on your GDI driver's acceleration.

With Vista and beyond the Desktop Window Manager uses DX to composite together multiple windows (some of which may use GDI, and some of which may not) to a single final scene that is rendered to a newer driver model. As a result directly poking at screen pixels gets a lot slower.

More fundamentally, this is not the way to get things done. It's not really clear to me what you are trying to build, but you probably want to learn about Layered Windows: http://msdn.microsoft.com/en-us/library/ms997507.aspx which provide a supported and appropriate way to create more complex scenes on top of the actual apps. They should also help you avoid using a mouse hook, which is also a bad idea.

Martyn

like image 81
Martyn Lovell Avatar answered Sep 22 '22 16:09

Martyn Lovell