Windows 8 modern UI applications and DirectX... How to draw text? - c++

I already have a Direct3d device at my beck and call...
I am working on a Windows 8 modern UI application (Metro if you will)
What's the general technique of getting text drawn to the screen?
Extra points: Can I do 3d stuff with it too? This is what originally got me here as I started to do some direct2d thing then I thought, but how can I do 3d with direct2d... second of all the d2d create text functions require a handle to a window hwnd and there is no such thing (or it has been abstracted away) in windows 8 metro apps.
Anyone got any good examples or demos I can take a look at?

You should look into DirectWrite.
Regarding your second question you can render your text to a texture and then when you render that texture on screen do 3d stuff with it.

Rendering text with DirectWrite and Direct2D it's relatively simple, however, if you want something higher level, you can look into Drawing Library for Windows Store Apps, which wraps raw DirectX calls into some more GDI like.

Related

Overlaying text and graphics (stroked rectangles) on desktop screen

For an accessibility desktop application I must overlay the desktop screen with numbers, text and a grid of rectangles (stroked with e.g. red brush).
Ideally this should work on any window manager system (windows, linux KDE/GNOME, possibly even mac).
What is the standard approach to something like this? I was thinking of taking a screenshot of the screen and then drawing on top of it but I'm unsure on what to use to draw.
There is a library that could help you out in making cross-platform applications. glfw, this is capable of making an application window for windows, mac, Linux, and more.
For the graphics stuff, you could use OpenGL or Vulkan(personally not advised for new users) graphics APIs which are cross-platformed. I was thinking of taking a screenshot of the screen and then drawing on top of it but I'm unsure on what to use to draw. For this you could you framebuffers, learning OpenGL.

How to render child window with Direct2D in native desktop Windows application?

I have a desktop application where all windows (HWND) render itself with Direct2D 1.1. My question is how to do it more correctly?
Should each window has its own Direct2D device context derived from one Direct2D device? In this case, I cannot render transparent content on a child window without additional tricks (I have to change target on parent window’s context, render parent window to Direct2D bitmap and then draw this bitmap on child’s target).
May be it is better to have one Direct2D device context where all windows render itself? I believe DirectComposition works in a similar way. Unfortunately, I cannot use it because I target Windows 7.
You're asking a question whose answer will be very application specific. I recommend avoiding the whole problem of trying to get HWNDs to render with transparency amongst each other, especially if you're throwing Direct2D into the mix. There is just too much pain in that direction. Every version of Windows that you support will have different bugs that you'll be constantly bumping into and grasping at workarounds for.
Case in point: For the v4.0 release of Paint.NET, I converted all text rendering to DirectWrite, and almost all UI controls to use Direct2D. The image thumbnail control at the top of the window (the MDI selector) is using Direct2D for rendering but it also has to compose on top of what's behind it. And it has to play nice with glass on Win7 (it looks great though!). The code for this is awful, tricky, barely maintainable, and it seems to bump into a different rendering bug on every release of Windows: 7, 7 SP1, 8, 8.1, and 10 all behave slightly differently! It's really annoying to test, too; it's the only reason I have to set up and maintain VMs for every version of Windows that I support (other than the installer and updater). Windows 7 worked fine, then 7 SP1 added a bug which required some tuning to how I filled the alpha channel. Windows 8 has flickering when you resize the window unless I do a certain hack, but 8.1 works fine. 10 then has its own flickering bug if software rendering is used. Remote Desktop breaks things in its own way. Then you also have to worry about High Contrast, and whether DWM is enabled/disabled if you're supporting Windows 7. They all behave differently and it's really really painful.
Anyway. What you seem to really need is a UI system like WPF or XAML which doesn't use anything other than a top-level HWND container. At that point you're custom rendering everything and doing your own hit-testing and input routing (and accessibility and all sorts of other things), so it's not a small task.
Regarding the "how to do it more correctly" question and cardinality of device and device context: Have you thought about just using ID2D1Factory::CreateHWNDRenderTarget or ID2D1Factory::CreateDCRenderTarget ? They return ID2D1RenderTarget but you can call QueryInterface to cast them to ID2D1DeviceContext (this fact is missing from the docs but is also clearly intentional). This should simplify working with Direct2D and HWNDs quite a bit. This is what I do in Paint.NET: I still use an HWND for each control, but each control is using its own HWND or DC render target. If you're willing to poke around with Reflector or ILSpy, checkout Direct2DControl and Direct2DControlHandler in the Paint.NET DLLs.
Also, be careful about using more than 1 hardware accelerated HWND render target. You don't want to get into a weird area where every Direct2D-based UI control is waiting on VSync. Using D2D1_PRESENT_OPTIONS_IMMEDIATELY when creating the HWND render target should help. DWM already handles VSync, so you should be fine to tell Direct2D to ignore it unless you're doing some rather specific stuff with animations and timers.

Windows Imaging Component - Direct2D C++ - Drawing, Saving

Using Windows Image Component (WIC), I want to do the following for my windows desktop application (Direct2D/C++ with Windows 7 SP1 - Visual Studio 2013)
Choose any type of RenderTarget (Direct2D Hwnd/Bitmap/WICBitmap -
etc) for drawing
Create a empty bitmap (D2D1Bitmap or IWICBitmap -
whichever applicable)
Begin draw - Fill colour, draw some lines and ellipses -
End draw ==> (All in the Bitmap)
At some point of time, I need to
save the drawn content in the bitmap as an image in my computer
Place the bitmap in the x1,y1 (top left - xy coordinates) and x2,y2
(bottom right - xy coordinates) of the render target. Because the
rest of the space of the window would be used by toolbar.
How do I achieve this using C++/Direct2D?
GDI+ Code for my functionality:
Bitmap* pBmp = NULL; //create null or empty bitmap
Graphics* pGrBuf = NULL; //initialise graphics to null
pBmp = new Bitmap((INT)rectClient.Width, (INT)rectClient.Height);
pGrBuf = new Graphics(pBmp);
On this Graphics, I could always draw Lines, Rectangles, etc..
pGrBuf.DrawRectangle(....)
pGrBuf.DrawLine(...)
In the end, for achieving point number 5
//leave some space (30, 30 in xy co-ordinates) for putting the toolbox in the top
pGrBuf.DrawImage(m_pBmp, 30.0f, 30.0f);
The code for point 4 is intentionally omitted.
The question have a simple, unambiguous answer, but there are some details that you should (re)consider.
Direct2D is not a panacea framework that will easily outperform others. It's not very clear what are your drawings about and whats their purpose, but there are cases where Direct2D usage is not very appropriate. If you replace GDI(+) with D2D, some of your sufferings will be:
(officialy) limited OS support, according to the DirectX version and/or the functions you will use. You will have to forget about Windows XP, (very possibly) Windows Vista and (less possibly) Windows 7
the performance (compared to GDI+, GDI) is not always greater. Mainly depends from the way and the purpose you use D2D. There are cases where D2D has very poor performance (usually wrong usage or misunderstood concepts).
But also, the advantages that Direct2D could provide are countless.
Roughly said, Direct2D is nothing but a wrapper around Direct3D. It was introduced with the DirectX 10 and its usage was very similar to GDI(+). But with DirectX 11(1), the Direct2D "principles" were changed and now its more D3D-like. It adds another approaches and drops old ones. It could be a little bit confusing at first. Confusing, also because all the tutorials, articles and whatever D2D resources (including MSDN) in the web are mixed up between the D2D versions. Some of them are for the old version and recommend one thing (approach), other describe the new version.
Anyway, I recommend the new version - ie Direct2D 11.1.
To your question...
The "RenderTarget" is a concept from the "old" D2D. The new one is a DeviceContext
The DeviceContext has a target that could be a D2D1Bitmap(1) - offscreen one, a swap chain's back buffer.
The most typical drawing approach is to call drawing functions within a DeviceContext.BeginScene --- DeviceContext.EndScene block. The drawing functions are very similar to the GDI(+) ones.
There are several ways to do that. You can do it with the help of WIC. Also you can copy your D2D1Bitmap data to a DIBBitmap or you can even (re)draw it over a GDI context.
There is a function DeviceContext.DrawImage, but the way you will do it depends on many things. For example, you could have two bitmaps, that are drawn over two different HWnd (one for the toolbar, another one for the other drawing).
Here are some resources that could help you:
What is Direct2D for
Drawing a rectangle with Direct2D
Very well explained guide about migrating to Direct2D 1.1
Answer to another question here, related to Direct2D, but explains in short the way you should draw to a HWnd

Doing graphics and drawings in a C++ Win32 client/frame area

How does one write code to make drawings in the Windows main client area of a Win32 C++ app?
I found an example online that uses the "eclipse" API but when I put it in my program, the IDE complained so it would not compile.
Assuming you mean basic drawing (primitives, rendering bitmaps, stuff like that), then GDI and GDI+ are most likely what you want. I haven't used GDI+ much, but theForger's Win32 API tutorial will show you how to work with bitmaps. Drawing primitives is more GDI+ territory, and I can really only refer you to the MSDN GDI+ documentation for details of those functions.
If you're looking for game-type graphics, on the other hand, then you might need to research DirectX or OpenGL.
By using GDI and/or GDI+ functions in the WM_PAINT handler of the form.
Here is a site with some examples.

Visual Studio 2010 C++ console applications

I am new to C++ and I would like to know what the limitations are in graphics for a console application. For example---Could I create something as compicated as some of the
very colorful screensavers that have all kinds of splashes of color?? Could I draw lines
of changing color based on input strings??? I would appreciate any advice someone could
give me.
Thanks Doug
If you want to do some serious animation you'll pribably want hardware accelerated graphics (DirectX, OpenGL). If you just want simple images and animations a GUI app would do. As far as the console it's not really intended for more than text output but it can draw lines and change colors if you really want too.
However none of the three are limitations of C++ ... C++ as a language does not care about graphics that would be an OS limitation primarily and you'll find most of your drawing code however you go about it will be somewhat OS or hardware dependent unless you use a cross platform library with GUI or graphics support like QT, wxWidgets, OpenGL, etc.
As others have said, a console application is for text, not graphics! I don't know of any way (or reason) to do graphics in a console.
To do the kinds of things you are interested in (except maybe Windows screensavers) using Visual C++, I would recommend starting off with the SDL library. The Lazy Foo Productions website has an excellent series of game programming tutorials, and the first lesson gives you a step-by-step guide to build an app that displays stuff on the screen. It even has screenshots showing how to configure Visual Studio 2010, which is pretty important if you're new to this kind of thing.
SDL is free, cross-platform, and will let you (within your program's window):
draw pixels, lines, and rectangles in any color
draw text
draw images
make animations (by changing/redrawing the screen many times per second)
obtain keyboard input (including when keys are released)
It will also let you do 3D graphics with OpenGL, but that's another story.
You could, if you're very desperate- but certainly not platform-independently. From memory, the Windows API is quite good about letting you do a lot of crazy shit to it's console. However, it's probably better advised to get a genuine graphical API for this purpose, such as GDI, DirectX or OpenGL.
C++ does not have any standard facilities for drawing graphics in a console application. Any features (like changing the font color) will depend on your OS. I doubt you will find functions that do much more than changing the text color though. (For example, on Windows you would use system("color xx") to change the foreground and background color.)
Basically, if you want graphics you're going to have to abandon the console application and look for a graphics API.
Could I create something as compicated as some of the very colorful screensavers that have all kinds of splashes of color??
If by "splash" you mean "chunk of text", then yes. Otherwise no.
Could I draw lines of changing color based on input strings???
No, consoles are textual media.
If you want to try to do things to the console you need to use the Windows Console Functions. Standard C++ does not have any way to change console color.