vc++ 窗口定义,消息循环

vc++窗口定义,消息循环是要放到winMain()里边还是外边?... vc++ 窗口定义,消息循环是要放到winMain()里边还是外边? 展开
 我来答
匿名用户
2014-03-02
展开全部

外面啊!


示例:

#include <windows.h>
#include "resource.h"
#include <stdio.h>
int x,y;
LRESULT CALLBACK myproc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
 switch(uMsg)
 {
 case WM_CLOSE:
  if(IDOK==MessageBox(hwnd,"你是真的要退出吗?","系统提示",MB_OKCANCEL|MB_ICONINFORMATION))
  {
   DestroyWindow(hwnd);
  }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 case WM_CHAR:
  {
  char buf[100];
  sprintf(buf,"%d",wParam);
  MessageBox(hwnd,buf,"show",MB_OK);
  break;
  }
 case WM_MOUSEMOVE:
  {//这里和上面的情况用大括号一是为了防止buf的重复定义,二是为解决类似int x=LOWORD(lParam);的定义编译器糊涂报错。
  HDC hdc=GetDC(hwnd);
  char buf[100];
  sprintf(buf,"x=%d,y=%d",x,y); 
  SetTextColor(hdc,RGB(255,255,255));
  TextOut(hdc,0,0,buf,strlen(buf));
  //InvalidateRect(hwnd,NULL,true);
  //PostMessage(hwnd,WM_PAINT,0,0);
  //SendMessage(hwnd,WM_PAINT,0,0);
  x=LOWORD(lParam);
  y=HIWORD(lParam); 
  memset(buf,0,100);
  sprintf(buf,"x=%d,y=%d",x,y);
  SetTextColor(hdc,RGB(0,0,0));
  TextOut(hdc,0,0,buf,strlen(buf));
  ReleaseDC(hwnd,hdc);
  break;
  }
 }
 return 1;
}
int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
 WNDCLASS wndclass;
 wndclass.cbClsExtra=NULL;
 wndclass.cbWndExtra=NULL;

 wndclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
 
 //wndclass.hCursor=LoadCursor(NULL,IDC_HAND);
 wndclass.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(IDC_POINTER));

 //wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
 wndclass.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));

 wndclass.hInstance=hInstance;
 wndclass.lpfnWndProc=myproc;
 wndclass.lpszClassName="mywnd";
 wndclass.lpszMenuName=NULL;
 wndclass.style=CS_HREDRAW;
 RegisterClass(&wndclass);
 HWND hwnd=CreateWindow("mywnd","window test",WS_OVERLAPPEDWINDOW,100,100,400,400,NULL,NULL,hInstance,NULL);
 ShowWindow(hwnd,SW_SHOW);
 MSG msg;
 while(GetMessage(&msg,NULL,NULL,NULL))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return 1;
}

详细请参考:http://www.codesky.net/article/200411/47883.html

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式