标题:求一个windows创建窗口程序(最好附近注释)~
只看楼主
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
结帖率:99.25%
已结贴  问题点数:20 回复次数:5 
求一个windows创建窗口程序(最好附近注释)~
最近九九看到设计游戏要用到窗口程序~就是自己做一个窗口界面~于是想自学写一个窗口程序~但网上的资料很多很难找到核心~而且感觉窗口函数很多比较难学~有没有一个比较规范的窗口程序模板让我参考一下~

顺便附加一句~个人感觉此贴发在c不太合适~有没有关于写windows控制台的论坛?~

如果各版主认为此贴不合适~可以移动到相关论坛~谢啦~

[此贴子已经被作者于2017-2-26 13:30编辑过]

搜索更多相关主题的帖子: windows 控制台 最好 而且 
2017-02-26 13:16
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:10 
C在Windows平台下运行那个控制台窗口就是,对此窗口句柄操作就可以吧。
2017-02-26 15:27
勿乱
Rank: 2
等 级:论坛游民
帖 子:21
专家分:58
注 册:2017-2-7
得分:10 
这是一个贪吃蛇游戏  里面有窗口程序,具体我也了解的不够。
// MySnake.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "time.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst;
TCHAR szTitle[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    MSG msg;
    HACCEL hAccelTable;
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_MYSNAKE, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }
    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MYSNAKE);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_MYSNAKE);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = (LPCSTR)IDC_MYSNAKE;
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
    return RegisterClassEx(&wcex);
}
HDC hdc;
int row=20;
int col=20;
#define  WIDTH 30
#define  HEIGHT 30
#define  MAXSIZE  50
POINT m_food;
struct Snake
{
   POINT m_pos[MAXSIZE];
   int m_direction;
   int len;
};
Snake m_snake;
void generatefood()
{
    srand((unsigned)time(NULL));
    m_food.x=rand()%20;
    m_food.y=rand()%20;
}
void init_snake()
{
    m_snake.len=3;
    m_snake.m_pos[0].x=0;
    m_snake.m_pos[0].y=0;
    m_snake.m_direction=1;
    for(int n=1;n<MAXSIZE;n++)
    {
        m_snake.m_pos[n].x=-1;
        m_snake.m_pos[n].y=-1;
    }
    generatefood();
}
void MyPaint(HDC hdc)
{
    HBRUSH hbr=CreateSolidBrush(RGB(255,0,0));
    SelectObject(hdc,hbr);
    for(int y=0;y<row;y++)
    {
        for(int x=0;x<col;x++)
        {
            Rectangle(hdc,x*WIDTH,y*HEIGHT,(x+1)*WIDTH,(y+1)*HEIGHT);
        }
    }
    HBRUSH hbrgreen=CreateSolidBrush(RGB(0,255,0));
    SelectObject(hdc,hbrgreen);
    for(int n=0;n<m_snake.len;n++)
    {
        Rectangle(hdc,m_snake.m_pos[n].x*WIDTH,m_snake.m_pos[n].y*HEIGHT,(m_snake.m_pos[n].x+1)*WIDTH,
            (m_snake.m_pos[n].y+1)*HEIGHT);
    }
    HBRUSH hbrblue=CreateSolidBrush(RGB(0,0,255));
    SelectObject(hdc,hbrblue);
    Rectangle(hdc,m_food.x*WIDTH,m_food.y*HEIGHT,(m_food.x+1)*WIDTH,(m_food.y+1)*HEIGHT);
}
void OnStart(HWND hWnd)
{
    init_snake();
    SetTimer(hWnd,1,300,NULL);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;
   hInst = hInstance;
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
   if (!hWnd)
   {
      return FALSE;
   }
   RECT crc,wrc;
   GetClientRect(hWnd,&crc);
   GetWindowRect(hWnd,&wrc);
   int borderwidth=(wrc.right-wrc.left)-(crc.right-crc.left);
   int borderheight=(wrc.bottom-wrc.top)-(crc.bottom-crc.top);
   SetWindowPos(hWnd,NULL,0,0,600+borderwidth,600+borderheight,SWP_NOMOVE);
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   hdc=GetDC(hWnd);
   OnStart(hWnd);
   return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int n;
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

    switch (message)
    {
        case WM_COMMAND:
            wmId    = LOWORD(wParam);
            wmEvent = HIWORD(wParam);
            switch (wmId)
            {
                case IDM_EXIT:
                   DestroyWindow(hWnd);
                   break;
                default:
                   return DefWindowProc(hWnd, message, wParam, lParam);
            }
            break;
            case WM_KEYDOWN:
                switch(wParam)
                {
                case VK_UP:
                    m_snake.m_direction=0;
                    break;
                case VK_RIGHT:
                    m_snake.m_direction=1;
                    break;
                case VK_DOWN:
                    m_snake.m_direction=2;
                    break;
                case  VK_LEFT:
                    m_snake.m_direction=3;
                    break;
                }
                break;
                case WM_TIMER:
                    for( n=m_snake.len-1;n>=1;n--)
                    {
                        m_snake.m_pos[n]=m_snake.m_pos[n-1];
                    }
                    if(0==m_snake.m_direction)
                    {
                        m_snake.m_pos[0].y=m_snake.m_pos[0].y-1;
                    }
                    if(1==m_snake.m_direction)
                    {
                        m_snake.m_pos[0].x=m_snake.m_pos[0].x+1;
                    }
                    if(2==m_snake.m_direction)
                    {
                        m_snake.m_pos[0].y=m_snake.m_pos[0].y+1;
                    }
                    if( 3==m_snake.m_direction)
                    {

                        m_snake.m_pos[0].x=m_snake.m_pos[0].x-1;
                    }
                    if(m_snake.m_pos[0].x==m_food.x&&m_snake.m_pos[0].y==m_food.y)
                    {
                        m_snake.len++;
                        generatefood();
                    }
                    if(m_snake.m_pos[0].x>19||m_snake.m_pos[0].y>19||m_snake.m_pos[0].x<0||m_snake.m_pos[0].y<0)
                    {
                        KillTimer(hWnd,1);
                        if(IDYES==MessageBox(hWnd,"Game over,你想重新开始吗?","提示",MB_YESNO))
                        {
                            OnStart(hWnd);
                        }
                        else
                        {
                            PostQuitMessage(0);
                        }
                    }
                    hdc=GetDC(hWnd);
                    MyPaint(hdc);
                    break;
        
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            MyPaint(hdc);
            EndPaint(hWnd, &ps);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}
2017-02-26 15:40
勿乱
Rank: 2
等 级:论坛游民
帖 子:21
专家分:58
注 册:2017-2-7
得分:0 
,我们课本上的
2017-02-26 15:44
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:0 
不用控制台,可在编译环境的连接器设置里选择类似“不使用控制台”的选项。
最简单的窗口
/* test.c */
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL,TEXT("我的第一个窗口程序"),TEXT("简单而强大的C语言"),MB_OK);
    return 0;
}

最基本的窗口
/* test.c */
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG msg;
    memset(&wc,0,sizeof(wc));
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          640,
                          480,
                          NULL,NULL,hInstance,NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "创建窗口失败!","错误!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
2017-02-26 16:00
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
得分:0 
感谢各位大神出手相助~这段时间比较忙~就没怎么看了~先保留~以后有时间慢慢消化~


[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-02-26 19:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-474561-1-1.html




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

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