Drawing to the desktop via injection - c++

I'd like to draw to the desktop wallpaper area with directX 9 in particular. Under the icons and text above the wallpaper.
Similar to Okozo or one of VLC's modes, or Dreamscene.
So there's a lot of similar questions to this but no working examples or tutorials.This A simmilar question but different approach seems pretty useful. A few years ago I was searching and found this site and code.
It's perfect aside from one big issue the icon text gets blocky even with alpha blending. I tried a few fixes but I wasn't able to find any helpful documentation maybe I just wasn't using the right words for searching, maybe it's proprietary. I think I fixed that with a call to invalidate the desktop area but I can't really tell as in the process I messed up the alpha values for the text so it doesn't properly draw the font color. So how do I alter the text for proper alpha blending?
Pouet's links are all dead
I know I should post a snippet but I can't pin down where the problem is as it's more an unexpected feature.

I've been searching for this code for too long.
I'd like to write a Dreamscene like app witch can render dynamic contents other than just video files, and this one may be the most hopful solution.
I compiled your code in VS2010 Win7x64 and found:
Desktop icon text become transparent just like Dreamscene do when the window is like:
0x00150138 "Program Manager" Progman
0x000605D6 "" SHELLDLL_DefView
0x000C05BA "FolderView" SysListView32
Desktop icon text become normal but not clear enough when the window is like:
0x000B007C "" WorkerW
0x000605D6 "" SHELLDLL_DefView
0x000C05BA "FolderView" SysListView32
0x000C05BA "FolderView" SysListView32
0x000511E2 "" WorkerW
0x00150138 "Program Manager" Progman
I tried this method Draw Behind Desktop Icons in Windows 8 to make the window status always be like the second condition; and tried to draw on WorkerW window above Progman window with D3D, but got nothing rendered out.
So far there's nothing helpful, dumping the bitmap used by memoryDC may help, which I'll try latter.
If you got any further approach, please let me know.
It has been long time since I last use English, Sorry for my mistake if any.
Update:Clear Icon Text got!
add this under d3ddev->BeginScene();
d3ddev->SetSamplerState(0,D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
d3ddev->SetSamplerState(0,D3DSAMP_MAGFILTER, D3DTEXF_GAUSSIANQUAD);
and this:
HWND desktop = GetDesktopWindow();
HWND explorer = FindWindowEx(desktop, 0, _T("Progman"), _T("Program Manager"));
SendMessageTimeout(explorer, 0x052C, 0, 0, SMTO_NORMAL, 3000, NULL);
HWND defView = 0;
HWND worker = 0;
while(!defView) {
worker = FindWindowEx(desktop, worker, _T("WorkerW"), 0);
if(worker) {
defView = FindWindowEx(worker, 0, _T("SHELLDLL_DefView"), 0);
}
else break;
}
if (!worker){ /*DBG("First WorkerW failed");*/ return 0;}
worker = FindWindowEx(desktop, (HWND)worker, _T("WorkerW"), 0); //find the next WorkerW
if (!worker){ /*DBG("Created WorkerW not found");*/ return 0;}
Still drawing on the listView, but worker window must exist. This code runs with Aero Theme enabled only.

Related

WINAPI MoveWindow trick

Once I've been given a task to create application that doesn't have a regular window shown to user. All the navigation was held from taskbar, something like a Spotify app -> in order to listen to a playlist you do not need a window.
Everything was displayed in this small area. I tried many ways to achieve that but finally I simply invoked MoveWindow(hWnd, -10000, -100000, width, height, ...) and it worked! But something popped in my head -> is this solution even safe? It seems odd to me to draw things behind the screen. I initially thought that Windows doesn't even bother and simply ignores drawing calls for this window. But somehow I can see update in application on this small preview window above taskbar. I suspect that it might be actually redrawn behind the screen. Then is the following true -> Windows redraws window but doesn't send a request to the graphics driver to show application on screen? It seems quite logical in spite of efficiency.

MFC Printing with CDC just works on some Printers

I'm implementing a Printing Function in a big Project to print so called gadgets (derived from CWnd). In the Gadget Class I've created a Function to Render it to the Device Context
PrintPageContent(CDC * pDC, const CRect & rContent, int page, int numPages)
{
PrintWindow(pDC, PW_CLIENTONLY);
pDC->Rectangle(rContent.left,rContent.top, rContent.right, rContent.bottom);
}
To Render the Gadget easily I thought about using the PrintWindow Function https://msdn.microsoft.com/en-us/library/x51z0shh.aspx
What is always working?
Everything in the PrintPreview
The Border Rectangle when I'm Actually Printing
What isnt Working?
the Gadget isnt printed in some cases of the "actual Printing Process" / "Printing to Paper".
So I tried to print via PDF Creator and via 3 Local Printers in my LAN on 2 Different Windows Machines (Win7, Win8) with different Results (they seem to be always the same so i don't think its some kind of uninitialized member). Whats very weird is that I have different Results on the Machines for example there is one Printer which works for PC A but not for PC B.
I can tell you that printing just wont work within my Application so it isn't a Driver Problem. Printing normal Documents, Images fully works. And as I already told the Border is always printed.
What could be the cause of this? Do you know any Cases of such kind of Problem?
Hint:
As an workaround I tried to Copy the Gadget from the CPaintDC of the UI directly via BitBlt. In this case I have the Same Problem
To find the Issue I created a Small Test Project to recreate the Situation.
Here Is the Source Code
//Create Members
CDC pDC;
HDC hdc;
//Get Printer/Printer Settings
LPCSTR buffer = NULL;
GetDefaultPrinterName(buffer);
hdc = CreateDC("WINSPOOL", buffer, NULL, NULL);
pDC.Attach(hdc);
pDC.m_bPrinting = TRUE;
//Start Document Printing
pDC.StartDoc("TEST");
pDC.StartPage();
//Render Window
PrintWindow(&pDC,PW_CLIENTONLY);
//Render Frame Rectangle
CRect WindowRect;
GetClientRect(WindowRect);
WindowRect.MoveToXY(0,0);
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
pDC.FrameRect(WindowRect, &brush);
// Finish Printing
pDC.EndPage();
pDC.EndDoc();
I'm facing the same Problems here. The Same Printers are working for the same PC's.
I think the only Problem could be the Line where I create the HDC
hdcBuffer = CreateDC("WINSPOOL", buffer, NULL, NULL);
I think this call in Connection with the "printWindow" or "bitblt" is the problem.
Or could this be a MFC bug?
It looks like a MFC Bug. Somehow the Printer Driver isn't initialized Correctly. I tried several Solution's but wasn't able to got this working. It really fails in the most Simple Examples with different Results on Differnt Machines.

Improve code for WM_PAINT and WM_CTLCOLORSTATIC handlers

INTRODUCTION AND RELEVANT INFORMATION:
I have implemented complex painting of the main window’s background and its child static controls.
The picture below shows how it looks.
Static controls have SS_NOTIFY style, which is important to mention, as certain things happen when user clicks on them.
At this point, actions activated when clicking on them, are not relevant.
Both main window, and static controls, have gradient backgrounds, which were made through usage of GradientFill(...) API.
Top banner of the main window is created with gray brush, and the grid lines were created with LineTo(...) and MoveTo(...) API.
Map on the orange static control, and the top left logo are EMF files, top right logo is PNG file, and other pictures are bitmaps.
Orange static control has 4 child static controls which are owner drawn and also have SS_NOTIFY style.
It was the only way I could have thought of, which enabled me to draw the control the way it was asked of me ( if I can improve on this, please suggest it, I will accept any reasonable suggestion ).
In order to paint the orange static control, I have decided to paint its background in WM_CTLCOLORSTATIC handler, and to owner draw child static controls in a subclass procedure.
Notifications received from child static controls are also handled in the orange static controls subclass procedure, since I didn’t know how to forward them to the parent window, but are omitted since they are also irrelevant at this moment.
I have decided to provide link to the demo project, instead of making this post quite lengthy with code snippets.
I have tried to submit demo application as small and simple as it is possible.
I did not skimp on the commentaries, so I believe everything is well covered and explained in source code.
If there are still questions please leave a comment and I will reply as soon as possible ( usually immediately, or in the same day, at least ).
Here is the link to the demo project:http://www.filedropper.com/geotermistgrafika_1
Important update:
/==========================================================/
Text bellow in square brackets was the original part of the question, but is now omitted since the project had memory leaks.The above link links to an improved version.
[ Updated in response to member xMRi's comment: This link should be fine: http://www.filedropper.com/geotermistgrafika ]
/==========================================================/
I work on Windows XP, using MS Visual Studio C++ and pure Win32 API.
One note: since Express edition of VS has no resource editor, resource file and resource header were created using ResEdit from here: http://www.resedit.net/.
PROBLEM:
When I resize my window, static controls slightly flicker.
MY EFFORTS TO SOLVE PROBLEM:
I believe that my code has no memory leaks-therefore I doubt this is the problem, but being inexperienced, I would highly appreciate if my assumption can be somehow confirmed.
I think that I have properly handled WM_ERASEBKGND, and I have excluded styles CS_VREDRAW and CS_HREDRAW from my window class-therefore flickering should not be caused because of this.
I have forgot to mention, that my window has WS_CLIPCHILDREN style, so I am mentioning that now, in response to the comment bellow made by member Roger Rowland.
I have implemented double buffering for both handlers, in order to avoid flickering.
QUESTIONS:
How can I modify code in demo project to get rid of flickering?
I need advice on how to optimize both WM_PAINT and WM_CTLCOLORSTATIC handlers, so my painting code gets more efficient and faster.
A small note for second question:
I was thinking to improve my code by drawing the entire picture on the main window’s background, and to put transparent static controls on top of the part of the picture that corresponds that static controls background.
That way, I would only return NULL_BRUSH in my WM_CTLCOLORSTATIC handler, and do all the work in the WM_PAINT.
Am I on the right track with this idea? Could this work ?
Thank you.
Regards.
Firstly, your App is leaky as hell. Haven't looked for leaks, but most of them should be in WM_CTLCOLORSTATIC as you forget to delete HBITMAP's(use this neat freeware http://www.nirsoft.net/utils/gdi_handles.html to look for gdi leaks).
Secondly, your code is way to big. I noticed that you didn't use functions, maybe because you don't know what they are capable of. For example I would use:
void DrawBackground(HDC &hDC, SOMEINFOSTRUCT GradientInfo, LPCTSTR Text);
to simplify your code a lot.
Anyway enough of lecturing, let's go back to your problem. In WM_CTLCOLORSTATIC you must return brush, you want to paint background with. What you're doing now is painting background manually using Bitblt(), then return NULL brush and program paints it on your already painted background. Instead of painting it yourself, let the brush do the job.
Simply instead of the last Bitblt() use CreatePatternBrush(), but then you need to take care of this Brush and here is what you should do:
HBRUSH TempBrush = NULL; //Create global brush
//Some Code....
case WM_CTLCOLORSTATIC:
{
if (TempBrush != NULL)
{
DeleteObject(TempBrush);
TempBrush = NULL;
}
//Let's skip to the end....
GradientFill( MemDC, vertex3, 3, &gTriangle, 1,
GRADIENT_FILL_TRIANGLE );
TempBrush = CreatePatternBrush(bmp);// these 3 line should be at the
//end of every if
DeleteDC(MemDC); // or put them once outside if's
DeleteObject(bmp); // also if you delete HDC first, you don't need to
//unselect hbitmap
}
return (LRESULT)TempBrush;
}
break;
case WM_CLOSE:
{
if (TempBrush != NULL)
{
DeleteObject(TempBrush);
TempBrush = NULL;
}
//.......

How to make a window appear on top of everything (even full screen games!) c++/Qt

I'm trying to make an application displaying a crosshair at the center of the screen and staying on top of everything else.
The aim is to have a crosshair in some FPS games that doesn't provide one.
I've succesfully made my window topmost for everything except the games :/
Here is my code : (everything is in the main since im only testing the core functionalitys of my app, I've commented it extensively to try and make my problem more accessible)
QApplication app(argc, argv);
DWORD error;
QWidget window;
QLabel *label = new QLabel(&window);
label->setText("<strong>O<strong>");//I'm using an "O" as a crosshair until I can figure out how to display image transparency.
window.setGeometry(960-label->width()/2,540-label->height()/2,label->width(),label->height());//here I'm making my window appear in the center of my screen
window.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);// here making the window frameless and topMost via qt
window.setAttribute(Qt::WA_TranslucentBackground);//making the window see through
window.setWindowTitle("Crosshair");
window.show();
//in this next part I tried using windows api to make my window appear on top of the game.
HWND handle, myHandle;
myHandle = FindWindow(NULL,TEXT("Crosshair"));//retieving my own application window handle
if(myHandle == 0){cout << "no own handle" << endl;}//it successfully retrieves it
handle = FindWindow(NULL,TEXT("Killing Floor"));//retrieving a game window handle
if(handle == 0){cout << "no KF handle" << endl;}//it successfully retrieves it
if(handle != 0 && myHandle != 0)
{
if(SetWindowPos(handle,myHandle,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE) == 0){cout << "couldnt set notopmost" << endl;}//here SetWindowPos returns 0 (function failed)
}
// I've also tried using SetWindowPos to set the game to Not TOPMOST, it didnt work either.
// here was my code : SetWindowPos(handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
error = GetLastError();// i tried using GetLastError to understand what was happening
cout << error << endl;// but it only returns "5", I've read that you can look in WINNT.H for information about the meanings of error codes
// however its a pretty big file and I wasn't able to understand where the relevant part was.
return app.exec();
My guess is that application such as games have a more direct control over the display device.
I'm looking for any solution to this problem (not neccesseraly one involving a transparent topmost window).
Also on a sidenote if someone could explain to me how to effectively use GetLastError(), and also why are game behaving differently than a common window.
Thanks in advance.
I know this doesn't really help you, but please have a look at this post from Raymond Chen: How do I create a topmost window that is never covered by other topmost windows?
From the SetWindowPos() documentation:
A window can be made a topmost window either by setting the
hWndInsertAfter parameter to HWND_TOPMOST and ensuring that the
SWP_NOZORDER flag is not set, or by setting a window's position in the
Z order so that it is above any existing topmost windows. When a
non-topmost window is made topmost, its owned windows are also made
topmost. Its owners, however, are not changed.
Also from the same page:
To use SetWindowPos to bring a window to the top, the process that
owns the window must have SetForegroundWindow permission.
However, I guess the link is for Windows Vista and above.
I found that the c++ library DirectDrawOverlayLib should be able to do this. To quote the website:
An unmanaged C++ library to create, manage and draw to DirectDraw overlays. A
C++/CLI wrapper for .NET clients is included.
DirectDraw overlays are special DirectDraw surfaces that are shown
over everything else, including full-screen games and applications.
They can be used to implement programs like XFire that display information during fullscreen game operation.

Drawing icons instead of bitmaps in a MFC CMenu?

I can use an bitmap in a menu
CMenu men;
CBitmap b;
b.LoadBitmap(IDB_0);
men.AppendMenu( MF_ENABLED,1,&b);
I can draw an icon into a DC
CImageList IL;
IL.Create(70, 14, ILC_COLOR16 | ILC_MASK, 1, 0);
IL.Add(AfxGetApp()->LoadIcon(IDI_0));
IL.Draw ( pDC, 0, rcIcon.TopLeft(), ILD_BLEND50 );
But I cannot find a simple way to show an icon in a menu. I would like to do this
CMenu men;
CBitmap b;
// here the miracle happens, load the icon into the bitmap
men.AppendMenu( MF_ENABLED,1,&b);
Seems like this should be possible.
This is the same question as this. However that question referred to the MFC feature pack, did not get answered, and has shown no activity for a month, so I thought it would be worthwhile to ask it again in reference to basic MFC.
I asked the question you reference.
The way to add (normal, 16-bit color) icons to menus is to make a toolbar with the same resource id as the menu you want to have icons in. You then assign id's to each of the toolbar buttons, the same id's as the menu entries. Make a wizard-generated new MFC application and you'll see how it works there.
The answers to the question I posted suggested that it should work the same for 32-bit images with transparency for the Feature Pack toolbars; I haven't gotten around to test it out though.
If your specific problem is how to make dynamically-generated menus, I think you should pass the id of an existing entry in a toolbar and then that image will be used.
Not a real answer to your question but maybe it'll point you in the right direction.
Good code. Please note that this shows the bitmap image but it is always good to remove the blank space left to the string (used for check/unchek) if you are showing the image.
I did like this.
MENUINFO mi;
mi.cbSize = sizeof(mi);
mi.fMask = MIM_STYLE;
mi.dwStyle = MNS_NOCHECK;
pcSubMenu->SetMenuInfo(&mi);
MENUITEMINFO mii;
mii.cbSize = sizeof mii;
mii.fMask = MIIM_BITMAP;
mii.hbmpItem = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_16_HELP),IMAGE_BITMAP,0,0,LR_SHARED |LR_VGACOLOR |LR_LOADTRANSPARENT);
pcSubMenu->SetMenuItemInfo(ID_CONTENTS,&mii,FALSE);
I think what you are looking for is very similar to what's described here...
www.codeguru.com/cpp/controls/menu/bitmappedmenus/article.php/c165/
mixed with what's described in www.codeproject.com/KB/shell/DynIcon.aspx
Still have to see if it will work.
Tamer
On Windows Vista/Windows7 you can not do that, it's either a 32 BGRA image or the menu is not drawn in the new UI style. There is no workaround, maybe ownerdrawing but i read the style API is not working correctly with menus, so i never tried to get deeper in it.
You should use 32bit BGRA Icons anyway.
In order to set up a bitmap for a menu, you need to call CMenu::SetMenuItemInfo() for each item with something like this:
MENUITEMINFO mii;
mii.cbSize = sizeof mii;
mii.fMask = MIIM_BITMAP;
mii.hbmpItem = bitmapHandle;
menu.SetMenuItemInfo(menuItem,&mii,TRUE);
A further complication with doing this is that this is okay for 256 colour bitmaps, but not full colour 32bit RGBA bitmaps - these will work, but only on Vista, and then only if you present the bitmap as pre-computed RGBA.
In practice, in my code I get round this by using another feature of menu icons, which is to set the hbmpItem to HBMMENU_CALLBACK, which allows a callback to draw the bitmap: I do that for Windows XP and before. The code gets a bit too complicated to post here. As an example, you could have a look at my code at
http://www.ifarchive.org/if-archive/infocom/interpreters/frotz/WindowsFrotzSrc.zip
Look in "MenuBar.h" and "MenuBar.cpp", particularly the code around MenuBar::SetBitmaps().