MSPAINT on a top level form

Published on by Yousfi Benameur

This code use Mspaint as child window of a top level form.
i cut the titlebar and position the window on the form with setWindowPos API.
The MSPAint menu and toolbars are all enabled and work.The only one problem was the QUIT
command in mspaint who closes mspaint and leaves the form blank.
I tryed bindevent without success to trap the close mspaint message.
Then i made a timer monitoring the window mspaint(with its hwnd).
When mspaint is closed, the form closes.

Here the SetPArent API works yet in Win8.1 and no longer to see the mspaint icon on taskbar.Its a good child window on the parent form.

Before run the code,change the mspaint.exe title relative to the language set on system( its localized)
In french this title is :"Sans titre - Paint  "

These codes are tested on win8.1

- Updated on vendredi 6 mars 2015; 12:01:13: the resizing is some difficult manually by mouse.can resize the form by keypress event (simply give the focus by click on form.titlebar and press CTRL+F12) set the width&height .it resizes automatically


*Begin Code
Publi yform
Read Events
Define Class ymspaint As Form
    Top = 0
    Left = 0
    Height = 505
    Width = 768
    ShowWindow = 2

    Caption = "yMSPAINT on a top level form"
    yhwnd = .F.
    noldproc = 0
    Name = "Form1"

    Add Object timer1 As Timer With ;
        Top = 48, ;
        Left = 708, ;
        Height = 23, ;
        Width = 23, ;
        Enabled = .F., ;
        Interval = 300, ;
        Name = "Timer1"

    Procedure yprocess
        Lparameters ytitle
 *Important: must be the EXACT Caption of the window depending on language used on system-place it in the form
        Local i,fail,yhwnd
        Do While yhwnd=0
            yhwnd = FindWindow(Null, Allt(ytitle))  &&localized
            If i>3
            Messagebox("Fail top find "+ytitle,16+4096,"error",1000)
            Return .F.
        #Define SW_SHOW 5
        SetParent(Thisform.yhwnd, Thisform.HWnd) && change parent

        *Remove titlebar
        #Define GWL_STYLE -16
        #Define WS_CAPTION 0x00C00000
        #Define WS_SYSMENU  0x80000
        Local lnStyle, lnNewStyle
        * Get the current style of the window
        lnStyle = GetWindowLong(Thisform.yhwnd, GWL_STYLE)
        * Remove the border of the window
        lnNewStyle = Bitxor(lnStyle, WS_CAPTION)   &&+ BITclear(lnStyle,31))
        * Set the new style for the window
        =SetWindowLong(Thisform.yhwnd, GWL_STYLE, lnNewStyle)

        *pb mspaint quit : if user close mspaint (by menu or ALT+F4) the form must close.


    Procedure Load
        Declare Integer SetParent In User32 Integer HWnd, Integer ParenthWnd
        Declare Integer SetFocus In user32 Integer
        Declare Integer FindWindow In user32;
            STRING lpClassName, String lpWindowName
        Declare Integer Sleep In kernel32 Integer

        Declare Integer GetWindowLong In User32 Integer HWnd, Integer nIndex
        Declare Integer SetWindowLong In user32 Integer HWnd,;
            INTEGER nIndex, Integer dwNewLong
        Declare Integer ShowWindow In user32;
            INTEGER HWnd,;
            INTEGER nCmdShow
        Declare Integer ShellExecute In SHELL32.Dll Integer nWinHandle,;
            STRING cOperation,;
            STRING cFileName,;
            STRING cParameters,;
            STRING cDirectory,;
            INTEGER nShowWindow

        Declare Long SetWindowPos In user32 Long HWnd, Long hWndInsertAfter, Long x, Long Y, Long cx, Long cy, Long wFlags
        Declare Integer BringWindowToTop In user32 Integer
        *Declare Integer CallWindowProc In user32;
        INTEGER lpPrevWndFunc, Integer hWindow, Long Msg,;
        INTEGER wParam, Integer Lparam
        Declare Integer IsWindow In user32 Integer hWindow

        If ! ShellExecute(0, "open", "Mspaint.exe","","",0)>32  &&Starts hidden
            Messagebox("An error was occured !",16+4096,"Error",1000)
            Return .F.

    Procedure Init
        Local m.xtitle
        m.xtitle="Sans titre - Paint    " 
&&warning ! localized (depends on langage on use in system)
        Thisform.yprocess( m.xtitle)

    Procedure Resize
            SetWindowPos (Thisform.yhwnd,1,-6,-41,Thisform.Width,Thisform.Height+41,64)

    Procedure timer1.Timer
        If !IsWindow(Thisform.yhwnd)=1

 Procedure keypress
    LPARAMETERS nKeyCode, nShiftAltCtrl

*resize the form   (and mspaint)
    if nkeycode=138  &&ctrl+F12   click on the form surface (TITLEBAR) to give the focus and make working
local m.xw,m.xh

m.xw=int(val(inputbox("Set New Width:","",trans(thisform.width))))
    if m.xw>0
 m.xh=int(val(inputbox("New Height:","",trans(thisform.height))))
    if m.xh>0

    Procedure Destroy
        Clea Events

    Procedure Destroy
        Clea Events


*End Code


MSPAINT on a top level form
MSPAINT on a top level form

Published on Visual foxpro, MSPaint, API

To be informed of the latest articles, subscribe:
Comment on this post
Hey – great blog, just looking around some blogs, seems a really nice platform you are using. I’m currently using WordPress for a few of my blogs but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?
All of my recent blog posts are built from visual foxpro .i use the recent backgrounds with no image (all is in javascript and html code).After obtaining the result i simply paste in the html area of the blog <br /> I made some vfp9 codes to build these automatically.<br /> this preserves all the code syntax (i struggled before that because the provider richtext editor cuts some of my code and made it not working.