qq_24278205
逄家大院
采纳率66.7%
2015-02-09 05:26 阅读 4.6k
已采纳

用vs编写了一个程序,运行时窗口显示不出来,但是这个程序确实在运行,请问哪里出错了呢?

#include
struct //定义按钮的风格和显示文本
{
long style;
char *text;
}
button[] = //定义9类按钮
{
BS_PUSHBUTTON, "按键按钮",
BS_DEFPUSHBUTTON, "默认按键按钮",
BS_CHECKBOX, "检查按钮",
BS_AUTOCHECKBOX, "自动检查按钮",
BS_GROUPBOX, "复选框",
BS_3STATE, "检查按钮",
BS_AUTO3STATE, "三状态自动检查按钮",
BS_AUTORADIOBUTTON, "自动圆按钮",
BS_RADIOBUTTON, "圆按钮",
};
//取得按钮的数目
#define NUM (sizeof button/sizeof button[0])
//定义应用程序的名字
static char szAppName[] = "BtnLook";
//预先声明消息处理、窗口类申请和初始化函数
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
//函数:WinMain
//作用:主应用函数
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
MSG msg;
//申请窗口类
MyRegisterClass(hInstance);
//应用程序的初始化
InitInstance(hInstance, iCmdShow);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//函数:MyRegisterClass
//作用:注册窗口类
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
return RegisterClassEx(&wndclass);
}
//函数:InitInstance
//作用:初始化应用程序
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hwnd;
hwnd = CreateWindow(szAppName, "按钮设计",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
return TRUE;
}
//函数:WndProc
//作用:消息处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{//窗口上显示提示信息
static char szTop[] = "消息 参数wParam 参数lParam",
szUnd[] = "____ ______ ______",
szFormat[] = "%-16s%04X-%04X %04X-%04X",
szBuffer[50];
//定义需要建立的按钮
static HWND hwndButton[NUM];
static RECT rect;
static int cxChar, cyChar;
HDC hdc;
PAINTSTRUCT ps;
int i;
TEXTMETRIC tm;
switch (iMsg)
{
case WM_CREATE:
//取得图形设备描述表的句柄
hdc = GetDC(hwnd);
SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
//创建按钮
for (i = 0; i < NUM; i++)
hwndButton[i] = CreateWindow("button",
button[i].text,
WS_CHILD | WS_VISIBLE | button[i].style,
cxChar, cyChar * (6 + 2 * i), 22 * cxChar,
7 * cyChar / 4,
hwnd,
(HMENU)i,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
return 0;
//取得文本显示区域的大小

case WM_SIZE:rect.left = 24 * cxChar;
    rect.top = 2 * cyChar;
    rect.right = LOWORD(lParam);
    rect.bottom = HIWORD(lParam);
    return 0;
    //重画文本

case WM_PAINT:InvalidateRect(hwnd, &rect, TRUE);
    hdc = BeginPaint(hwnd, &ps);
    SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
    SetBkMode(hdc, TRANSPARENT);
    //输出文本头
    TextOut(hdc, 24 * cxChar, cyChar, szTop, sizeof(szTop) - 1);
    TextOut(hdc, 24 * cxChar, cyChar, szUnd, sizeof(szUnd) - 1);
    EndPaint(hwnd, &ps);
    return 0;

case WM_DRAWITEM:

case WM_COMMAND:
    ScrollWindow(hwnd, 0, -cyChar, &rect, &rect);
    hdc = GetDC(hwnd);
    SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
    //输出新的消息
    TextOut(hdc, 24 * cxChar,
        cyChar * (rect.bottom / cyChar - 1),
        szBuffer, wsprintf(szBuffer,
        szFormat,
        iMsg == WM_DRAWITEM ? "WM_DRAWITEM" : "WM_COMMAND",
        (int)(wParam), (int)(wParam),
        HIWORD(lParam), LOWORD(lParam)));
    //释放图形设备资源
    ReleaseDC(hwnd, hdc);
    //重新描绘客户区域
    ValidateRect(hwnd, &rect);
    //弹出消息对话框
    switch (LOWORD(wParam))
    {
    case 0:
    case 1:
    case 2:
    case 3:
        MessageBox(hwnd, "Good", "Yes", MB_OKCANCEL);
        break;
        //推出应用程序


    }   
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hwnd, iMsg, wParam, lParam);

}   
return 0;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    testcs_dn 微wx笑 2015-02-09 09:07

    产生该问题的根本原因请参考:RegisterClass和RegisterClassEx有什么区别?

    点赞 评论 复制链接分享
  • testcs_dn 微wx笑 2015-02-09 06:58

    好久不写c++的代码了,亲自新建了一个项目调试了一下:
    图片说明
    图片说明

    点赞 评论 复制链接分享
  • testcs_dn 微wx笑 2015-02-09 09:04
    点赞 评论 复制链接分享

相关推荐