1. 程式人生 > 其它 >第一個視窗程式

第一個視窗程式

第一個視窗程式

#include "stdafx.h"
#include<windows.h>
#include<stdio.h>
//視窗程式
LRESULT CALLBACK  WindowProc(
   HWND hwnd,
   UINT uMsg,
   WPARAM wParam,
   LPARAM lParam
);

int APIENTRY WinMain(HINSTANCE hInstance,//handle to current instance  當前模組所在的位置
                     HINSTANCE hPrevInstance,//handle to previous instance
                     LPSTR     lpCmdLine,// command line
                     int       nCmdShow) //show state
{
 	// TODO: Place code here.
	//HANDLE 核心控制代碼
	//HWND 視窗控制代碼
	//HDC 指向裝置上下文的控制代碼
	//HINSTANCE 真正物件在0環 這個東西只是一個索引 DWORD  指向一個模組
	 
	/*DWORD dwAddr = (DWORD) hInstance;
	
	char szOutBuff[0x80];
	sprintf(szOutBuff,"模組地址,%x",dwAddr);
	OutputDebugString(szOutBuff);
*/

	//第一步定義你的視窗是什麼樣的?
	//建立要給字串
	
	char szOutBuff[0x80];
	TCHAR className[] = TEXT("My First Window");
	WNDCLASS wndclass ={0};
	wndclass.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
	wndclass.lpszClassName =className;
	wndclass.hInstance = hInstance  ;//當前程式屬於哪個程式
	wndclass.lpfnWndProc=  WindowProc;//視窗處理程式  不是呼叫函式  視窗處理過程
	//註冊視窗類
	RegisterClass(&wndclass);


	// 2. 建立並顯示視窗
	HWND hwnd = CreateWindow(
		className,
		TEXT("我的第一個視窗"),
		WS_OVERLAPPEDWINDOW,
		10,//單位都是畫素
		10,
		600,
		300,
		NULL,
		NULL,
		hInstance,
		NULL
		 );
	if(hwnd == NULL)
	{
	
	sprintf(szOutBuff,"Error : %d",GetLastError());
	OutputDebugString(szOutBuff);
	return 0;
	}
	//3.展示視窗,以及獲取訊息
	ShowWindow(hwnd,SW_SHOW);
	MSG msg;
	BOOL bRet;
	//GetMessage是從訊息佇列中取得視窗的資訊 ,第一個引數是取出來放的地址,第二個引數是所有的視窗的所有訊息
	//判斷是否qu'dao
	while((bRet = GetMessage(&msg ,NULL,0 , 0))!=0){
	
	if(bRet == -1)
	{
		//handle the error and possibly exit
	}
	else
	{
		//轉發訊息
		TranslateMessage(&msg);
		//分發訊息,呼叫訊息處理函式
		DispatchMessage(&msg);
	}
	
	}
	


	return 0;
}

LRESULT CALLBACK  WindowProc(
   HWND hwnd,
   UINT uMsg,
   WPARAM wParam,
   LPARAM lParam
   ){


return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

呼叫過程