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
yform=Newobject("ymsPaint")
yform.Show
Read Events
*
Define Class ymspaint As Form
    Top = 0
    Left = 0
    Height = 505
    Width = 768
    ShowWindow = 2

Keypreview=.t.
   Autocenter=.t.
    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
        i=0
        fail=.F.
        yhwnd=0
        Do While yhwnd=0
            yhwnd = FindWindow(Null, Allt(ytitle))  &&localized
            sleep(1000)
            i=i+1
            If i>3
                m.fail=.T.
                Exit
            Endi
        Enddo
        If m.fail=.T.
            Messagebox("Fail top find "+ytitle,16+4096,"error",1000)
            Return .F.
        Endi
        #Define SW_SHOW 5
        ShowWindow(Thisform.yhwnd,SW_SHOW)
        Thisform.yhwnd=m.yhwnd
        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.
        Thisform.timer1.Enabled=.T.

        Thisform.Resize()
    Endproc

    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.
        Endi
        sleep(500)
    Endproc

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

    Procedure Resize
      dodefault()
        Try
            SetWindowPos (Thisform.yhwnd,1,-6,-41,Thisform.Width,Thisform.Height+41,64)
            BringWindowToTop(Thisform.yhwnd)
        Catch
        Endtry
    Endproc

    Procedure timer1.Timer
        If !IsWindow(Thisform.yhwnd)=1
            This.Enabled=.F.
            Thisform.Release
        Endi
    Endproc

 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=thisform.width
m.xh=thisform.height

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

    Procedure Destroy
        Clea Events
    Endproc

    Procedure Destroy
        Clea Events
    Endproc

Enddefine
*

*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
S
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?
Reply
Y
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.