GTKMM: Take a screen shot of an DrawingArea? - c++

I Have a Drawing Area that I draw some figures or images inside, so I have this callback to take screenshots:
void CanvasToolBox::actionCanvasCamera()
{
auto root = Gdk::Window::get_default_root_window();
int height = root->get_height();
int width = root->get_width();
auto pixels = Gdk::Pixbuf::create(root, 0, 0, width, height);
pixels->save("s.png", "png");
}
That code takes a screenshot of the whole screen..
But I need to take the screenshot of a especific window a Gtk::DrawingArea in my case.. so the function create needs a Gdk::Window as a parameter.
Is there any way to take a screenshot of a Gtk especific window?
Thanks

I'm not used to GTKmm (I use C and Python APIs instead), but with your example, that looks pretty straightforward. You just want to use your Gtk::DrawingArea instead of the root window. So you search in the documentation of Gdk::Window::get_default_root_window and find that it returns a Gdk::Window.
Now you have to find how to get a Gdk::Window out of a Gtk::DrawingArea. So you go to the documentation of Gtk::DrawingArea and search for a function that returns a Gdk::Window, and then find this is Gtk::Widget::get_window, which is inherited from Gtk::Widget. BTW, you may prefer the const version, as you don't modify the window, just read it.
You can then replace code that gets the root windows with a call to get the Gdk::Window of the widget you care about, and you're done. If you want to go further, instead of using Gdk::Pixbuf, there may be a way to use a cairo context.

Related

How to resize an image using SDL2

I have been looking around on many links to find out how to resize an image on SDL2, like in pygame, the "pygame.transform.scale()" function, and I have tried to look to see how to implement one but all of the links are unique to the question or are very unclear to me. How can I implement a function that returns a surface/texture, whatever is used for SDL_RenderCopy() of a newly-resized image, so when I want to use the rectangle it has, it's rectangle is now resized to what i tell it to do. As in:
SDL_Surface/SDL_Texture ResizeImage(int width, int height){
//Process code here to effectively do the equivalent in python of:
newSurface/Texture = pygame.transform.scale(img, width, height);
return newSurface/Texture;
}

Show certain image from image sprite

I have wrote a class which will be responsible for the image buttons handling:
#include "ImageButton.h"
ImageButton::ImageButton()
{
// main constructor
}
ImageButton::ImageButton(wxWindow* parent, const wxString& buttonPath)
: wxStaticBitmap(parent, wxID_ANY, wxBitmap(buttonPath, wxBITMAP_TYPE_PNG), wxPoint(0, 0), wxDefaultSize)
{
Refresh();
}
ImageButton::~ImageButton()
{
// ...
}
This is very beginning and basic. However, I have just found out that there is probably no possibility to resize the image (not changing it's dimensions).
This is how the image looks like:
What I'd like to achieve right here is tell wxStaticBitmap to display only one close square button at the time (so then I could make a mouse over/click event handlers for it). Setting it's size won't work here and thats not what I want.
Is it possible to crop the image at certain dimensions in this situation?
You can create two images from it. One of the ways to do it is to use wxImage::Resize (first make a copy of the original image). Second way is to use wxBitmap::GetSubBitmap (you need to convert the wxImage to wxBitmap - this can be done by simple assignment). It really depends what you want to achieve and what is more convenient to you.

MFC Image Button with transparency

I'm updating an MFC dialog with a number of buttons on it.
At present, the dialog has a Picture control covering the whole dialog providing a patterned background. On top of that, each button is a CBitmapButton using (opaque) images carefully generated to match the area of background they cover.
It would obviously be much easier if the images could be created as mostly transparent, so the background shows through automatically. However, I can't work out how to get MFC to render transparent images correctly in this case.
I understand that I might want a different class to CBitmapButton, or need to write a custom subclass; that's fine, but I don't know where to start. It would be nice to support 32-bit BMP or PNG with alpha channel, but I'd settle for the "specified colour should be transparent" type.
It may not be the best way to do it, but what I'd do is create a custom CButton derived class (assuming that you're actually using the rest of the CButton functionality), then override the DrawItem function to put your custom draw code in.
For the image itself I'd use a Bitmap GDI+ object (which will allow you to load either BMPs or PNGs with alpha channels) then use the regular DrawImage function to draw the bitmap.
If you're going to put PNGs into your resource file then you need to put them in as a "PNG" type. Make sure when you look in the resource file code that the entry looks like
IDB_PNG1 PNG "C:\temp\test.png"
and doesn't try to treat it as a BITMAP resource otherwise you'll have problems loading them.
Edit
Putting my response here so I can post code. Yes, I meant to derive a custom class from CButton, then add a Gdiplus::Bitmap member variable. Here is roughly what you'll need to do to get it to work, though I haven't checked that the code actually compiles and works, but hopefully you'll get the idea. It's not the most efficient way to do it, but if you've not done much custom drawing before then it does have the advantage of being simple!
void CMyButton::LoadImage(const int resourceID)
{
m_pBitmap = Gdiplus::Bitmap::FromResource(NULL, MAKEINTRESOURCE(resourceID));
ASSERT(m_pBitmap);
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
CRect rcClient;
GetClientRect(&rcClient);
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
// If you want to do anything special when the button is pressed, do it here
// Maybe offset the rect to give the impression of the button being pressed?
rcClient.OffsetRect(1,1);
}
Graphics gr(lpDrawItemStruct->hDC);
gr.DrawImage(m_pBitmap, rcClient.left, rcClient.top);
}

Function to retrieve the size of an element

What I need is a function to retrieve the size of an element, be it a Java Applet window in a browser, text boxes in programs, applications window and so on.
Now I don't know what this kind of function is called, but I uploaded an example from an application that had that functionality.
Example (The red box)
What I need to be able to with this is get its size and its coordinates on the screen. This needs to be in C++.
So ff anyone could give an example, or atleast the name of that kind of function, I would be grateful.
I found a program that has the function I seek: It is called Scar Divi, which is a scriptable tool to perform repetitive actions, mostly uses for cheating in a game called Runescape it seems. Unfortunately it is closed source.
Are you looking for Window Geometry or desktop widget?
After getting an image of the screen (there will be somethign in Qt to do this)
You need an image processing library like openCV to find the rectangle - look for "Hough Transform"
I found the solution!
POINT p;
HWND wnd;
RECT rec;
GetCursorPos(&p);
wnd = WindowFromPoint(p);
GetWindowRect(wnd, &rec);
This will give you the coordinates for the box (extract from rec).

When using SDL_SetVideoMode, is there a way to get the internal SDL_Window pointer or ID?

If you create a window by using SDL_SetVideoMode(), you are returned a surface, not a window handle. Is there a way to get the SDL_Window handle? I know there is a SDL_GetWindowFromID function, but I'm also not sure how to get the ID, other than the SDL_GetWindowID function, which would require me to already have the window handle.
Any suggestions? Note that it is very important that I maintain cross platform portability, so I prefer to stick with built in SDL functionality if at all possible.
If it helps any, I'm trying to get and set the window position and window size, and those functions require a window handle.
Thanks!
EDIT: I should mention also that I am changing video modes at the user's request, so I cannot just use the default ID of 1, since this ID changes every time I call SDL_SetVideoMode().
I had the same problem with SDL-1.2.15 for windows ,but the problem solved by GetActiveWindow.
You can get SDL window handle like this :
...
screen = SDL_SetVideoMode(w, h, 0, flags);
...
HWND hnd= GetActiveWindow();
See this :
GetActiveWindow function
I had this exact problem - old SDL 1.2 only uses one window, so it keeps the handle to itself. Here's the method I found from reading the source code:
Include SDL_syswm.h then get the window handle using SDL_GetWMInfo
e.g. my code for getting the handle in Windows:
SDL_SysWMinfo wmInfo;
SDL_GetWMInfo(&wmInfo);
HWND window = wmInfo.window;
SDL_SetVideoMode returns a surface based on the video frame buffer, not on a window (just like SDL_GetVideoSurface). You seem to assume that all surfaces correspond to windows, but that is not the case.