mouse click to get coordinates of picture control-MFC - mfc

I created a picture control (type: frame) to display image. Now I want to use mouse to click specified coordinates of picture control to show position and R, G,B values. How could I solve this problem?

Catch the WM_LBUTTONDOWN message. Get x/y coordinates from lParam (see MSDN for details). Get bits from image using GetDIBits(). Read RGBA from bitmap buffer you got from GetDIBits(), at the location x/y you got from lParam. This assumes your picture control doesn't do scaling etc., you'd have to correct x/y for that. Alternatively, you could use the ::PrintWindow() API to get a copy of the window into a DC; you could then use GetPixel() on the DC to get a COLORREF. Come to think of it, this is probably a better solution, if you're only after one RGB value.

Related

Direct2D WM_MOUSEMOVE message with scaled display

I am new to Direct2D programming and I have encountered an issue with the WM_MOUSEMOVE message handling.
As documented in MSDN, I should use this enum to handle the mouse move, and should use the LOWORD & HIWORD to extract the current x and y coordinates.
That works fine when I am working on a normal display, but when trying to run it on scaled displays (e.g. 125% in my case), the values of x and y aren't accurate, in other words, there is an "indentation" between the current position of the mouse and the values extracted from lparam.
I guess I should query the OS or the window to get the current scaling so I can calculate the right position, but don't know how!
any help please?
You can take control of the scaling by declaring your program as DPI aware. Then the automatic scaling will stop and you'll get the original coordinates. You'll need to scale the window yourself though.
Creating a DPI-Aware Application

Changing the metrics in console from characters to pixels

I'm wondering, is there a way to click the mouse on a console window and get the x y position by pixels instead of a character like the console mode is set on?
i.e I click on the (350,250) pixel on the console but the returned position is (56,35) for example, I searched all over google but didn't find anything usefull.
This will get you a bit further along:
Call GetConsoleWindow() to the the handle to the console that is running your process.
Call GetConsoleFontSize() to get width and height of the console font in pixels.
Call GetConsoleScreenBufferInfo() to get the screen buffer size.
Call PeekConsoleInput() or ReadConsoleOutput() to read the contents of the console you should probably read up on Low-Level Console Input Functions to understand what's going on here.
This MSDN article explains how to get console click events,
Now that you know how to get the mouse click coordinates and you have access to the buffer contents of the console it's simple to calculate what character is being clicked by dividing the X & Y cooridinates with the font width and height.

How to get every pixel coordinates from the WM_MOUSEMOVE when the mouse move?

I am developing a brush just like the brush in mspaint, but I cannot get all the pixels from
WM_MOUSEMOVE when the mouse move over the pixels.only can get a set of desultory points.
You can use GetMouseMovePointsEx to get a history of the last 64 mouse points, which may include points that were never delivered to your app via WM_MOUSEMOVE.
But you can often improve the performance in this sort of application by ignoring the actual mouse position that WM_MOUSEMOVE sends you and instead query the pointer position directly with GetCursorPos.
Either way, you'll never get enough resolution from the mouse to draw a smooth joined-up line - the best you can do is draw straight lines (or even interpolate splines) between the coordinates.

Draw mouse pointer icon?

I am coding little fun gadget. I want to be able to draw second (or more) mouse pointer icons at different location than the original mouse but to move it according to move of original mouse.
I know how to track movement of the mouse but I dunno how to draw/redraw mouse pointer; can anyone help?
You can use the following code:
CURSORINFO ci;
ci.cbSize = sizeof(CURSORINFO);
GetCursorInfo(&ci);
Next you can draw a cursor by calling:
DrawIcon(ContextDC, YourXPosition, YourYPosition, ci.hCursor);
If you need additional information about the cursor, like hotspot for example, check the ICONINFO structure:
ICONINFO ii;
GetIconInfo(ci.hCursor, &ii);
This could be done like:
grab the current mouse cursor from your application, using LoadCursor(). Just specify NULL, and the cursor you want. Or just load a bitmap for the cursor. Now, you have a bitmap.
Next step is to get the Device context of your Desktop: GetWindowDC(NULL). This will give you the opportunity to draw on the desktop anywhere.
There is a huge chance that you will need to apply CreateCompatibleBitmap() to the Image at #1 with the DC obtained at #2.
Now, use some BitBlt() to copy bits OUT from the DC obtained at #2 into a save image (YOU will need to create these) from the position you want to put your cursor.
Now, put the image obtained at #3 onto the DC of the Desktop obtained at #2 at the position you want.
When the user moved the mouse restore the image on the desktop with the saved data at #4. Release all the stuff you don't need (yes, this is mandatory).
And restart from #1.
These two more links might help:
Bitmaps, Device Contexts and BitBlt
Capturing an Image
Good luck!

MFC dialog and form

I have a dialog (there are 3 edit controls in order to display 3 color channels RGB), I use openCV to open an image in a new form. What I want is when I move the mouse to any point on the image, each of the RGB color values will be shown in 3 edit boxed (on the dialog). How can I do that?
Thanks!!!
Perhaps this will help:
OpenCV rgb value for cv::Point in cv::Mat
To get the current cursor position you can use GetCursorPos and eventually ScreenToClient functions.