Re: Getting Started with Python on Mobile windows 2003

"Edward Fewell" <[email protected]>
Newsgroups gmane.comp.python.windows-ce
Message-ID <[email protected]>
<< There is a win32gui module, which exposes the Mobile windows API. >>

There should also be a win32api module and win32con module. I don't have
those in my installation, and haven't had luck finding them.  win32con is
just a collection of constants, so the desktop version would be very, very
close with a few tweaks.  And we can skip using win32api for a simple demo.

There's a bigger problem in that win32gui isn't working (or at least the
version I have).  And it's also incomplete (even the desktop version is
missing some of the APIs).

Below is a sample that should create a window.  You can run it on a desktop,
and it correctly creates the window.  The print messages show up in the
console window confirming that the message pump is hooked in and getting the
WM_PAINT messages.  Sadly, that's where I wanted to put code to put text on
the window, but DrawText() isn't implemented in win32gui.

Also the WM_DESTROY message happens, I manually issue a quit message, the
gui window closes, but the console window stays open.  We never return from
the message pump.

Under CE, I see all those problems plus an issue that CE never paints the
window background.  Also it leaves the hourglass cursor up.  I do get a
window, and can drag it around by the title bar.  I can even see that it is
getting Windows messages.  But it's just not getting redrawn, so it's not
very useful yet.  

------------------------------------------------------------------------

import struct
#import win32api
#import win32con
import win32gui
import os

# Defines taken from desktop win32con.py
class win32con:
    CS_VREDRAW          = 1
    CS_HREDRAW          = 2
    IDC_ARROW           = 32512
    COLOR_WINDOW        = 5
    IDI_APPLICATION     = 32512
    DLGWINDOWEXTRA      = 32
    WS_OVERLAPPED       = 0
    WS_VISIBLE          = 268435456
    WS_CAPTION          = 12582912
    WS_SYSMENU          = 524288
    WS_THICKFRAME       = 262144
    WS_MINIMIZEBOX      = 131072
    WS_MAXIMIZEBOX      = 65536
    WS_OVERLAPPEDWINDOW = (WS_OVERLAPPED   | \
                           WS_CAPTION      | \
                           WS_SYSMENU      | \
                           WS_THICKFRAME   | \
                           WS_MINIMIZEBOX  | \
                           WS_MAXIMIZEBOX)
    CW_USEDEFAULT       = -2147483648
    GWL_WNDPROC         = -4
    SW_SHOW             = 5
    WM_PAINT            = 0x000F
    WM_DESTROY          = 0x0002
    MB_OK               = 0x0000

class MainWindow:
    def __init__(self):
        # Customize for WinCE
        if os.name == "ce":
            print "Running under CE"
            className = unicode("win32gui test") # not coverted to unicode
by win32gui
            style = win32con.WS_VISIBLE          # WS_OVERLAPPEDWINDOW isn't
valid style for CE
            hIcon  = 0                           # LoadIcon on
IDI_APPLICATION isn't working      
        else:
            print "Running on desktop"
            className = "win32gui test"
            style = win32con.WS_OVERLAPPEDWINDOW
            hIcon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)

        # Initialize instance
        win32gui.InitCommonControls()
        self.hinst = 0 # win32api.GetModuleHandle(None)

        # Register class
        wc = win32gui.WNDCLASS()
        wc.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
        wc.lpfnWndProc = self.MyWndProc
        wc.hCursor = win32gui.LoadCursor( 0, win32con.IDC_ARROW )
        wc.hbrBackground = win32con.COLOR_WINDOW + 1
        wc.hIcon = hIcon
        wc.lpszClassName = className
        wc.cbWndExtra = 0

        classAtom = win32gui.RegisterClass(wc)

        # Create window
        style = win32con.WS_OVERLAPPEDWINDOW | win32con.WS_VISIBLE
        self.hwnd = win32gui.CreateWindow(className,
                                          "win32gui Sample",
                                          style,
                                          win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT,
                                          win32con.CW_USEDEFAULT,
                                          0,
                                          0,
                                          self.hinst,
                                          None)
        # Hook WndProc
        self.oldWndProc = win32gui.SetWindowLong (self.hwnd,
                                                 win32con.GWL_WNDPROC,
                                                 self.MyWndProc)

        win32gui.ShowWindow(self.hwnd, win32con.SW_SHOW)
        win32gui.UpdateWindow(self.hwnd)

    def MyWndProc(self, hwnd, message, wparam, lparam):
        if message == win32con.WM_PAINT:
            return self.OnPaint(hwnd, message, wparam, lparam)
        if message == win32con.WM_DESTROY:
            return self.OnDestroy(hwnd, message, wparam, lparam)
     
        return win32gui.CallWindowProc(self.oldWndProc, hwnd, message,
wparam, lparam)

    def OnPaint(self, hwnd, message, wparam, lparam):
        print "on paint"
        return win32gui.CallWindowProc(self.oldWndProc, hwnd, message,
wparam, lparam)

    def OnDestroy(self, hwnd, message, wparam, lparam):
        print "on destroy"
        win32gui.PostQuitMessage(0)
        return True

print "Starting up ..."
win32gui.MessageBox(0, "Hello world!", "win32gui Sample", win32con.MB_OK)
w = MainWindow()
win32gui.PumpMessages()
print "Quit?"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.