C语言创建windows窗口实例_C语言教程-查字典教程网
C语言创建windows窗口实例
C语言创建windows窗口实例
发布时间:2016-12-28 来源:查字典编辑
摘要:耐得住寂寞,禁得起诱惑,这就是程序人生步骤:1.在WinMain中定义各种变量2.注册窗口类RegisterClass3.创建窗口Creat...

耐得住寂寞,禁得起诱惑,这就是程序人生

步骤:

1.在WinMain中定义各种变量

2.注册窗口类RegisterClass

3.创建窗口CreateWindow

4.显示窗口和更新窗口

复制代码 代码如下:

ShowWindow (hwnd, iCmdShow) ;

UpdateWindow (hwnd) ;

5.消息循环

复制代码 代码如下:

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

{

TranslateMessage (&msg) ;

DispatchMessage (&msg) ;

}

完整代码:

复制代码 代码如下:

#include <windows.h>

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

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )

{

MSG msg;

HWND hwnd;

static TCHAR szAppName[] = "hl";

WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.lpfnWndProc = MyProc;

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;

if(!RegisterClass(&wndclass))

{

MessageBox(NULL,TEXT("error"),TEXT("title"),MB_ICONERROR);

return 0;

}

hwnd = CreateWindow(szAppName,

TEXT("Hello"),

WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

CW_USEDEFAULT,

NULL,

NULL,

hInstance,

NULL

);

ShowWindow(hwnd,nShowCmd);

UpdateWindow(hwnd);

while(GetMessage(&msg,hwnd,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

return msg.wParam;

}

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

{

switch(message)

{

case WM_DESTROY:

PostQuitMessage(0);

return 0;

}

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

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C语言学习
    热门C语言学习
    编程开发子分类