Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1251602
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: 嵌入式

2012-02-08 14:14:06

创建窗口程序

#include
#include

 

LRESULT CALLBACK WinDouProc(
    HWND hwnd,      // handle to window
    UINT uMsg,      // message identifier
    WPARAM wParam,  // first message parameter
    LPARAM lParam   // second message parameter
);


int WINAPI WinMain(
    HINSTANCE hInstance,  // handle to current instance
    HINSTANCE hPrevInstance,  // handle to previous instance
    LPSTR lpCmdLine,      // pointer to command line
    int nCmdShow          // show state of window
)
{
     WNDCLASS wndclass;     //先设计窗口类
 
     wndclass.cbCl***tra = 0;
     wndclass.cbWndExtra = 0;
     wndclass.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
     wndclass.hCursor = LoadCursor(NULL,IDC_HELP);
     wndclass.hIcon = LoadIcon(NULL,IDI_WARNING);
     wndclass.hInstance = hInstance;
     wndclass.lpfnWndProc = WinDouProc;
     wndclass.lpszClassName = "Magic_Maggie";
     wndclass.lpszMenuName = 0;
     wndclass.style = CS_VREDRAW | CS_HREDRAW; 

     //某一个变量原油几个变量去掉一个特征,可以用取反(~)后再进行与(&)

     //例如:style上去掉CS_NOCLOSE,可以style&~CS_NOCLOSE;
   
     RegisterClass(&wndclass);     ///注意先建立再注册昂
 
     HWND hwnd;    //创建窗口类
     hwnd = CreateWindow("Magic_Maggie","DouDou",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,0);
 //第一个参数必须与lpszClassName相同,第二个参数指的是标题

    ShowWindow(hwnd,SW_SHOWNORMAL);   // 显示
    UpdateWindow(hwnd);

 

    MSG msg;     //消息循环
    while(GetMessage(&msg,NULL,0,0))
    {
           TranslateMessage(&msg);
           DispatchMessage(&msg);   //触发WinDouProc
     }
    return 0;
}

 

 

LRESULT CALLBACK WinDouProc(
    HWND hwnd,      // handle to window
     UINT uMsg,      // message identifier

    WPARAM wParam,  // first message parameter
    LPARAM lParam   // second message parameter
)
{
 switch(uMsg)
 {
 case WM_LBUTTONDOWN:
      MessageBox(hwnd,"您按下了鼠标左键昂","豆豆的程序",MB_OK);
      HDC hdc;
      hdc = GetDC(hwnd);     

     //The GetDC function retrieves a handle to a display device context for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the device context. 
      TextOut(hdc,0,0,"感谢您对豆豆程序的支持昂",strlen("感谢您对豆豆程序的支持昂"));
      ReleaseDC(hwnd,hdc);
      break;
 

case WM_CHAR:
      char szChar[20];
      sprintf(szChar,"Char is %d",wParam);
      MessageBox(hwnd,szChar,"豆豆的程序",MB_OK);
      break;
 

case WM_PAINT:
      PAINTSTRUCT ps;
      HDC hDc;
      hDc = BeginPaint(hwnd,&ps);
      TextOut(hDc,0,0,"这个是重绘滴哦",strlen("这个是重绘滴哦"));
      EndPaint(hwnd,&ps);
      break;
 

case WM_CLOSE:   //这个case与下边的destroy这个case不要弄错了,否则窗口不出现,但任务管理器中运行
      if(IDYES == MessageBox(hwnd,"您真的要退出么?","豆豆的程序",MB_YESNO))
      {
           DestroyWindow(hwnd);
      }
      break;
 

case WM_DESTROY:
      PostQuitMessage(0);
      //////////////////////////////////////////?????????????????????
      break;
 

default:
     return DefWindowProc(hwnd,uMsg,wParam,lParam);  // 别忘记了return
  
  }
 return 0;
}

 

每天都要一点点提高   之  总结:

  • GetDC与ReleaseDC配套使用,BeginPaint与EndPaint配套使用,且后一对只能用在WM_PAINT中,其一对不能用在WM_PAINT中。
  • 编程规范:if里多把常量放在前边,例如if(IDYES == MessageBox(hwnd,"您真的要退出么?","豆豆的程序",MB_YESNO))
  •  上一条中的判断要放在WM_CLOSE 中,不要放在WM_DESTROY中
  • 如果省略了case里的default,不显示窗口。
  • LRESULT实际long类型,如果作为函数返回类型时,别忘记了return 0
  • cmd下nohttp://blog.sina.com.cn/s/blog_4a87cb870100ad2v.htmltepad 1.txt  新建记事本文

阅读(732) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~