新手,上个随手小段,MP3播放程序。

highwing 2006-11-16
//Tinyplayer
//Created by Highwing.

module Tinyplayer;

private import dwt.all;
private import std.string;
private import std.utf;
private import std.c.windows.windows;

version(build)
{
    pragma(link, "dwt.lib");
	
    pragma(link, "user32_dwt.lib");
    pragma(link, "imm32_dwt.lib");
    pragma(link, "shell32_dwt.lib");
    pragma(link, "msimg32_dwt.lib");
    pragma(link, "gdi32_dwt.lib");
    pragma(link, "kernel32_dwt.lib");
    pragma(link, "usp10_dwt.lib");
    pragma(link, "olepro32_dwt.lib");
    pragma(link, "oleaut32_dwt.lib");
    pragma(link, "oleacc_dwt.lib");
	
    pragma(link, "Tinyplayer.res");
}

alias DWORD MCIERROR;
alias mciSendStringW mciSendString;

extern(Windows)	MCIERROR mciSendStringW(LPCWSTR,LPWSTR,UINT,HWND);

class Tinyplayer
{
    private Shell shell;
    private Menu trayMenu;
    private TrayItem trayItem;
    private FileDialog fd;
    private Image image;
	
    private enum TPSTAT {READY, PLAY, PAUSE}
	
    private TPSTAT stat;
    private char[][] fext = ["*.mp3;*.wma","*.*"];
    private char[] fname;

    this(char[][] args)
    {
        stat = TPSTAT.READY;
        fname = null;
        fd = null;
    }

    public Shell initGUI(Display display)
    {
        shell = new Shell(display);
		
        DWTResourceManager.registerResourceUser(shell);
        image = DWTResourceManager.getImage("ID_ICON_101", "ICO");

        Tray tray = display.getSystemTray();
        trayItem = new TrayItem(tray, DWT.NONE);
        trayItem.setImage(image);
        trayItem.handleEvent(this, DWT.MenuDetect, &onTrayEvent);
        trayItem.handleEvent(this, DWT.Selection, &onTrayEvent);

        trayItem.setToolTipText("");
		
        trayMenu = new Menu(shell);

        with(new MenuItem(trayMenu, DWT.PUSH))
        {
            setText("打开影音文件...");
            handleEvent(this, DWT.Selection, &onMenuEvent);
        }

        with(new MenuItem(trayMenu, DWT.PUSH))
        {
            setText("暂停/继续");
            handleEvent(this, DWT.Selection, &onMenuEvent);
        }

        with(new MenuItem(trayMenu, DWT.PUSH))
        {
            setText("关闭影音文件");
            handleEvent(this, DWT.Selection, &onMenuEvent);
        }

        new MenuItem(trayMenu, DWT.SEPARATOR);

        with(new MenuItem(trayMenu, DWT.PUSH))
        {
            setText("退出(&X)");
            handleEvent(this, DWT.Selection, &onMenuEvent);
        }
        return shell;
    }
	
    private void onTrayEvent(Event e)
    {
        if(e.type == DWT.MenuDetect)
        {
            Point pt = shell.getDisplay().getCursorLocation();
            trayMenu.setLocation(pt.x, pt.y);
            trayMenu.setVisible(true);
        }
        else if(e.type == DWT.Selection)
        {
            pauseCtrl();
        }
    }
	
    private void onMenuEvent(Event e)
    {
        switch((cast(MenuItem)e.widget).getText())
        {
            case "打开影音文件...":
                openCtrl();
                break;
            case "暂停/继续":
                pauseCtrl();
                break;
            case "关闭影音文件":
                closeCtrl();
                break;
            case "退出(&X)":
                closeCtrl();
                shell.dispose();
                break;
            default:
                break;
        }
    }

    private void openCtrl()
    {
        FileDialog fd = new FileDialog(shell, DWT.OPEN);
        fd.setFilterExtensions(fext);
        char[] newfname = fd.open();
        if(newfname)
        {
            fname = newfname;
            mciSendString("close MEDIA", null, 0, null);
            mciSendString(toUTF16z("open \"" ~ fname ~ "\" alias MEDIA"), null, 0, null);
            mciSendString("play MEDIA repeat", null, 0, null);
            stat = TPSTAT.PLAY;
            trayItem.setToolTipText("正在播放 " ~ fname);
        }		
    }

    private void closeCtrl()
    {
        mciSendString("close MEDIA", null, 0, null);
        stat = TPSTAT.READY;
        trayItem.setToolTipText("请选择影音文件");
    }

    private void pauseCtrl()
    {
        if(stat == TPSTAT.PLAY)
        {
            mciSendString("pause MEDIA", null, 0, null);
            stat = TPSTAT.PAUSE;
            trayItem.setToolTipText("暂停播放 " ~ fname);
        }
        else if(stat == TPSTAT.PAUSE)
        {
            mciSendString("resume MEDIA", null, 0, null);
            stat = TPSTAT.PLAY;
            trayItem.setToolTipText("正在播放 " ~ fname);
        }
    }
}

void main(char[][] args)
{
    try
    {
        Display display = Display.getDefault();
        Shell shell = (new Tinyplayer(args)).initGUI(display);
        while(!shell.isDisposed())
        {
            if(!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
    catch(Object err)
    {
        MessageBox.showMsg(err.toString());
    }
}
qiezi 2006-11-16
不错,好像缺少Tinyplayer.res,亚

没做过这方面,想不到mci用起来这么简单。dwt好久没更新了,还可以直接编译吗?我现在很少用windows。

另外,代码用[code][/code]包起来吧

发到你的博客中,选上“同时发到D语言群博”,方便别人从圈子首页上看到。

圈子论坛我估计灌水、搞活动比较合适。
highwing 2006-11-16
多谢qiezi提醒,代码处理了,Blog还在筹划,有过两次失败尝试,呵呵!

Tinyplayer.rc只是一个图标资源:

ID_ICON_100 ICON DISCARDABLE "D.ico"
ID_ICON_101 ICO DISCARDABLE "D.ico"

至于DWT,我现在使用的版本最初来自un_guru的修改版:

http://www.dsource.org/forums/viewtopic.php?t=1661

而后由于D语言中默认import已更改为private,我又修改了几处import引用,用最新的dmd 0.174和bud(build)3.04 成功编译。

DWT是一个令人惋惜的项目,不过最近看新闻组有人提出要再次移植swt3.2.1,期盼中...
http://www.digitalmars.com/d/archives/digitalmars/D/SWT_port_43926.html

sw2wolf 2007-10-14
我下载了DWT, 但编译不成功!
c:\D>dmd tinyplayer.d -Idwt\import
dwt\import\dwt\custom\styledtexthelper.d(134): function dwt.custom.styledtexthel
per.RANGE.opAssign identity assignment operator overload is illegal
sleets 2007-10-14
今天没事,做了个Js版本的播放器。
http://music.xwall.cn/demo/player/iplayer.php
sw2wolf 2007-10-17
e:\dmd\bin\..\..\dm\bin\link.exe tinyplayer,a.exe,,dwt.lib+advapi32.lib+comctl32
.lib+gdi32.lib+shell32.lib+comdlg32.lib+ole32.lib+user32_dwt.lib+imm32_dwt.lib+s
hell32_dwt.lib+msimg32_dwt.lib+usp10_dwt.lib+gdi32_dwt.lib+kernel32_dwt.lib+olep
ro32_dwt.lib+oleaut32_dwt.lib+oleacc_dwt.lib+uuid.lib+user32+kernel32/noi/SUBSYS
TEM:windows:5;
OPTLINK (R) for Win32  Release 8.00.1
Copyright (C) Digital Mars 1989-2004  All rights reserved.
tinyplayer.obj(tinyplayer)
Error 42: Symbol Undefined _mciSendStringW@16
--- errorlevel 1

mciSendString在哪个库里啊 ?
tomqyp 2007-10-17
sw2wolf 写道


mciSendString在哪个库里啊 ?

winmm.lib
sw2wolf 2007-10-17
thanks
highwing 2007-10-20
哦?谁又把我这个旧帖翻出来了,我都编译不了,没有了DWT,呵呵~
Global site tag (gtag.js) - Google Analytics