| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1184 人关注过本帖
标题:用win32 Application创建一个窗体,想在窗口中显示出一个按钮,请各位帮忙找 ...
只看楼主 加入收藏
he189
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-8-17
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:4 
用win32 Application创建一个窗体,想在窗口中显示出一个按钮,请各位帮忙找一下我的错误
为什么我用这个方法编写窗口,然后想单击鼠标左键在窗口中显示出一个按钮,怎么不行呢,请高手指点下


#include <afxwin.h>
#include<windows.h>
LRESULT CALLBACK WinSunProc(
    HWND    hwnd,
    UINT    uMsg,
    WPARAM    wParam,
    LPARAM    lParam
    );
CButton button;
HWND    hwnd;


int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR      lpCmdLine,
    int  nCmdShow
    )
{   
    WNDCLASS wndcls;
    wndcls.cbClsExtra = 0;
    wndcls.cbWndExtra = 0;
    wndcls.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndcls.hIcon = LoadIcon (NULL,IDI_INFORMATION);
    wndcls.hCursor = LoadCursor (NULL,IDC_CROSS);
    wndcls.hInstance = hInstance;
    wndcls.lpfnWndProc = WinSunProc;   
    wndcls.lpszClassName = "welcome";
    wndcls.style = CS_HREDRAW | CS_VREDRAW;
    wndcls.lpszMenuName = NULL;

    RegisterClass(&wndcls);    //注册窗口类

    HWND hwnd;
    hwnd = CreateWindow ("welcome","My first dialog", WS_OVERLAPPEDWINDOW, 0, 0, 600, 400, NULL, NULL, hInstance, NULL );
    ShowWindow (hwnd, SW_SHOWNORMAL);
    UpdateWindow (hwnd);

    ///////////
    MSG  msg;
    while (GetMessage (&msg, NULL, 0, 0 ))
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }//endof while
    return 0;



}    //endof winMain()


LRESULT CALLBACK WinSunProc(
    HWND    hwnd,
    UINT    uMsg,
    WPARAM    wParam,
    LPARAM    lParam
    )
{    HDC hDC;///handle to DC
    HBRUSH hBrush; ///用的是画刷,画出的图里面是有颜色的
    HPEN hPen ;///handle to object画笔画出的只是线条
    PAINTSTRUCT PtStr;
    POINT points [6] = { { 100,212}, {70,227},{70,300},{130,300},{130,227},{100,212} };//用6个点确定一个五边形
   
    switch(uMsg)
    {
   
    case WM_LBUTTONDOWN:
        

        {/////此代码用于创建按钮,未成功
            CWnd *pWnd;   
            pWnd=CWnd::FromHandle(hwnd);   
            CRect rect;
            GetClientRect(hwnd,rect);
            rect.left +=300;
            rect.right =rect.left +100;
            rect.top +=200;
            rect.bottom =rect.top +50;
            button.Create ("Button",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect,pWnd,201);

        
        }
        break;
        /////////////
    case WM_RBUTTONDOWN:
        ShowWindow (hwnd, SW_MAXIMIZE);
        break;

   
    case WM_PAINT://///填充图形
        hDC = BeginPaint (hwnd , & PtStr);
        hPen = (HPEN) GetStockObject(NULL_PEN);//默认颜色
        SelectObject (hDC,hPen);
        hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
        SelectObject (hDC,hBrush);
        LineTo (hDC, 70, 70);
        DeleteObject(hPen);
        hPen = CreatePen(PS_SOLID,1,RGB(255,0,0));///pen style,pen width,pen color定义画笔
        SelectObject(hDC,hPen);
        //LineTo(hDC,180,90);/////3点确定一个三角形
        //LineTo(hDC,100,137);
        //LineTo(hDC,70,70);
        Polyline(hDC,points,6);//左边最下方显示的图形
        Arc(hDC,63,137,138,212,100,137,100,137);
        Arc(hDC,83,157,91,165,87,161,87,161);
        Arc(hDC,118,157,126,165,122,161,122,161);
        RoundRect(hDC,101,185,116,193,15,8);

        DeleteObject(hPen);
        DeleteObject(hBrush);
        EndPaint(hwnd,&PtStr);
        return 0;
   



    case WM_USER:


        {
        /*    CWnd *pWnd;   
            pWnd=CWnd::FromHandle(hwnd);   

            CRect rect;
            GetClientRect(hwnd,rect);
            rect.left +=190;
            rect.right =rect.left +50;
            rect.top +=80;
            rect.bottom =rect.top +50;
            button.Create("Button",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect,pWnd,201);*/

        }
        break;
    case WM_CLOSE:
        if(IDYES == MessageBox(hwnd,"是否真的结束?","要退出程序吗?",MB_YESNO))
        {
            DestroyWindow(hwnd);
        }//ENDOF IF
        break;
    case WM_DESTROY:
        PostQuitMessage(0);

        break;
    ///////////////

    case WM_KEYUP:  // 当收到按ESC键的消息时,(如果当前是全屏模式,必须要加入退出方式)
     switch (wParam) {
                      case VK_ESCAPE:
                        MessageBox(hwnd,"您按下了ESC键","消息提示",MB_OK|MB_ICONEXCLAMATION);
                        return 0;
    break;
   }
     break;

    default:
        return DefWindowProc(hwnd,uMsg, wParam, lParam);
    }//endof swith()

    return 0;
   
}//end of WinSunProc()

BOOL CButton(WPARAM wParam,LPARAM lParam)
{   
    WORD nCode = HIWORD(wParam);
    WORD nID = LOWORD(wParam);
            if((nCode==BN_CLICKED)&&(nID==201))
            {
                    MessageBox(hwnd,"hello","你好",MB_OK);
            }
return CButton(wParam,lParam);
}
搜索更多相关主题的帖子: 鼠标 include button 
2011-08-17 16:13
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:10 
额。
你在WIN SDK里用MFC的类。

这个有点麻烦,一般也不会这么用。还不如直接用Win API来创建呢。
程序代码:
case WM_LBUTTONDOWN:
    {/////此代码用于创建按钮,未成功
        //static CWnd *pWnd;   
        //pWnd=CWnd::FromHandle(hwnd);   
        CRect rect;
        GetClientRect(hwnd,rect);
        rect.left +=300;
        rect.right =rect.left +100;
        rect.top +=200;
        rect.bottom =rect.top +50;
        //button.Create ("Button",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect,pWnd,201);*/
        HWND hwndBtn;
        hwndBtn = CreateWindow("button", "Button", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 
            rect.left,
            rect.top,
            rect.Width(),
            rect.Height(),
            hwnd,
            (HMENU)201,
            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
            NULL);
    }
    break;
    /////////////
2011-08-17 16:50
zhoufeng1988
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:北京
等 级:贵宾
威 望:27
帖 子:1432
专家分:6329
注 册:2009-5-31
收藏
得分:10 
错误是:afxwin1.inl下19行,afxCurrentInstance为NULL,才出现的断言错误。
MFC和SDK还是有很大区别的。
MFC用API好说,但API反过去调MFC,就不是那么容易了。
2011-08-17 16:55
he189
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-8-17
收藏
得分:0 
谢谢各位的帮助,问题已经解决了
2011-08-18 17:11
lansunlong
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-1-28
收藏
得分:0 
回复 4楼 he189
怎么解决的呢
2011-08-20 17:18
快速回复:用win32 Application创建一个窗体,想在窗口中显示出一个按钮,请各位 ...
数据加载中...
 
   



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

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