I developed a tool to be transparent on a window in Windows 10, using WINAPI, SetWindowLong, SetLayeredWindowAttributes and LWA_COLORKEY (C++). It can work on any window, however, it cannot work on web browsers. I found a same case,
Winapi - How to achieve equivalent of LWA_COLORKEY for MS Edge browser
SetLayeredWindowAttributes to make a window transparent is only working part of the time
and it can work on browsers not using hardware acceleration option. But, the actions on browsers are slow because of not using the hardware acceleration.
Is there another way to be transparent on a window? ( Can it work using Direct3D programing? and how?)
Have you checked the Rita Han's reply: https://stackoverflow.com/a/61768293
I have tested the code. It can work on web browsers.
Here is the code:
#include <iostream>
#include <Windows.h>
int main(int argc, char* argv[])
{
HWND hwnd = (HWND)0x0005059C;// The handel of the web browser window
if (!SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED))
printf("SetWindowLong error: %d\n", GetLastError());
if (!SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 50, LWA_COLORKEY))
printf("SetLayeredWindowAttributes error: %d\n", GetLastError());
}
Related
I'm learning how to use the openGL but I only find tutorials using SDL functions for manage windows and events.
This the code I made with this tutorial:
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
int main(int argc, char **argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Mon premier programme OpenGL !",NULL);
SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
bool continuer = true;
SDL_Event event;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = false;
}
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3ub(255,0,0); glVertex2d(-0.75,-0.75);
glColor3ub(0,255,0); glVertex2d(0,0.75);
glColor3ub(0,0,255); glVertex2d(0.75,-0.75);
glEnd();
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
I'm ok tu use the SDL functions to manage windoxs but I would use the openGL events handler.
Does someone know how to replace and how to use it ?
Thanks
OpenGL does not have an event handler. It focuses on drawing, not input.
To handle window events and input, you need to either use the OS facilities, or use a library like GLFW, GLUT, or SDL that helps/abstracts them.
Does someone know how to replace and how to use it?
No. Because it doesn't exist.
OpenGL is focused on one thing: Drawing stuff to some framebuffer. In the case of a window framebuffer OpenGL has no capabilities in creating or managing it, and depends entirely on the operating system functions for this. SDL, GLUT, GLFW, Qt, … shrink-wrap the process of interacting with the OS in easy to use way, portable libraries with a cross-platform API.
I am trying to "print" items that contain text, onto a mono image.
I have items that either subclass QGraphicsTextItem, or some other class that uses painter->drawText().
I can do it in Qt4 - but in Qt5, all I get are some blobs. I have tried to experiment with a small sample program, but I can't figure out what is going on...
Here is a simple self contained program:
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsTextItem>
class TextItem : public QGraphicsItem
{
public:
TextItem(){}
virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem*, QWidget* = NULL)
{
QPen p(Qt::red);
painter->setPen(p);
painter->setBrush(Qt::NoBrush);
//painter->setFont(QFont("Arial", 30));
painter->drawText(boundingRect(), Qt::AlignCenter, "abcd");
}
virtual QRectF boundingRect() const { return QRectF(0, 0, 30, 20); }
};
void processScene(QGraphicsScene* s) {
QGraphicsTextItem* t = new QGraphicsTextItem();
t->setPlainText("abcd");
// t->setDefaultTextColor(Qt::red); // won't make a difference
t->setPos(0, 0);
s->addItem(t);
//t->setFont(QFont("Arial", 30));
//t->setTransform(t->transform().fromScale(3,3));
TextItem* t1 = new TextItem();
t1->setPos(0, 20);
s->addItem(t1);
QImage image = QImage(s->sceneRect().size().toSize(),
QImage::Format_Mono);
//QImage::Format_RGB32); // this works in qt5
image.fill(QColor(Qt::color0).rgb());
QPainter painter;
painter.begin(&image);
s->render(&painter);
painter.end();
image.save(QString("../test.bmp"));
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene s;
s.setSceneRect(0, 0, 50, 50);
QGraphicsView view(&s);
view.show();
processScene(&s);
return app.exec();
}
Running it in Qt4 it gives what is expected: outputs a mono image containing text (in both versions, using QGraphicsTextItem or paint->drawText() (left image).
The same in Qt5 only shows some blobs in place of text (right image):
I have tried all kinds of experiments... Setting the font doesn't make a difference (except, for QGraphicsTextItem, setting the font to something big AND scaling to something big - but that is not useful in a practical application).
The other thing that worked in Qt5 is rendering to a color image instead of mono - but that is very bad for my application, where I need to store small mono images. My thought would be then, for Qt5, to render text to a color bitmap and then convert it to mono... But really !
I have also tried to save the items themselves to a bitmap, in a function implemented inside their class - it did the same thing.
I have tried to set point size on the QFont before applying it.
Is there something else I can set on QPainter in order to draw text ?
I found a related question: QT5 Text rendering issue also asked again in Qt forums: QT5 Text rendering issue. But that question refers to all rendering, and to a Qt5 in Alpha on a MIPS based platform... It implies that other platforms don't see that issue...
I'm using 5.5.0... I am also running under windows... And rendering on color image works... And...
I simply do not understand the "solution":
For time being, when we tried forcefully to enable Glyph cache, then
we are not seeing this issue.
Not sure what that means and how to do it.
Why is text painting blobs on mono images (but fine on color images) in qt5 and what can I do to fix it ?
Using Qt 5.5.0 32 bit, in Windows 7
Edit:
1) I tested the code in 5.4.2 and it renders text correctly.
I tried to compare some of the relevant code in qpainter.cpp, qtextlayout.cpp, qpaintengine_raster.cpp, qtextengine.cpp
- but with my limited understanding, the changes between Qt 5.4.2 and Qt 5.5.0 look logical... and would not affect the behavior...
2) I did paint my text on a color QImage, then painted it on mono - it worked fine on my own text items, but a third party software that creates very small text gives me very bad resolution when I try the same (illegible).
Currently I'm using Direct2D for rendering 2D frames, but I realized that it's not supported in Windows XP (or anywhere with DirectX < 10), and now I'm looking for a way to support DirectX 9.
My scenario is not a simple one, and to cut to the chase my problem is that I do not have a HWND which I can use to initialize direct3d, however I do have an HDC.
This is what I'm doing with direct2d:
ID2D1DCRenderTarget *renderTarget = NULL;
HRESULT hr = FACTORY->CreateDCRenderTarget(&RENDER_TARGET_PROPERTIES, &renderTarget);
renderTarget->BindDC(this->hdc, &this->viewport);
renderTarget->BeginDraw();
...
I'm looking for an equivalent thing that will work with DirectX 9 and that allows to use HDC and not HWND.
I've searched a lot, and found other question regarding this issue, but I can't find a satisfying solution, the only thing I managed to find is offscreen rendering, but I couldn't find any good source for examples or explanations about that, what I did find included the use of an HWND to initiate d3d9.
Any ideas on how I can manage to render 2D frame (with transparency) on XP?
Thanks.
Edit
WindowFromDC returns null for the HDC I pass it.
Here's my code:
HWND hwnd = WindowFromDC(hdc);
LPDIRECT3D9 d3d = Direct3DCreate9(D3D_SDK_VERSION);
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 texture;
D3DXVECTOR3 imagepos(0, 0, 0);
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = False;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
if (SUCCEEDED(d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev))) {
....
}
The hwnd is null and so the call to d3d->CreateDevice fails (I think that's the reason).
As for the HDC, it exists and when I use it with direct2d it works fine.
Any ideas how to bypass this problem?
2nd Edit
Thanks to #RogerRowland I've discovered that the HDC that I get is a memory HDC (OBJ_MEMDC).
I guess that it's what the browser/firebreath is giving me, but it works well when I use it with Direct2D (like in the code from my first posting) and also when I try regular Win32 rendering (with BitBlt and all).
I can't seem to find a way to use Direct3D 9 with only the HDC I get, and so I'm currently working on a workaround which creates a new hidden window in which I'll do the rendering and then will copy the result to the HDC I have (BitBlting probably).
Even if this solution works I still don't like it very much so if anyone has another approach for dealing with this problem I'd love to know.
I have recently setup openCV 2.3.1 in Visual Studio 2010. I set it up using cmake and managed to run simple 'hello world' code as follows:
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <cxcore.h>
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("funny-pictures-cat-goes-pew.jpg");
cvNamedWindow("Image:",1);
cvShowImage("Image:",img);
cvWaitKey();
cvDestroyWindow("Image:");
cvReleaseImage(&img);
return 0;
}
This code was able to run for the first time, though it was displaying a grey image instead of the cat. As I was trying to see whts wrong, it started giving the following error; Unhandled Exception at a certain memory location...:![enter image description here][1](can't upload image because of low reputation points. But I hope you understood my problem description...
Regards,
Ruzzar
Can you please recheck that the *img is being properly filled out?
I just tested this, with the single change that my path to the image is absolute (IplImage *img = cvLoadImage("D:\\Development\\TestProjects\\OpenCVTest\\funny-pictures-cat-goes-pew.jpg");), aside from that its the same. It worked perfectly fine on my system here.
OpenCV 2.4.2 used, image from
http://i288.photobucket.com/albums/ll169/critterclaw101/funny/funny-pictures-cat-goes-pew.jpg
Edit: when testing with a wrongly set path, I get a grey image, thus I am sure that your code cannot find the image.
I am developing a Windows API application without using MFC.
I am using standard Windows libraries.
How do I draw a PNG image in a window?
Help me with some sample code.
I have tried some codes which are available on the Internet, but all are using MFC.
Take a look at this StackOverflow question. It offers several options which should meet your needs.
Adapted from MSDN:
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
void draw()
{
// start up GDI+ -- only need to do this once per process at startup
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
Rect rect(20,20,50,50);
Graphics grpx(dc);
Image* image = new Image(L"SomePhoto.png");
grpx.DrawImage(Img,rect);
delete image;
// shut down - only once per process
GdiplusShutdown(gdiplusToken);
return;
}
Your choices are: GDI+, WIC(Windows Imaging Component) or libpng
You can use GDI+. See Loading and Displaying Bitmaps.
The below code worked for me. It's free of MFC and can be used straight away to draw PNG images in a window.
Gdiplus::Image image(L"C:\\Logo.png") ;
Gdiplus::Graphics* graphics = Gdiplus::Graphics::FromHDC(GetDC(hWnd));
RectF ImgRect(0,0,y3/10,y3/10) ;
Gdiplus::Status result = graphics->DrawImage(&image, ImgRect);
Thanks for all your support and quick response to solve my problem.
If you know PNG'coding,you can decoding it. So you can draw PNG in any way~