| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6359 人关注过本帖
标题:All about windows programming
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

接37楼

第4种操作:调用函数 DeleteMenu 删除菜单项,其函数原型为:BOOL DeleteMenu(HMENU hmenu, UINT wPosition, UINT dwFlags)

参数 wPosition 的意思取决于 参数 dwFlags 的意思。如果dwFlags 为 MF_BYCOMMAND, 则 wPosition 为菜单项的 ID 值,如果dwFlags 为MF_BYPOSITION, 则wPosition 为菜单项的位置号。另外,如果菜单项含有弹出式菜单,则删除该菜单项时该弹出式菜单也同时被删除。例如,删除弹出式 "编辑"菜单中的"复制"项的形式如下:

DeleteMenu(hMenu, IDM_COPY, MF_BYCOMMAND)

第5种操作:修改菜单项

应用程序可调用函数 ModifyMenu 修改菜单中的某个项,该函数的原型为:

BOOL ModifyMenu(HMENU hmenu, UINT wPosition, // 指定需修改的菜单项位置

UINT dwFlags, UINT dwIDNewItem, // 修改后的菜单项标识

LPCTSTR lpNewItem // 修改后的菜单项名

);

对于 wPosition, 如果dwFlags 为MF_BYCOMMAND, 则该参数为菜单项的 ID 值,如果dwFlags 为MF_BYPOSITION, 则该参数为菜单项的位置号。 如:修改弹出式菜单"文件"中"打开"项为"加载"项的语句如下:

ModifyMenu(hmenu, IDM_OPEN, MF_BYCOMMAND, IDM_LOAD"加载(&L)");

除此以外,我们还可以动态的创建菜单,使得资源更加节省,在应用程序中动态创建菜单分两个步骤:

1)调用函数 HMENU CreateMenu(void) 创建空的弹出式菜单,

2)调用函数 AppendMenu 或 InsertMenu 在菜单中加入菜单项。

// to be continued, 下次关于加速键资源


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-10-11 02:13
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

创建加速键的步骤如下:

1。 定义加速键资源

定义形式为: 加速键的名字 ACCELERATORS {加速键的定义}

加速键定义的格式为:键名,加速键标识(ID), [类型][NOINVERT][ALT][SHIFT][CONTROL]

其中:

加速键标识:与所表示的菜单项标识相同的标识值

类型:标识改建为标准键还是虚拟键

NOINVERT:表示当使用加速键时,菜单项不高亮度显示

ALT, SHIFT, CONTROL:表示加速键的组合方式

常用的加速键有两种形式:

1)"^char", id

与Ctrl键组合的加速键。例如"编辑"菜单中"复制"项的加速键可定义为:"^C", IDM_COPY

2 ) nCode, idVIRTKEY

使用虚拟键作为加速键。虚拟键是系统提供的与设备无关的键码, 如键盘上的F功能键,方向键,Delete 键等等。Windows.H 文件中以 VK 开头定义虚拟键,如VK_F1, VK_DELETE 等。例如将F1键定义为"帮助"菜单项的加速键,其形式为  VK_F1, IDM_HELP, VIRTKEY

2. 加载加速键资源

在应用程序定义加速键资源 handle 后,即可通过调用函数 LoadAccelerators 加载加速键资源,其形式为:

。。。

HACCEL hAccel;

。。。

hAccel = LoadAccelerators(hInstance, lpAccelName);

其中: lpAccelName 加速键表名

3。 翻译加速键

应用程序使用加速键的目的是快捷的切换到需要的菜单项,因此,应用程序需要完成加速键消息到菜单消息的翻译。该翻译操作经常在应用程序的消息循环中进行,其形式如下:

while(GetMessage(&Msg, NULL, 0, 0))

{

if(!TranslateAccelerators(hwnd, hAccel, &Msg))

{

TranslateMessage(&Msg);

DispatchMessage(&Msg);

}

}

函数TranslateAccelerators 是翻译操作的核心,该函数的原型为:int TranslateAccelerators(HWND hwnd, HACCEL hAccel, lpMSG lpMsg) // lpMsg 为指向MSG结构的指针。

函数 TranslateAccelerators 的作用是对照加速键表,将相关的按键消息 WM_KEYDOWN 和 WM_KEYUP 翻译成WM_COMMAND 或WM_SYSCOMMAND 消息。其特点是将翻译后的WM_COMMAND 或 WM_SYSCOMMAND 消息直接发往窗口,而不在消息队列中等待。消息翻译完成后,函数返回非0值

// to be continued, next time an example program


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-10-12 01:06
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
很高兴,这个帖子再次回到C++教室板块,这个帖子是我本人移到Windows编程板块。但何故被删除,我就不得而知了。当时live41建议我出任Windows 编程板块的版主,我没有应答,我觉得我的能力还很有限,现在还是无意出任那里的版主。在Windows编程这个领域,我无力胜任版主这个位置,怕有失大家的期望。出于对 windows编程的爱好和热情,我还是愿意继续我的这个帖子,将我对windows 编程的感知写出来,希望能与大家共勉,学习windowsprogramming 是一件很有趣的事情,但也是非常困难的事情,很多学习者在这条道路上折腰。它需要你的毅力,你的激情。让我们继续。。。

我很想上传  Programming Windows by Charles Petzold  这本E-book. 但考虑到商业版权的问题,所以在这里我不上传了,以避免不必要的纠纷。
如果哪位朋友对此书感兴趣,可以向我索取,我可以Email 给你。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-01-14 18:59
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
出于好玩,一个小代码:

#include <windows.h>

#include <cmath>

#define PI 3.1415926



class Pentacle

{

private:

   POINT points[5];

   POINT mittel;

   double length;

public:

   Pentacle(){}

   Pentacle(const POINT & mP, const double l);

   void createPentacle(const POINT & mP, const double l);

   void paint1(HDC, HWND);

   void paint2(HDC, HWND);

   void paint3(HDC, HWND);

};



Pentacle::Pentacle(const POINT & mP, const double l)

{

   mittel = mP;

   length = l;

   for(int i = 0; i<5; i++)

   {

      points[i].x = (LONG)(length/2/sin(36*PI/180)*sin(i*72*PI/180) + mittel.x);

      points[i].y = (LONG)(length/2/sin(36*PI/180)*cos(i*72*PI/180) + mittel.y);

   }

}



void Pentacle::createPentacle(const POINT & mP, const double l)

{

   mittel = mP;

   length = l;

   for(int i = 0; i<5; i++)

   {

      points[i].x = (LONG)(length/2/sin(36*PI/180)*sin(i*72*PI/180) + mittel.x);

      points[i].y = (LONG)(length/2/sin(36*PI/180)*cos(i*72*PI/180) + mittel.y);

   }

}



void Pentacle::paint1(HDC hDC, HWND hWnd)

{

   //PAINTSTRUCT ps;

   //HDC hDC = BeginPaint(hWnd, &ps);

   HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));

   SelectObject(hDC, hPen);

   POINT points[6];// = {{100,100}, {129,121}, {118,154},{82,154},{71,121},{100,100}};

   

   for(int i = 0; i<5; i++)

   {

      points[i] = Pentacle::points[i];

   }

   points[5] = Pentacle::points[0];

   

   Polyline(hDC, points, 6);

   DeleteObject(hPen);

   //EndPaint(hWnd, &ps);

}



void Pentacle::paint2(HDC hDC, HWND hWnd)

{

   POINT points[6];// = {{100,100}, {129,121}, {118,154},{82,154},{71,121},{100,100}};

   

   for(int i = 0; i<5; i++)

   {

      points[i] = Pentacle::points[i];

   }

   points[5] = Pentacle::points[0];

   

   HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));

   SelectObject(hDC, hPen);

   MoveToEx(hDC, points[0].x, points[0].y, NULL);

   LineTo(hDC, points[2].x, points[2].y);

   DeleteObject(hPen);

   hPen = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));

   SelectObject(hDC, hPen);

   LineTo(hDC, points[4].x, points[4].y);

   DeleteObject(hPen);

   hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));

   SelectObject(hDC, hPen);

   LineTo(hDC, points[1].x, points[1].y);

   DeleteObject(hPen);

   hPen = CreatePen(PS_SOLID, 2, RGB(125, 125, 125));

   SelectObject(hDC, hPen);

   LineTo(hDC, points[3].x, points[3].y);

   DeleteObject(hPen);

   hPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 125));

   SelectObject(hDC, hPen);

   LineTo(hDC, points[0].x, points[0].y);

   DeleteObject(hPen);

}



void Pentacle::paint3(HDC hDC, HWND hWnd)

{

   POINT points[6];// = {{100,100}, {129,121}, {118,154},{82,154},{71,121},{100,100}};

   

   for(int i = 0; i<5; i++)

   {

      points[i] = Pentacle::points[i];

   }

   points[5] = Pentacle::points[0];

   

   HBRUSH hBrush = CreateSolidBrush(RGB(0, 128, 255));

   SelectObject(hDC, hBrush);

   Polygon(hDC, points, 6);

   DeleteObject(hBrush);

}

/*  Declare Windows procedure  */

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);



/*  Make the class name into a global variable  */

char szClassName[ ] = "WindowsApp";



int WINAPI WinMain (HINSTANCE hThisInstance,

                  
HINSTANCE hPrevInstance,

                  
LPSTR lpszArgument,

                  
int nFunsterStil)



{

    HWND
hwnd;              
/* This is the handle for our window */

    MSG
messages;           
/* Here messages to the application are saved */

    WNDCLASSEX wincl;        /* Data structure for the windowclass */



    /* The Window structure */

    wincl.hInstance = hThisInstance;

    wincl.lpszClassName = szClassName;

    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */

    wincl.style =
CS_DBLCLKS;               
/* Catch double-clicks */

    wincl.cbSize = sizeof (WNDCLASSEX);



    /* Use default icon and mouse-pointer */

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);

    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);

    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);

    wincl.lpszMenuName =
NULL;               
/* No menu */

    wincl.cbClsExtra =
0;                     
/* No extra bytes after the window class */

    wincl.cbWndExtra =
0;                     
/* structure or the window instance */

    /* Use Windows's default color as the background of the window */

    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;



    /* Register the window class, and if it fails quit the program */

    if (!RegisterClassEx (&wincl))

        return 0;



    /* The class is registered, let's create the program*/

    hwnd = CreateWindowEx (

         
0,                  
/* Extended possibilites for variation */

         
szClassName,         /*
Classname */

           "Pentacle",       /* Title Text */

           WS_OVERLAPPEDWINDOW, /* default window */

         
CW_USEDEFAULT,       /* Windows decides
the position */

         
CW_USEDEFAULT,       /* where the window
ends up on the screen */

         
544,               
/* The programs width */

         
375,               
/* and height in pixels */

         
HWND_DESKTOP,        /* The window
is a child-window to desktop */

         
NULL,               
/* No menu */

         
hThisInstance,       /* Program Instance
handler */

         
NULL               
/* No Window Creation data */

           );



    /* Make the window visible on the screen */

    ShowWindow (hwnd, nFunsterStil);



    /* Run the message loop. It will run until GetMessage() returns 0 */

    while (GetMessage (&messages, NULL, 0, 0))

    {

        /* Translate virtual-key messages into character messages */

        TranslateMessage(&messages);

        /* Send message to WindowProcedure */

        DispatchMessage(&messages);

    }



    /* The program return-value is 0 - The value that PostQuitMessage() gave */

    return messages.wParam;

}





/*  This function is called by the Windows function DispatchMessage()  */



LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    HDC hdc;

    PAINTSTRUCT ps;

    HPEN hPen;

    POINT mittel;

    POINT points[6];

    int length;

    Pentacle myPentacle;

    switch
(message)                 
/* handle the messages */

    {

        case WM_DESTROY:

           
PostQuitMessage (0);       /* send a
WM_QUIT to the message queue */

            break;

        case WM_PAINT:

            hdc = BeginPaint(hwnd, &ps);

            mittel.x = 150;

            mittel.y = 150;

            length = 150;

         

            myPentacle.createPentacle(mittel, length);

            myPentacle.paint1(hdc, hwnd);

            myPentacle.paint3(hdc, hwnd);

            myPentacle.paint2(hdc, hwnd);

            EndPaint(hwnd, &ps);

            break;

      
default:                     
/* for messages that we don't deal with */

            return DefWindowProc (hwnd, message, wParam, lParam);

    }

    return 0;

}




自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-02 10:12
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
发现大家对 Windows 编程都没什么兴趣,是不是太难了?



我最近在写五子棋游戏,模拟联众的五子棋界面。程序写了个开头。

如果谁有兴趣,可以报名加入。我可以对加入的朋友公开源代码。



代码用 C++ 和 Winapi32 写的,不要误解,此程序与MFC 无关。

编译环境为 VC 6.0。

想实现人人对战模式和人计对战模式。


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-12 05:29
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
到昨天已经基本完成五子棋程序的人人对战模式,写地比较急,对效率考虑地肯定有不周到的地方。

接下来,我会用一个星期的时间来检测代码的安全性,和正确性以及高效,对于部分未实现的功能,添加新的代码。

这里好像不能上传文件了,所以有兴趣的可以通过email 向我索取执行代码和源代码。



这个五子棋程序可以说是刚刚拉开了序幕,我并不打算把它的界面做的太华翘,也不打算提供太复杂的功能,比如计时功能,比如环境设置等我都不提供了。



喜欢玩五子棋的朋友,一定清楚,当前最厉害的是黑石。写一款算力强劲的五子棋游戏程序是我的目标,还是那句话,如果有朋友愿意加入,可以联系。



记得当时有很多朋友,有意向开发项目,怎么现在都没人反映了呢??

这个程序开发之后,还有很多可以做。比如打飞机游戏,比如中国象棋,比如国际象棋。等等。

我想,学了C++,总要开发点什么吧,要不然,学了有啥用?

有兴趣的朋友一起来加入吧。

对于C++ 和 Winapi32 有问题的朋友,可以提出来,我会尽量予以答复,当然在我自己清楚地情况下。 即便毫无基础,也可以边看边学。

不过系统的学习C++ 很重要。


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-15 01:50
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
windows programming 到底是什么? 是MFC? 还是C#? 还是其他的什么? C++ 对windows programming 的作用是什么?



Charles Petzold 写的Windows Programming  是彻彻底底 用C 和 Winapi 写的 windows  教学代码。

那么是不是 C++ 与windows Programming 无关了呢? 回答当然不是。

那么是不是 一提到 windows Programing  的 C++ 就是指 MFC? 回答是:不是。 MFC 提供了一种可能,
但不是必须,我们完全可以忘记 MFC.  我也确确实实打算将MFC 抛到九霄云外。Charles Petzold 已经向我们展示
如何用 C 和 winapi  来编写 windows 程序。那么再回到这个问题,如果不提及MFC,那么C++ 与windows
有什么关系呢?



我给大家举个例子大家就清楚了。如果你要画这样一个场景:成千上万个蚂蚁在那里爬动,你准备怎么做?如果你是C 程序员,你会告诉我,建立一个蚂蚁
struct , 然后在 WM_PAINT 的消息下 调用 某个
自己写的函数,来画那成千上万的蚂蚁。这样可以实现。当然还有其他的方案,比如调用资源文件,将那些事先画好的蚂蚁图,以一定单位时间祯数连续播放,这样
你便看到了那成千上万的蚂蚁。到现在都与 C++ 无关,或者说都与
对象化编程无关。但是我再提个问题,如何让蚂蚁在爬动中实现一定的指令,比如让他们互相交流,让他们向其他蚂蚁群发起进攻。显然这时你会感到,C
这时变得相对虚弱了。而C++ 就有了用武之地。

如果我们事先设计一个 蚂蚁class,  这个class 表征了蚂蚁的特性,也赋予了对蚂蚁的操作功能,比如画一个蚂蚁,比如蚂蚁的爬行,比如蚂蚁的彼此交流,比如的进攻,等等。



44 楼的那个小程序,画了两个图形,而这两个图形是两个 object.  也就是说,我们可以在程序中创立 Object,然后对 Object 操作从而实现我们的要求。



Winapi 结合 C++ 来编写 windows 程序是一个很好的方法。


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-15 10:53
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
下面是这个五子棋程序的源代码,程序由3个文件组成,1个头文件和2个cpp 文件

当前基本可以实现人人对战模式,悔棋功能的代码还没写。

人机对战模式的代码也还没有写。


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-18 22:17
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
// wuziqi.h

#include <windows.h>

#include <string>

#include <vector>



#ifndef WUZIQI_H_

#define WUZIQI_H_



class Point

{

private:

    int x;

    int y;

    int state;

public:

    enum{NOCHESSMAN = -1, BLACK = 0, WHITE = 1};

    Point(int _x = 0, int _y = 0, int s = -1) : x(_x), y(_y), state(s){}

    void reset(){ state = NOCHESSMAN; }

    void setBlack(){ state = BLACK; }

    void setWhite(){ state = WHITE; }

    int getState(){ return state; }

    int getX(){ return x; }

    int getY(){ return y; }

};



class Mark

{

private:

    POINT point;

    COLORREF color;

public:

    Mark(){}

    Mark(const POINT & _point) : point(_point){ color = RGB(255, 0, 0);}

    Mark(int x, int y)

    {

        point.x = x;

        point.y = y;

        color = RGB(255, 0, 0);

    }

    bool operator!=(const Mark & anotherMark)

    {

        return (point.x != anotherMark.point.x || point.y != anotherMark.point.y);

    }

    void setColor(const COLORREF & col)

    {

        color = col;

    }

    void paintMark(HWND hwnd);

    void erase(HWND hwnd, const COLORREF & col);

};

/*

class QiZi

{

private:

    enum{ BLACK, WHITE, R = 16};

    POINT position;

    COLORREF color;

public:

    QiZi(){};

    QiZi(const POINT pos, const COLORREF col) : position(pos), color(col){}

    void paint(HWND hwnd);

};

*/

class Step

{

private:

    int step;

    int xIndex;

    int yIndex;

    int player;   // 0 for black, 1 for white

public:

    Step(){}

    Step(int s, int xI, int yI, int BorW)

    {

        step = s;

        xIndex = xI;

        yIndex = yI;

        player = BorW;

    }

    void getStepInfo(int & xI, int & yI, int & p)

    {

        xI = xIndex;

        yI = yIndex;

        p = player;

    }

};





class QiPan

{

private:

    enum{SIZE = 15, LENGTH = 504};

    std::string zeichen1;

    std::string zeichen2[15];

    Point points[SIZE][SIZE];

    Mark current_mark;

    Mark prev_mark;

    COLORREF bkgcol;

    bool gameover;

    std::string winner;

    std::vector<Step> vcSteps;

   

    bool findIndex(int x, int y, int & xIndex, int & yIndex);

public:

    QiPan();

    Point & getPoint(int xIndex, int yIndex);

    void paint(HWND hwnd);

    void drawChessman(HWND hwnd, Point point, int col);

    void eraseChessman(HWND hwnd, Point point);

    void drawAllChessman(HWND hwnd);

    void eraseAllChessman(HWND hwnd);

    void reset()

    {

        for(int i = 0; i<SIZE; i++)

        {

            for(int j = 0; j<SIZE; j++)

            {

                points[i][j].reset();

            }

        }

        gameover = false;

        vcSteps.clear();

    }

    void setCurrentMark(int x, int y);

    bool setStep(int x, int y, int round, Step & step);

    void setBkgColor(const COLORREF & col){ bkgcol = col; }

    COLORREF getBkgColor(){ return bkgcol; }

    Mark getCurrentMark(){ return current_mark; }

    Mark getPrevMark(){ return prev_mark; };

    void eraseMark(HWND hwnd, const COLORREF & col);

    void paintMark(HWND hwnd);

    void updateMark(){ prev_mark = current_mark;}

    bool isSetable(int x, int y);

    void addAStep(Step & step);

    void stepBack(){;}  // noch nicht fertig

    bool judger();

    std::string getWinner(){ return winner; }

};



class Player

{

private:

    enum{BLACK, WHITE, NOEFFECT};

    int sequence;

    bool current_player;

//    bool giveup;

    std::vector<Point> points;

public:

    Player(){sequence = NOEFFECT; current_player = false;};

    void setSequence(const int s = BLACK)

    {

        sequence = (s == BLACK || s == WHITE)? s : NOEFFECT;

    }

    void play(const Point & p);

    void stepback();

//    void giveup(){ giveup = true; }

};



class ComputerPlayer : public Player

{



};



class WuZiQi

{

private:

    int mode;

    static int step;

    Step a_step;

    int currentPlayer;

    QiPan chessboard;

    ComputerPlayer * pc;

    int gameState;

    int modeState;



    // for grafic user interface

    HWND hGroupMode, hGroupSequence;

    HWND hRadioBtnPP, hRadioBtnPC;

    HWND hRadioBtnBlack, hRadioBtnWhite;

    HWND hPushBtnStepback, hPushBtnBegin, hPushBtnGiveup;

    HWND hMessage;

    HINSTANCE hInst;

    COLORREF BKGCOL;

    std::string winner;

    void createWindow(HWND hwnd);

   

public:

    enum{ NULLSTATE, PLAYING, GAMEOVER}; //, NEWBEGIN};

    enum{ PVSP, PVSC, NOTHING};

    enum{ BLACK, WHITE, NOONE};

    enum{

        IDGROUP_MODE        = 101,

        IDGROUP_SEQUENCE    = 102,

        IDRADIOBTN_PP       = 103,

        IDRADIOBTN_PC       = 104,

        IDRADIOBTN_BLACK    = 105,

        IDRADIOBTN_WHITE    = 106,

        IDPUSHBTN_STEPBACK  = 107,

        IDPUSHBTN_BEGIN     = 108,

        IDPUSHBTN_GIVEUP    = 109 };

    WuZiQi(){BKGCOL = RGB(255, 240, 177);}

    COLORREF getBackgroudColor(){ return BKGCOL; }

    void setGameState(int gS = NULLSTATE){ gameState = gS; }

    int getGameState(){ return gameState; }

    LRESULT selectRadioBtn(HWND hRadioBtn)

    {

        return SendMessage(hRadioBtn, BM_SETCHECK, 1, 0);

    }

    LRESULT deselectRadioBtn(HWND hRadioBtn)

    {

        return SendMessage(hRadioBtn, BM_SETCHECK, 0, 0);

    }

    void setModeState(int mS = PVSC)

    {

        if(gameState == PLAYING)

        {

            modeState = mS;

            switch(modeState)

            {

            case PVSC:

                selectRadioBtn(hRadioBtnPC);

                deselectRadioBtn(hRadioBtnPP);

                break;

            case PVSP:

                selectRadioBtn(hRadioBtnPP);

                deselectRadioBtn(hRadioBtnPC);

                deselectRadioBtn(hRadioBtnBlack);

                deselectRadioBtn(hRadioBtnWhite);

                break;

            }

        }

    }

    void executeCommand(HWND hwnd, const int command);

    int getModeState(){ return modeState; }

    void setFocus(){ SetFocus(hGroupMode); }

    void init(HWND hwnd);

    void paintChessboard(HWND hwnd){ chessboard.paint(hwnd); }

    void paintMark(HWND hwnd, int x, int y);

    void paintAllChessman(HWND hwnd)

    {

        chessboard.drawAllChessman(hwnd);

    }

    void beginPlay(HWND hwnd)

    {

        if(gameState == NULLSTATE)

        {

            gameState = PLAYING;

            step = 0;

            currentPlayer = BLACK;

            chessboard.reset();

        }

   

        else if(gameState == GAMEOVER)

        {

            step = 0;

            currentPlayer = BLACK;

            chessboard.reset();

            chessboard.eraseAllChessman(hwnd);

            chessboard.paint(hwnd);

            gameState = PLAYING;

        }

        

    }

    void setChessman(HWND hwnd, int x, int y)

    {

        if(gameState == PLAYING)

        {

            // finde correct index;

            // create a step;

            if(chessboard.isSetable(x, y))

            {

              
 // step incremented, and so that change the player 0 for black, 1
for white

                stepCount();

                chessboard.setStep(x, y, step, a_step);

                // write step record;

                // update chessboard;



                chessboard.addAStep(a_step);

                chessboard.drawAllChessman(hwnd);

                if(chessboard.judger())

                {

                    gameOver();

              
     MessageBox(hMessage, chessboard.getWinner() ==
"Black"? "Black has won the game" : "White has won the game", "Winner",
MB_OK);

                }

            }

        }

    }

    void gameOver(){ gameState = GAMEOVER; }

    ComputerPlayer * getComputerPlayer(){ return pc; }

    int getCurrentPlayer(){ return currentPlayer; }

    int getMode(){ return modeState;}

    QiPan & getQiPan(){ return chessboard; }

    int getStepAmount(){ return step; }

    bool isPlaying(){ return gameState == PLAYING; }

    void setMode(const int m);

    void setSequence(const int sq);

    void stepCount(){ step++; currentPlayer = step&1; }

    ~WuZiQi(){ delete pc; }

};

#endif




自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-18 22:18
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
// wuziqi.cpp

#include "wuziqi.h"



void Mark::paintMark(HWND hwnd)

{

    HDC hdc = GetDC(hwnd);

    HPEN hpen = CreatePen(PS_SOLID, 1, color);

    SelectObject(hdc, hpen);

    MoveToEx(hdc, point.x, point.y, NULL);

    LineTo(hdc, point.x + 9, point.y);



    MoveToEx(hdc, point.x , point.y, NULL);

    LineTo(hdc, point.x, point.y + 9);



    MoveToEx(hdc, point.x + 36 , point.y, NULL);

    LineTo(hdc, point.x + 27, point.y);



    MoveToEx(hdc, point.x + 36 , point.y, NULL);

    LineTo(hdc, point.x + 36, point.y + 9);



    MoveToEx(hdc, point.x , point.y + 36, NULL);

    LineTo(hdc, point.x, point.y + 27);



    MoveToEx(hdc, point.x, point.y + 36, NULL);

    LineTo(hdc, point.x + 9, point.y + 36);



    MoveToEx(hdc, point.x + 36 , point.y + 36, NULL);

    LineTo(hdc, point.x + 27, point.y + 36);



    MoveToEx(hdc, point.x + 36 , point.y + 36, NULL);

    LineTo(hdc, point.x + 36, point.y + 27);

    DeleteObject(hpen);

    ReleaseDC(hwnd, hdc);

}



void Mark::erase(HWND hwnd, const COLORREF & col)

{

    setColor(col);

    paintMark(hwnd);

}



QiPan::QiPan()

{

    int a = LENGTH/(SIZE-1);

    int offset = 40;

    zeichen1 = "ABCDEFGHIJKLMNO";

    char temp[2];

    for(int i = 0; i<SIZE; i++)

    {

        sprintf(temp, "%d ", i+1);

        zeichen2[SIZE - i - 1] = temp;

        for(int j = 0; j<SIZE; j++)

        {

            points[i][j] = Point(offset + j * a, offset + i * a);

        }

    }

}



void QiPan::drawChessman(HWND hwnd, Point point, int col)

{

    HDC hdc;

    HPEN hpen;

    HBRUSH hbrush;

        

    hdc = GetDC(hwnd);

    if(col == 0) // black

    {

        hpen = CreatePen(PS_SOLID, 1, RGB(54,54,54));  // a little black

        hbrush = CreateSolidBrush(RGB(54,54,54));

    }

    else if(col == 1)  // white

    {

        hpen = CreatePen(PS_SOLID, 1, RGB(250,240,230));  // a little white

        hbrush = CreateSolidBrush(RGB(255,250,245));

    }

    else  // same color as background

    {

        hpen = CreatePen(PS_SOLID, 1, bkgcol);

        hbrush = CreateSolidBrush(bkgcol);

    }

    SelectObject(hdc, hpen);

    SelectObject(hdc, hbrush);



    Ellipse(hdc, point.getX() - 15, point.getY() - 15, point.getX() + 15, point.getY() + 15);

    DeleteObject(hpen);

    DeleteObject(hbrush);

    ReleaseDC(hwnd, hdc);

}



void QiPan::eraseChessman(HWND hwnd, Point point)

{

    this->drawChessman(hwnd, point, 2);  // draw it with background color

}



void QiPan::eraseAllChessman(HWND hwnd)

{

    for(int i = 0; i<SIZE; i++)

    {

       for(int j = 0; j<SIZE; j++)

       {

               drawChessman(hwnd, points[i][j], 2);

       }

    }

}

bool QiPan::findIndex(int x, int y, int & xIndex, int & yIndex)

{

    if(x<=562 && x>= 22 && y<=562 && y>=22)

    {

        if(x<=58)

            yIndex = 0;

        else if(x<=94)

            yIndex = 1;

        else if(x<=130)

            yIndex = 2;

        else if(x<=166)

            yIndex = 3;

        else if(x<=202)

            yIndex = 4;

        else if(x<=238)

            yIndex = 5;

        else if(x<=274)

            yIndex = 6;

        else if(x<=310)

            yIndex = 7;

        else if(x<=346)

            yIndex = 8;

        else if(x<=382)

            yIndex = 9;

        else if(x<=418)

            yIndex = 10;

        else if(x<=454)

            yIndex = 11;

        else if(x<=490)

            yIndex = 12;

        else if(x<=526)

            yIndex = 13;

        else if(x<=562)

            yIndex = 14;



        if(y<=58)

            xIndex = 0;

        else if(y<=94)

            xIndex = 1;

        else if(y<=130)

            xIndex = 2;

        else if(y<=166)

            xIndex = 3;

        else if(y<=202)

            xIndex = 4;

        else if(y<=238)

            xIndex = 5;

        else if(y<=274)

            xIndex = 6;

        else if(y<=310)

            xIndex = 7;

        else if(y<=346)

            xIndex = 8;

        else if(y<=382)

            xIndex = 9;

        else if(y<=418)

            xIndex = 10;

        else if(y<=454)

            xIndex = 11;

        else if(y<=490)

            xIndex = 12;

        else if(y<=526)

            xIndex = 13;

        else if(y<=562)

            xIndex = 14;

        return true;

    }

    else

        return false;

}



// wuziqi.cpp 下面部分请见下一个帖子




自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-02-18 22:25
快速回复:All about windows programming
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027680 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved