Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,

My problem is I wanna open MSPaint within another application through programming then after doing some drawing like shapes, figures and text over the untitled document of MSPaint I wanna save the same as bmp or jpeg file. all this needs to be done through programming. As of now I have done this much only. ShellExecute function to open the paint application then creating a graphics object for paint and performing some drawing operations and then generating the save menu item click event for saving the untitled document. once the Save As dialog box comes up it needs user response to save the file but i want the save button event should automatically be called through programming and also even obtaining the graphics object for paint i am not able to see any drawing on the untitled document of paint. guys please provide some solutions for my problem as I am struck there for quite few days now.

Note: Application is a windows console based application using MFC headers

Code for the invoking MSPaint is given below:

HWND hWndPaint = NULL;
    HINSTANCE hinst = ShellExecute(hWndPaint, NULL, "C:\\WINDOWS\\system32\\mspaint.exe", NULL, NULL, SW_HIDE);
    DWORD dwErr = GetLastError();
    hWndPaint = FindWindow("MSPaintApp", NULL);
    int err = (int)hinst;
    dwErr = GetLastError();
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    Graphics g(hWndPaint, true);
    Pen pen(Color::Black, 3.0f);
    g.DrawLine(&pen, 0, 10, 200, 10);
    SolidBrush brush(Color::Red);
Nothing is getting drawn on the paint untitled document
g.FillEllipse(&brush, 0, 30, 200, 100);
g.FillRectangle(&brush, 0, 155, 500, 30);
HMENU hMenu;
hMenu = GetMenu(hWndPaint);
dwErr = GetLastError();
HMENU hMenu1;
hMenu1 = GetSubMenu(hMenu, 0);
dwErr = GetLastError();
UINT unMenu;
unMenu = GetMenuItemID(hMenu1, 3);
dwErr = GetLastError();
LRESULT res = SendMessage(hWndPaint, WM_COMMAND, MAKEWPARAM(unMenu, 1), NULL);

I'm struck at this point as it needs user response to proceed further please suggest any Win API for automating the Save As process
res = SendMessage(hWndPaint, WM_CLOSE, 0, 0);
    GdiplusShutdown(gdiplusToken);
Posted
Updated 12-Oct-10 20:01pm
v2

I am not certain what you are trying to do. Are you trying to create an image file programmatically ? If yes, opening paint to do something like that is probably not the most elegant solution.
Why don't you simply create a bitmap in memory, draw to it and save the result to a file. Is it what you are looking for ?
 
Share this answer
 
I guess what you have suggested can also be a probable solution. could you please tell how a bitmap can be created in memory and then how to save its contents as a bmp or jpeg image file...
 
Share this answer
 
Comments
Cedric Moonen 14-Oct-10 4:28am    
I don't have much experience with that but if that's what you want to do, then it is much elegant solution than using MSPaint (basically, you want to redo what paint is doing internally). You can have a look at the CImage class for instance: http://msdn.microsoft.com/en-us/library/bwea7by5(VS.80).aspx.
Or even CxImage: http://www.codeproject.com/KB/graphics/cximage.aspx.
You will need to do some research but it is definitively a much better option than using paint.

If you want an example, you can have a look at the code of my charting control (http://www.codeproject.com/KB/miscctrl/High-speedCharting.aspx). In the ChartCtrl.cpp file there is a SaveAsImage function, which does exactly what you want. It is rather easy to create a CImage, draw to it and save to a file (much easier than your solution).
You may tranform MSPaint into a (COM) automation server. Have a look at this (rather 'advanced', I suppose) article: "Automating Windows Applications"[^].
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900