MFC: draw in a control - mfc

Looking for some code/project examples on how to draw a pulse waveform with certain width/frequency in a MFC control. Which control would be the best to use for this purpose?. I am very new to this. Have already done some online search and definitely can use some expert ideas. I am lost.

The MFC CStatic class represents a picture control. You can derive a class from CStatic and paint whatever you like in its OnPaint member function. For examples search codeproject.com and codeguru.com for "CStatic", like this one:
http://www.codeguru.com/cpp/controls/controls/chartingandanaloguecontrols/article.php/c5219/Simple-Graph-Control.htm

Related

How to draw a close button like Visual Studio?

Visual Studio's windows have a close button (as well as other frame controls) that blend into the caption color and appear like so:
I was wondering if there's an accepted way of drawing controls like that. Do controls like these use DrawFrameControl or DrawThemeBackground, or do they use another approach altogether?
Thank you for any information.
There are two approaches for creating custom buttons using Win32 API:
1. Draw the control yourself. This is very complicated and requires a lot of knowledge about Win32 controls. Here are some links to get you started:
https://learn.microsoft.com/en-us/windows/win32/controls/user-controls-intro
https://www.codeproject.com/Articles/646482/Custom-Controls-in-Win-API-Control-Customization
Owner-drawn button, WM_CTLCOLORBTN and WM_DRAWITEM (clearing an HDC)
2. Modify an existing control using SubclassWindow. This is much easier, but in most cases very limited.
For example you can use a static control and handle mouse activity for it. You can also use multiple controls and show and move them as needed.
There is no set way of doing this since it's custom logic. But here are some links:
https://learn.microsoft.com/en-us/windows/win32/controls/window-controls
https://learn.microsoft.com/en-us/windows/win32/inputdev/mouse-input
https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-mousemove

C++ creating hdc on dilogex control on external dll

In my main program I have a dialg box that contains many controls.
One of them is a static control in witch I want to draw an image with a text on and some transparency effect.
Can any one help ?
You should consider deriving your own class from CStatic. You should define the static control with the SS_OWNERDRAW style, so you will be able to handle its rendering in CStatic::DrawItem. When DrawItem is called, you will receive a pointer to the DrawItemStruct, which has embedded in it, a handle to the control's DC that you can use to render into the control.
I'd also suggest looking at this implementation: http://www.codeproject.com/Articles/9064/Yet-Another-Transparent-Static-Control

OpenCV and creating GUIs

Can I use OpenCV to create GUIs that contain buttons, list boxes, ...etc for example?
Thanks.
OpenCV has highgui built-in for making GUIs.
You can find online tutorials to get you started.
Note that this is not an extensive GUI library. You can only do basic stuff like opening windows, drawing points, anti-aliased lines, rectangles and text. There is a slider widget that can be used as a on-off button. If you need more than that, you can either
build stuff yourself (for instance drawing a rectangle with text to make your own button), or
use another library like Qt which provide plenty of widgets (buttons, menus, lists, dialogs…)
Good luck if you go for the first one!
In the Learning OpenCV, the following title is mentioned in page 101: No Buttons.
And, this is some what is mentioned under this title:
Unfortunately, HighGUI does not provide any explicit support for buttons. It is thus
common practice, among the particularly lazy, to instead use sliders with only two
positions. Another option that occurs oft en in the OpenCV samples in …/opencv/
samples/c/ is to use keyboard shortcuts instead of buttons (see, e.g., the fl oodfi ll demo in
the OpenCV source-code bundle).*
Along with the highgui functions that Simon has pointed out, others have used OpenCV in conjunction with Qt. It is possible to translate the camera frames into images on a QLabel widget, and update the image on the label periodically.
On windows, you may use cvGetWindowHandle to obtain window handle (HWND). With that handle you may call CreateWindow from WinAPI and put WinAPI controls on that window.
But you will also need to override or hook the WindowProc that was set by OpenCV for that window. Hooking and overriding explained here Multiple WndProc functions in Win32

Custom draw button using uxtheme.dll

I have implemented my custom button inheriting from CButton and drawing it by using uxtheme.dll (DrawThemeBackground with BP_PUSHBUTTON).
Everything works fine but I have two statuses (Normal and Pressed) which Hot status is the same. It means when the user places the cursor over the button it is drawn alike regardless the button status (Pressed or not).
This is a bit confusing to the user and I would like to change the way the button is drawn in Pressed & Hot status. Does anybody know a way?
I have also thought about custumizing the whole drawing but the buttons use gradients, borders, shadows, etc. So it is not easy to achive the same look&feel drawing everything by myself. Is there a way to find the source code of the dll or know how to do it?
Thanks in advance.
Javier
Note: I think I could be able to achive what I want to do by using CMFCButton and overriding the OnDraw method. Let the control draw the button on OnDrawBorder and then drawing the inside button myself. But I need to know how the control draws the inside button when pressed. It is a gradient and I can't guess how it's done. Does anybody have a clue?
In answer to your second question, if you derive from CMFCButton instead of CButton you can override OnDraw() or OnDrawText() instead of the usual DrawItem(). That way the default button background will be drawn, and then your drawing code is executed.
The only way I know of to really tackle this is to use 'custom draw', rather than 'owner draw'. Custom draw came in with Windows 2000, but is only used by button controls with comctrl32 6.0 (so Windows XP onwards), isn't very clearly documented, and isn't something MFC goes out of its way to support.
Anyway, the good thing about custom draw is that it lets you hook in at various points in the drawing process, unlike owner draw, which makes you deal with the whole thing. Have a look in MSDN at the NM_CUSTOMDRAW notification message.
For the other part of your problem, detecting the 'hot' state, the easiest way to do this is to use WM_MOUSEMOVE messages and the TrackMouseEvent() function to track whether the mouse is over your button.
Unfortunately this is a bit of a vague answer: the amount of code you need to demonstrate a button that uses custom draw is a bit too much to type into these answer boxes! I do have a project that demonstrates such techniques, using a custom draw button (falling back to owner draw on older Windows versions) that adds a little arrow to the button. You can have a look at the source code by getting
Windows_UI_source.zip
Open it and have a look at the "DropArrowButton" class. The important bit is the OnCustomDraw() handler and its helper function DrawControl(): these get called at the various button drawing phases, and use UxTheme to draw the control appropriately.
I finally figured out how to achive what I want to do. It's pretty easy indeed.
I use two calls to DrawThemeBackground. The first one with PBS_PRESSED and the second one with state PBS_HOT. Then I make a ExcludeClipRect to avoid from drawing over the center of the button.
Something like this:
DrawThemeBackground( hTheme,
pCustomDraw->hdc,
BP_PUSHBUTTON,
PBS_PRESSED,
&pCustomDraw->rc,
NULL);
CDC *pDC = CDC::FromHandle(pCustomDraw->hdc);
CRect rectClient;
GetClientRect(rectClient);
CRect rectInternal = rectClient;
rectInternal.DeflateRect(4,4);
pDC->SelectClipRgn(NULL);
pDC->ExcludeClipRect(&rectInternal);
DrawThemeBackground( hTheme,
pCustomDraw->hdc,
BP_PUSHBUTTON,
PBS_HOT,
&pCustomDraw->rc,
NULL);
pDC->SelectClipRgn(NULL);
Of course this is not the whole code but I think is enough to make my point.
Thanks.

Simplest way to change listview and treeview colours

I'm trying to find a simple way to change the colour of the text and background in listview and treeview controls in WTL or plain Win32 code.
I really don't want to have to implement full owner drawing for these controls, simply change the colours used.
I want to make sure that the images are still drawn with proper transparency.
Any suggestions?
Have a look at the following macros:
ListView_SetBkColor
ListView_SetTextColor
TreeView_SetBkColor
TreeView_SetTextColor
There are also appropriate methods of the CListViewCtrl and CTreeViewCtrl wrapper classes:
GetBkColor
SetBkColor
You may also want to take a look at WTL's CCustomDraw::OnItemPrePaint (that's if you need to control the drawing of individual items)
A good article that describes this process is here
It's been a while since I've use the win32 API directly, but I believe that if you handle the WM_ERASEBACKGROUND message for your control, you can use FillRect() in your handler to paint the background using whatever color you like.