控制台程序生成窗口

来源:互联网 发布:mindnode有windows版么 编辑:程序博客网 时间:2024/05/20 07:57

//控制台程序生成窗口
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>

LRESULT CALLBACK WinProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
 WNDCLASS wndclass;
 wndclass.hInstance=hInstance;
 wndclass.hCursor=LoadCursor(hInstance,IDC_ARROW);
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hbrBackground=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
 wndclass.hIcon=LoadIcon(hInstance,IDI_INFORMATION);
 wndclass.lpfnWndProc=WinProc;
 wndclass.lpszClassName=L"sunxin";
 wndclass.lpszMenuName=NULL;
 wndclass.style=CS_HREDRAW | CS_VREDRAW ;

 RegisterClass(&wndclass);
 HWND hwnd;
 hwnd=CreateWindow(L"sunxin",L"mywindow",WS_OVERLAPPEDWINDOW,300,150,600,500,NULL,NULL,hInstance,NULL);
 ShowWindow(hwnd,SW_SHOWNORMAL);
 UpdateWindow(hwnd);

 MSG msg;
 BOOL bret;
 while((bret=GetMessage(&msg,NULL,0,0))!=0)
 {
  if(bret==-1)
  {
   return -1;
  }
  else
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }

 }
 return msg.wParam;
}


LRESULT CALLBACK WinProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
 PAINTSTRUCT ps;
 HDC hDC=GetDC(hwnd);
 TCHAR greeting[] = _T("Hello, World!");

 switch (uMsg)
 {
 case WM_CHAR:
  char szChar[20];
  sprintf_s(szChar,"char code is %d",wParam);
  WCHAR wszClassName[20]; 
  //memset(wszClassName,0,sizeof(wszClassName)); 
  MultiByteToWideChar(CP_ACP,0,szChar,strlen(szChar)+1,wszClassName, 
       sizeof(wszClassName)/sizeof(wszClassName[0]));
  MessageBox(hwnd,wszClassName,L"char",0);
  break;
 case WM_LBUTTONDOWN:
  MessageBox(hwnd,L"asdasdf",L"adasd",0);  
  TextOut(hDC,20,20,L"sldfksfd",strlen("sldksfd"));
  ReleaseDC(hwnd,hDC);
  break;

 case WM_PAINT:
  HDC hdc;
  hdc = BeginPaint(hwnd, &ps);

  // Here your application is laid out.// For this introduction, we just print out "Hello, World!"
  // in the top left corner.
  TextOut(hdc,5, 5,greeting, _tcslen(greeting));
  // End application specific layout section.EndPaint(hWnd, &ps);
  EndPaint(hwnd,&ps);
  break;
 case WM_CLOSE:
  if(IDOK==MessageBox(hwnd,L"是否真的关闭",L"123",0))
  {
   DestroyWindow(hwnd);
  }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);
  break;
 default:
  return DefWindowProc(hwnd,uMsg, wParam, lParam);
  break;
 }
 return 0;
}

原创粉丝点击