一個完整的Win32應用程序

#include<windows.h>
#include
<stdio.h>

LRESULT CALLBACK WinSunProc(
        HWND hwnd,
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam
        );
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPreInstance,
    LPSTR lpCmdLine,
    int nCmdShow
    )
{
    WNDCLASS wndcls;
    wndcls.cbClsExtra=0;
    wndcls.cbWndExtra=0;
    wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=WinSunProc;
    wndcls.lpszClassName="weixin";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(
&wndcls);

    HWND hwnd;
    hwnd=CreateWindow("weixin","這是標題欄顯示的內容",WS_OVERLAPPEDWINDOW,
        100,100,600,400,NULL,NULL,hInstance,NULL);//CreateWindow()的第一個參數必須取wncls.lpszClassName的值,這樣窗口才能創建成功,並且窗口的顯示屬性爲在WNDCLASS wncls中所設置的屬性。

    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG msg;
    while(GetMessage(
&msg,NULL,0,0))
    {
        TranslateMessage(
&msg);
        DispatchMessage(
&msg);
    }
    return 0;
}
LRESULT CALLBACK WinSunProc(
        HWND hwnd,
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam
        )
{
    switch(uMsg)
    {
    case WM_CHAR:
        char szChar[6];
        sprintf(szChar,"char is %d",wParam);
        MessageBox(hwnd,szChar,"weixin",0);
        
        break;
    case WM_LBUTTONDOWN:
        MessageBox(hwnd,"mouse clicked","weixin",0);
        HDC hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,0,"計算機編程語言",strlen("計算機編程語言"));
        ReleaseDC(hwnd,hdc);
        break;
    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT ps;
        hDC=BeginPaint(hwnd,
&ps);
        EndPaint(hwnd,
&ps);
        break;
    case WM_CLOSE:
        if(IDYES==MessageBox(hwnd,"是否真的結束?","weixin",MB_YESNO))
        { 
            DestroyWindow(hwnd);//注意,這並不能退出應用程序
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);//退出應用程序
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);//如果去掉default這一項,運行時看不到窗口,但是程序已運行了(從任務管理器中可以看到).
    }
    return 0;
}
//以下是頭文件 BTree.h
struct BTreeNode{
    ElemType data;
    BTreeNode* left;
    BTreeNode* right;
};
void InitBTree(BTreeNode* 
&BT)
{
    BT=NULL;
}
void CreateBTree(BTreeNode* 
&BT,char* a)
{
    BTreeNode* s[10];
    int top=-1;
    BT=NULL;
    BTreeNode *p;
    int k;
    istrstream ins(a);
    char ch;
    ins >>ch;
    while(ch!='@')
    {
        switch(ch)
        {
        case '(':
            top++;s[top]=p;k=1;
            break;
        case')':
            top--;
            break;
        case',':
            k=2;
            break;
        default:
            p=new BTreeNode;
            p->data=ch;p->left=p->right=NULL;
            if(BT==NULL)
                BT=p;
            else{
                switch(k)
                {
                case 1:
                    s[top]->left=p;
                    break;
                case 2:
                    s[top]->right=p;
                }
            }
        }
        ins>>ch;
    }
}

int BTreeEmpty(BTreeNode* BT)
 {
     return BT==NULL;
 }
 void Preorder(BTreeNode* BT)//前序遍歷
 {
     if(BT!=NULL){
         cout
<<BT->data<<' ';
         Preorder
(BT->left);
         Preorder(BT->right);
     }
 }
 void Inorder(BTreeNode* BT)//中序遍歷
 {
     if(BT!=NULL){
         Inorder(BT->left);
         cout
<<BT->data<<' ';
         Inorder
(BT->right);
     }
 }
 void Postorder(BTreeNode* BT)//後序遍歷
 {
     if(BT!=NULL){
         Postorder(BT->left);
         Postorder(BT->right);
         cout
<<BT->data<<' ';
     }
 }
 void 
Levelorder(BTreeNode* BT)
 {
     BTreeNode* q[30];
     int front
=0,rear=0;
     
BTreeNode* p;
     if(BT!
=NULL){
         
rear=(rear+1)%30;
         
q[rear]=BT;
     
}
     while(front!
=rear)
     
{
         front
=(front+1)%30;
         
p=q[front];
         
cout<<p->data<<' ';
         if
(p->left!=NULL){
             rear=(rear+1)%30;
             q[rear]=p->left;
         }
         if(p->right!=NULL)
         {
             rear=(rear+1)%30;
             q[rear]=p->right;
         }
     }
 }
 BTreeDepth(BTreeNode* BT)
 {
     if(BT==NULL)
         return 0;
     else
     {
         int dep1=BTreeDepth(BT->left);
         int dep2=BTreeDepth(BT->right);
         if(dep1>dep2)
             return dep1+1;
         else
             return dep2+1;
     }
 }
 void PrintBTree(BTreeNode* BT)
 {
     if(BT!=NULL)
     {
         cout
<<BT->data;
         if(BT->left!=NULL||BT->right!=NULL)
         {
             cout
<<'(';
             PrintBTree
(BT->left);
             if(BT->right!=NULL)
             cout
<<',';
             PrintBTree
(BT->right);
             cout
<<')';
         }
     }
 }
 void 
DeleteBTree(BTreeNode* BT)
 {
     if(BT!
=NULL)
     
{
         DeleteBTree(BT-
>left);
         DeleteBTree(BT->right);
         delete BT;
     }
 }
 void ClearBTree(BTreeNode* 
&BT)
 {
     DeleteBTree(BT);
     BT=NULL;
 }


//以下是源文件部分
#include
<iostream.h>
#include
<conio.h>
#include
<stdlib.h>
#include
<strstrea.h>
typedef char ElemType;
 

#include"BTree.h"
void main()
{
    BTreeNode* bt;
InitBTree(bt);
char b[50];
cout
<<"輸入以'@'字符作爲結束符的二叉樹廣義表表示:"<<endl;
cin.getline(b,sizeof(b));
CreateBTree(bt,b);
PrintBTree(bt);
cout<<endl;
cout<<"前序:";
Preorder(bt);cout<<endl;
cout<<"中序:";
Inorder(bt);cout<<endl;
cout<<"後序:";
Postorder(bt);cout<<endl;
cout<<"按層:";
Levelorder(bt);cout<<endl;
cout<<"二叉樹的深度爲:";
cout<<BTreeDepth(bt)<<endl;
ClearBTree(bt);
getch();
}
QQ羣:
34409541 討論網頁 
34409326 討論JAVA 已滿
34408784 討論VC++ 
34409699 討論VC++ 
9143041 討論MFC編程 
10614204 討論C# 
10613030 討論Win32編程 
10613067 討論遊戲開發 
18779860 討論JAVA 
*/

 
發佈了39 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章