CPaintDC in CStatic not cutting of drawing - visual-c++

My OnPaint() method in a derived CStatic-control is supposed to be cutting of parts of the drawing which are bigger than the control, as far as I know.
However it doesn't do this.
void CGraph::OnPaint ()
{
CPaintDC dc(this);
dc.SetViewportOrg (0, 400);
dc.SetMapMode(MM_ISOTROPIC);
dc.SetWindowExt(1000, 800);
dc.SetViewportExt(1000, -800);
// MessageBox(L"OnPaint");
ProcessData ();
DrawCoordinateSystem (&dc);
DrawGrid (&dc);
DrawGraph (&dc);
}

Depends on the definition of a method.
OnPaint is not really a method; it is a static member function used to handle a WM_PAINT message by mapping it in the message map array.
For a C++, I personally prefer to call it a member function for clarity.
You can create your own handler using ON_MESSAGE macro and call it whatever you desire.
The code snippet does not tell much, since it does not show the code for drawing (painting). You should include code that actually performs paining.
The best would be if you could attach project demonstrating your problem.
Did you try to paint a simple bitmap that has a size bigger than a window?

#JohnCz Got it now.
CDC* pDC = GetDC();
CRect rClient();
GetClientRect(rClient);
CRgn ClipRgn;
if (ClipRgn.CreateRectRgnIndirect(&rClient))
{
pDC->SelectClipRgn(&ClipRgn);
}
// Drawing content
pDC->SelectClipRgn(NULL);
ReleaseDC(pDC);
Thanks for your answer

Related

Alternative for ON_COMMAND message map macro to call a function with a paramenter(s)

I've got a message map at the beginning of my program that looks like the following:
BEGIN_MESSAGE_MAP(SoftwareDlg, CDialog)
//{{AFX_MSG_MAP(SoftwareDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_DESTROY()
...
ON_COMMAND(ID_TOOLS_UPLOADDATA, UploadData)
...
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
And here the function that the ID_TOOLS_UPLOADDATA menu option calls when clicked:
void UploadData()
{
string apiEndpoint = "/api/stuff";
upload_data(apiEndpoint);
}
My problem is that that I want my UploadData() function to be able to take the string apiEndpoint as a parameter so that I can call it from multiple locations in my program for multiple operations (not just when the user clicks the menu button). Like so:
void UploadData(string apiEndpoint = "/api/stuff")
{
upload_data(apiEndpoint);
}
I took a look at ON_COMMAND_EX, but the only usage example I was able to find does not appear to be what I'm looking for... Anyone have any ideas?
As #Iinspectable said, you can’t.
Try for example to add a member variable and set it before calling the function.

How to render opengl in a separate thread in Qt, using QOpenGLWidget?

I googled and found out, that there is an option to draw on a separate thread using qml.
http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html
But it's not what I need. How can I render in a separate thread using common qt widgets without qml?
If your QWidget inherits from QOpenglWidget you can just call
this->makeCurrent();
But I personally prefer a more efficient and robust way to encapsulate OpenGL context by using QWindow and configuring all the OpenGL related settings there:
Here is an example:
bool MyOpenGLWindow::Create()
{
this->requestActivate();
if(!glContext)
{
glContext= new QOpenGLContext(this);
QSurfaceFormat fmt = requestedFormat();
int maj = fmt.majorVersion();
int min = fmt.minorVersion();
glContext->setFormat( requestedFormat());
glContext->create();
if(glContext->isValid() == false)
{
QString str;
str.sprintf("Failed to create GL:%i.%i context",maj,min);
return false;
}
}
glContext->makeCurrent(this);
//after this line you can work with OpenGL
initializeOpenGLFunctions();
glDisable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//etc...
Now,remember, every time you spawn (or move existing one) an instance of such a class in a thread you must set the context current.

I need something like this, but for Phaser

i need something for adding object in Phaser, this is something similar but in wade.
wade.addSceneObject(new SceneObject(dotSprite, 0, dotPosition.x, dotPosition.y));
Adding objects (usually called sprites) in phaser is super simple. Just load the image in the preloader function
function preload() {
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
}
And then add the sprite in the create function
function create() {
// This simply creates a sprite using the mushroom image we loaded above and positions it at 200 x 200
var test = game.add.sprite(200, 200, 'mushroom');
}
Phaser has a ton of documentation on how to do things like this. I got the code from this example.
If you are completely new to phaser I highly recommend going through their tutorial

Can I use HaxeUI with HaxeFlixel?

I tried to use both HaxeUI and HaxeFlixel, but what I obtain is HaxeUI's interface over a white background, covering everything underneath. Moreover, even if it was possible to somewhat make HaxeUI and HaxeFlixel work together, it's not clear how to change the UI of HaxeUI when the state change in HaxeFlixel. Here is the code I used:
private function setupGame():Void {
Toolkit.theme = new GradientTheme();
Toolkit.init();
var stageWidth:Int = Lib.current.stage.stageWidth;
var stageHeight:Int = Lib.current.stage.stageHeight;
if (zoom == -1) {
var ratioX:Float = stageWidth / gameWidth;
var ratioY:Float = stageHeight / gameHeight;
zoom = Math.min(ratioX, ratioY);
gameWidth = Math.ceil(stageWidth / zoom);
gameHeight = Math.ceil(stageHeight / zoom);
}
trace('stage: ${stageWidth}x${stageHeight}, game: ${gameWidth}x${gameHeight}, zoom=$zoom');
addChild(new FlxGame(gameWidth, gameHeight, initialState, zoom, framerate, framerate, skipSplash, startFullscreen));
Toolkit.openFullscreen(function(root:Root) {
var view:IDisplayObject = Toolkit.processXmlResource("assets/xml/haxeui-resource.xml");
root.addChild(view);
});
}
I can guess that, probably, both HaxeUI and HaxeFlixel have their own main loop and that their event handling might not be compatible, but just in case, can someone have a more definitive answer?
Edit:
Actually, it's much better when using openPopup:
Toolkit.openPopup( { x:20, y:150, width:100, height:100 }, function(root:Root) {
var view:IDisplayObject = Toolkit.processXmlResource("assets/xml/haxeui-naming.xml");
root.addChild(view);
});
It's possible to interact with the rest of the screen (managed with HaxeFlixel), but the mouse pointer present in the part of the screen managed with HaxeFlixel remains under the HaxeUI user interface elements.
When using Flixel and HaxeUI together, its almost like running two applications at once. However, they both rely on OpenFL as a back-end and each attach themselves to its display tree.
One technique I'm experimenting with right now is to open a Flixel sub state, and within the sub state, call Toolkit.openFullscreen(). From inside of this, you can set the alpha of the root's background to 0, which allows you to see through it onto the underlying bitmap that Flixel uses to render.
Here is a minimal example of how you might "embed" an editor interface inside a Flixel sub state:
import haxe.ui.toolkit.core.Toolkit;
import haxe.ui.toolkit.core.RootManager;
import haxe.ui.toolkit.themes.DefaultTheme;
import flixel.FlxG;
import flixel.FlxSubState;
// This would typically be a Haxe UI XMLController
import app.MainEditor;
class HaxeUIState extends FlxSubState
{
override public function create()
{
super.create();
// Flixel uses a sprite-based cursor by default,
// so you need to enable the system cursor to be
// able to see what you're clicking.
FlxG.mouse.useSystemCursor = true;
Toolkit.theme = new DefaultTheme();
Toolkit.init();
Toolkit.openFullscreen(function (root) {
var editor = new MainEditor();
// Allows you to see what's going on in the sub state
root.style.backgroundAlpha = 0;
root.addChild(editor.view);
});
}
override public function destroy()
{
super.destroy();
// Switch back to Flixel's cursor
FlxG.mouse.useSystemCursor = true;
// Not sure if this is the "correct" way to close the UI,
// but it works for my purposes. Alternatively you could
// try opening the editor in advance, but hiding it
// until the sub-state opens.
RootManager.instance.destroyAllRoots();
}
// As far as I can tell, the update function continues to get
// called even while Haxe UI is open.
override public function update() {
super.update();
if (FlxG.keys.justPressed.ESCAPE) {
// This will implicitly trigger destroy().
close();
}
}
}
In this way, you can associate different Flixel states with different Haxe UI controllers. (NOTE: They don't strictly have to be sub-states, that's just what worked best in my case.)
When you open a fullscreen or popup with haxeui, the program flow will be blocked (your update() and draw() function won't be called). You should probably have a look at flixel-ui instead.
From my experience haxeflixel and haxeui work well together but they are totally independent projects, and as such, any coordination between flixel states and displayed UI must be added by the coder.
I don't recall having the white background problem you mention, it shouldn't happen unless haxeui root sprite has a solid background, in that case it should be addressed to haxeui project maintainer.

How to call a function in mfc...?

I am new to MFC PROGRAMMING. I use vs 2008, in a Dialog Based App. I want to call bellow function on a button click event...?
When I call like SortList(listboxone); is giving an error that SortList not found...!
Please help me..!!
void SortList(CListBox& templistbox)
{
DWORD_PTR abc;
int a=templistbox.GetCurSel();// Select current Item Index
if(a<templistbox.GetCount()-1)
{
abc = (DWORD_PTR )templistbox.GetItemData(a);
a++;
templistbox.SetItemData(a,(DWORD_PTR) templistbox.GetItemData(templistbox.GetCurSel()));
}
}
Sorry now I changed the function to as above but still gives same error.
You probably are calling the function above the function definition. In C/C++, you need to define the function (or it's prototype at least) before calling the function. Put this:
void SortList(CListBox& templistbox);
at the the top of the source file.

Resources