Good Afternoon,
So I'm working with C++ (Visual Studios C++ 2010 to be exact) and am working on a seemingly easy task;
Draw a picture onto the middle of the window. If you readjust the window size, the picture/bitmap will be redrawn into the middle of the newly sized window.
I figure to get the middle of the window, I should find its rightmost and bottommost bits, then divide each by 2, but I don't know how to get the window length and height.
I currently have
centrewidth = glutGetWindow().Size.Width;
centreheight = glutGetWindow().Size.Height;
Yet for both I'm getting errors on glutGetWindow, saying "Error: Expression must have class type". I'm certain once I get this, it will be no problem, but this is causing a lot of trouble for me. Any advice is greatly appreciated. Thanks!
glutGetWindow doesn't return object of any class type. It's signature is this:
int glutGetWindow(void);
And you need is this (see doc):
int glutGet(GLenum eWhat);
Example,
int width = glutGet(GLUT_WINDOW_WIDTH); //Width in pixels of the current window.
int height = glutGet(GLUT_WINDOW_HEIGHT); //Height in pixels of the current window.
Have a look at the doc to know what states you can get using this function.
Related
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.
I am creating a GUI using C++ and winapi. I have been able to work out a lot of details, but I'm having a bit of trouble with the scrolling of panels within another panel. I know how to do this in c#, but with c++ I am defeated.
I have created class which generates a panel for a generic "Topic" and I can designate the name of the topic (e.g., "Topic A"). I have create a separate class which generates a Panel to contain all of these topics. That is the TopicHolder. So I pass in a vector with the names as strings. I then generate new Topic objects, each with their own GUI control. I then update the layout of TopicHolder, including all of the Topics, giving them x and y locations based on their standard size.
Everything looks more or less like I want it to initially. If this was c# the topic holder would scroll and the various topics would scroll in and out of sight depending on the exact portion of the larger panel which was visible. In this case, however, scrolling has absolutley no effect. This is my first time working with scrolling on a c++ gui, so I might be missing something.
Below are some of the key code segments. I'd welcome any helpful tips.
Thanks
Adding the topics
void TopicHolder::SetTopics(std::vector<std::string> t)
{
for(unsigned i=0;i<t.size();i++)
{
//I give the topic the HWND of the TopicHolder so it can
//be properly parented on that GUI element
Topic* tmp = new Topic(myHWND, t[i]);
vectorOfTopics.push_back(tmp);
}
UpdateLayout(holderRect);
}
Handling topic layout relative to TopicHolder
void TopicHolder::UpdateLayout(RECT r)
{
int buffer = 5;//buffer between elements
int x = buffer;
int y = buffer;
for(unsigned i=0;i<vectorOfTopics.size();i++)
{
RECT tmp;
tmp.left=x;
tmp.top=y;
tmp.right = r.right-(2*buffer);
tmp.bottom = y+Topic::HEIGHT;//standard height per topic
vectorOfTopics[i].Layout(tmp);
y+= Topic::HEIGHT+buffer;
}
}
Layout in Topic object
void Topic::Layout(RECT r)
{
SetWindowPos(myHWND, //this is the HWND of the topic
HWND_TOP, r.left, r.top, Topic::WIDTH, Topic::HEIGHT, SWP_SHOWWINDOW);
}
-------------------Edit-----------------------------------------
Grr... Okay, so C++ has no support for this as many of you have explained. I have (painfully) been learning to set up my scroll parameters. Mostly it is working as expected, but I still have a problem with the scrollbar. I expected the scrollbar to show up on the right side of the panel and leave the rest of the space for my Topic panels. Instead it is slightly offset and the rest of the area is painted a light gray rather than the background color of the TopicHolder. Right now it's just irritating, but I would like to see it resolved. Again, any help would be appreciated.
Starting y position of topics in UpdateLayout always remains 5, so no scrolling happens. You should keep some offset value and change it if scrolling up and down happens so y will start from this offset.
You need to call SetScrollPos() to change the scrolling position or the window contents.
i.e:
// hwnd is your control window, SB_VERT refers to its verti8cal scrollbar.
SetScrollPos(hwnd, SB_VERT, topicHeight * topicToBringToTop, TRUE);
InvalidateRect(hwnd, NULL, TRUE); // redraw the control.
As of now, I'm using the following to code to resize my CStatic controls:
WINDOWPLACEMENT wndpl;
m_myStaticControl.GetWindowPlacement(&wndpl);
// Increase the static box's width
wndpl.rcNormalPosition.right += 10;
m_myStaticControl.SetWindowPlacement(&wndpl);
m_myStaticControl.SetWindowText("Some text");
I obtain the constant (in the above case, 10) by trial-and-error. Because this seems like a really inelegant and hard-to-maintain solution, I want to change this. After some research, I think I have a basic idea; which is:
Get the pixel width and height of the required text using GetTextExtentPoint32.
Get the current window placement of the CStatic control as in code example above.
If the current width < obtained pixel width, add obtained pixel width. Do the same for height.
Set the window placement as in code example above.
Set the window text as in code example above.
Would this be a good, efficient approach? Also, does GetTextExtentPoint32 use pixels or dialog units?
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).
Stuck on a little fiddly problem. I'm creating a GUI in C++ using XP and VS C++ using the command CreateWindow().
My question is, how do I make the inside paintable region a perfect square. When passing in the size of the window to create, some of this is deducted for the menu bar at the top, border all around etc. Are there any real time variables I can pass in, e.g. to create a 500x500 window would be:
...500+BORDER,500+MENU_TOP+BORDER...
Thanks everyone
The way I usually do it is with AdjustWindowRect. I find it simpler than the other suggested methods (which should work just as well, it's your choice). Use it as such:
RECT rect = {0, 0, desiredWidth, desiredHeight};
AdjustWindowRect(&rect, windowStyle, hasMenu);
const int realWidth = rect.right - rect.left;
const int realHeight = rect.bottom - rect.top;
And pass realWidth & realHeight to CreateWindow.
The function will, as its name suggests, adjust the window according to your window style and menu use, so that the client region matches your desired size.
you can find all the relevant size (windows framewidth, menubar height, etc) here: GetSystemMetrics(). Using these values you should be able to create a perfect square window
You can get all the UI metrics from the GetSystemMetrics() API call.
For example, the menu will be SM_CXMENU and SM_CYMENU.